0

I use this code to encrypt string. When I encrypt string which contains only english letters - it works fine, but when I try to encrypt cyrillic text it throws an exception.

import base64
from Crypto.Cipher import AES
def encrypt(text):
    BLOCK_SIZE = 16
    PADDING = '{'
    pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
    EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
    secret = "123456789101112A"
    iv = "AAAABBBBCCCCDDDD"
    cipher = AES.new(key=secret, mode=AES.MODE_CBC, IV=iv)
    return EncodeAES(cipher, text)

print(encrypt('text')) # OK
print(encrypt('абвг')) # Exception

Output

b'ZVigw9c3zTbZOrGJKJe5QQ=='
Traceback (most recent call last):
  File "/tmp/1.py", line 14, in <module>
    print(encrypt('абвг'))
  File "/tmp/1.py", line 11, in encrypt
    return EncodeAES(cipher, text)
  File "/tmp/1.py", line 7, in <lambda>
    EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
  File "/usr/lib/python3.6/site-packages/Crypto/Cipher/blockalgo.py", line 244, in encrypt
    return self._cipher.encrypt(plaintext)
ValueError: Input strings must be a multiple of 16 in length

I verified input strings using len function after padding. No matter what input string is, it's length is always multiple of 16. File is encoded in utf-8 and OS too, python 3.6.0, pycrypto==2.6.1

changer
  • 329
  • 2
  • 4
  • 19

0 Answers0