I have a project that needs to encrypt text on the front end using Flutter and decrypt it on Python.
After I searched, I found the way to use AES CTR in this link same encryption between flutter and python [ AES ]
After I tried that it worked on Flutter, but it didn't work on Python because I failed to pip install pycrypto
, then I found another way in this https://cryptobook.nakov.com/symmetric-key-ciphers/aes-encrypt-decrypt-examples that i think that can work
After I tried, it failed too.
Here is my flutter code to encrypt:
final plainText = "abcde";//encoded;
final key = enc.Key.fromUtf8('12345678901234567890123456789012');
final iv = enc.IV.fromUtf8('12345');
final encrypter = enc.Encrypter(enc.AES(key, mode: enc.AESMode.ctr, padding: null));
final encrypted = encrypter.encrypt(plainText, iv: iv);
print(encrypted.base64); => result is yJpzWuw=
and here is my Python code:
print(text) => yJpzWuw=
plaintext = text.encode('utf-8')
key = '12345678901234567890123456789012'.encode("utf-8")
iv = '12345'.encode('utf-8')
iv_int = int.from_bytes(iv)
aes = pyaes.AESModeOfOperationCTR(key, pyaes.Counter(iv_int))
decrypted = aes.decrypt(plaintext)
print('Decrypted:', decrypted)
This is the result when I print the decrypted
Decrypted: b'\xe7:\xcb\x94\xbe\xbb\xf8\x1c'
Am I missing something? Or is there another way to encrypt in Flutter than decrypt in Python? I would be glad for any help.
==Solution from comment == Change the code in python with below code
ciphertext = base64.b64decode('yJpzWuw=') # Base64 decode the Base64 encoded ciphertext
key = '12345678901234567890123456789012'.encode("utf-8")
iv = '12345\0\0\0\0\0\0\0\0\0\0\0'.encode('utf-8') # Pad the IV to 16 bytes with 0x00 values
iv_int = int.from_bytes(iv, 'big') # Convert the binary data into an int (big endian order)
aes = pyaes.AESModeOfOperationCTR(key, pyaes.Counter(iv_int))
decrypted = aes.decrypt(ciphertext)
print('Decrypted:', decrypted)