Проблема с переводом в байты

Вот ошибка - и сразу скажу что я новичок в пайтоне, а задача у меня зашифровать строку. Но сначала была ошибка с переводом в байты, а теперь вот

Traceback (most recent call last):
  File "main.py", line 46, in <module>
    encrypted = encrypt(str(data).encode("utf-8"), str(password))
  File "main.py", line 11, in encrypt
    raw = pad(raw)
  File "main.py", line 7, in <lambda>
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
TypeError: can't concat str to bytes

Вот мой код

import qrcode
import base64
import hashlib
from Crypto.Cipher import AES
from Crypto import Random
BLOCK_SIZE = 16
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
def encrypt(raw, password):
    private_key = hashlib.sha256(password.encode("utf-8")).digest()
    raw = pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw))
 
 
def decrypt(enc, password):
    private_key = hashlib.sha256(password.encode("utf-8")).digest()
    enc = base64.b64decode(enc)
    iv = enc[:16]
    cipher = AES.new(private_key, AES.MODE_CBC, iv)
    return unpad(cipher.decrypt(enc[16:]))
print("CryptQR v1")
print("==============================================")
while True:
  lang = input("Choose the language - 1)Russian  2)English: ")
  if (lang == "1"): 
    a = input("Выберите режим - 1)Создание QR  2)Открытие QR: ")
    if (a == "1"): # Создание QR режим - QR Creating Mode
      while True:
        data = input ("Введите текст (до 512 символов): ")
        if (len(data) > 512):
          print("==============================================")
          print("Вы не можете ввести больше 512 символов!")
          print("==============================================")
          continue
        else:
          while True:
            password = input ("Введите пароль (От 16 символов): ")
            if (len(password) < 16):
              print("==============================================")
              print("Вы не можете ввести меньше 16 символов!")
              print("==============================================")
              continue
            else:
              encrypted = encrypt(str(data).encode("utf-8"), str(password))
              filename = "cryptqr.png"
              img = qrcode.make(str(encrypted))
              img.save(filename)
              print("==============================================")
              print("QR-код создан!")
              print("==============================================")
              break
          break
    break
  elif (lang == "2"): # Часть на наглийском - English part
    a = input("Choose mode - 1)Create QR  2)Open QR: ")
    if (a == "1"): # Создание QR режима - QR Creating Mode
      while True:
        data = input ("Enter text (up to 512 characters): ")
        if (len(data) > 512):
          print("==============================================")
          print("You cannot enter more than 512 characters!")
          print("==============================================")
          continue
        else:
          while True:
            password = input ("Enter password (From 16 characters):")
            if (len(password) < 16):
              print("==============================================")
              print("You cannot enter less than 16 characters!")
              print("==============================================")
              continue
            else:
              encrypted = encrypt(str(data), str(password))
              filename = "cryptqr.png"
              img = qrcode.make(str(encrypted))
              img.save(filename)
              print("==============================================")
              print("QR code created successfully!")
              print("==============================================")
              break
          break
    break
  else:
    continue
#pip install --upgrade pip
#pip3 install pycryptodome

Ответы (1 шт):

Автор решения: Non

pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * chr(BLOCK_SIZE - len(s) % BLOCK_SIZE).encode()

Должно решить проблему

→ Ссылка