I am writing a program for the school. I get the error described above for about 20 minutes. Here is the code:

def aes(): #aes os.system('cls') print('1.Шифруем') print('2.Дешифруем') c = input('Ваш выбор:') if int(c) == 1: #shifr os.system('cls') print('Шифруем значит') print('Введите текст, который хотите зашифровать') text = input() f = open('plaintext.txt', 'w') f.write(text) f.close() BLOCK_SIZE = 32 PADDING = '{' pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s))) secret = os.urandom(BLOCK_SIZE) f = open('aeskey.txt', 'w') f.write(str(secret)) f.close() f = open('plaintext.txt', 'r') privateInfo = f.read() f.close() cipher = AES.new(secret) encoded = EncodeAES(cipher, privateInfo) f = open('plaintext.txt', 'w') f.write(str(encoded)) f.close() print(str(encoded)) if int(c) == 2: os.system('cls') print('Дешифруем значит') f = open('plaintext.txt','r') encryptedString = f.read() f.close() PADDING = '{' DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING) encryption = encryptedString f = open('aeskey.txt', 'r') key = f.read() f.close() cipher = AES.new(key) decoded = DecodeAES(cipher, encryption) f = open('plaintext.txt', 'w') f.write(decoded) f.close() print(decoded) 

Fully error:

Traceback (most recent call last): File "C: /Users/vital/Desktop/Prog/Python/Enc_dec/Enc_dec.py", line 341, in aes ()
File "C: /Users/vital/Desktop/Prog/Python/Enc_dec/Enc_dec.py", line 180, in aes cipher = AES.new (key)
File "C: \ Users \ vital \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ site-packages \ Crypto \ Cipher \ AES.py", line 179, AESCipher (key, * args, * * kwargs)
File "C: \ Users \ vital \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ site-packages \ Crypto \ Cipher \ AES.py", line 114, in init blockalgo.BlockAlgo. init (self, _AES, key, * args, ** kwargs)
File "C: \ Users \ vital \ AppData \ Local \ Programs \ Python \ Python35-32 \ lib \ site-packages \ Crypto \ Cipher \ blockalgo.py", line 401, in init self._cipher = factory.new (key , * args, ** kwargs) ValueError: AES key must be either 16, 24, or 32 bytes long

Process finished with exit code 1

I considered that the key is 32 bytes, anyway.

  • What is your key there? - D-side
  • secret = os.urandom (BLOCK_SIZE) - Tukanoid 6:43 pm
  • Check that you have read from the file if there are any extra characters there. I tried to execute the program line by line (without reading / saving to a file) - everything works. - FeroxTL

0