Home > Net >  AES128 produces result longer than 16 bytes in Flutter Encrypt package
AES128 produces result longer than 16 bytes in Flutter Encrypt package

Time:11-01

I am working on a flutter project where we need to encrypt a plaintext that is given in hex with a key that is also given in hex. We're using a package called encrypt which only accepts plaintext as String and most of the online AES tools also assume the plaintext to be String. So we tried converting the hex to String but to no avail, we always get the result longer than 16 bytes. This only seems to be the result with our particular String (that has special characters). So other Strings the length of the ciphertext is as expected.

KEY = B0A42687AA50A67A6DCEB68EA59A1332 (16 Bytes)
Plaintext = A0A1A2A3A4A5A6A7B1B2B3B4B5B6B7B0 (16 Bytes)
Expected Ciphertext = D704CE5AAE286511A691AF8EF7D31FE1 (16 Bytes)

What we have tried so far and the results:

void main() {
  final plainText = 'A0A1A2A3A4A5A6A7B1B2B3B4B5B6B7B0' //Original Hex treated as String;
  final key = Key.fromBase16('0A42687AA50A67A6DCEB68EA59A1332');
  final iv = IV.fromLength(16);

  final encrypter = Encrypter(AES(key, mode: AESMode.ebc, padding: null));

  final encrypted = encrypter.encrypt(plainText, iv: iv);
  final decrypted = encrypter.decrypt(encrypted, iv: iv);

  print(encrypted.base16); // 477eobf5f216745a7b1daf703218861b2c26ba77843429adc8928a164CCCC435
}
void main() {
  final plainText = ' ¡¢£¤¥¦§±²³´µ¶·°' //Hex to equivalent String;
  final key = Key.fromBase16('0A42687AA50A67A6DCEB68EA59A1332');
  final iv = IV.fromLength(16);

  final encrypter = Encrypter(AES(key, mode: AESMode.ebc, padding: null));

  final encrypted = encrypter.encrypt(plainText, iv: iv);
  final decrypted = encrypter.decrypt(encrypted, iv: iv);

  print(encrypted.base16); // 268ceb0611e696ab9eeb4b87d51c60eedfd54a8755c5df5b3f657ee2b93936
}

CodePudding user response:

' ¡¢£¤¥¦§±²³´µ¶·°' is almost certainly not the equivalent of the hex A0A1A2A3A4A5A6A7B1B2B3B4B5B6B7B0. That would require this source code to be interpreted as Latin-1, which I can't believe Flutter does. (It also means you need to have been extremely careful to make sure the first character is "Non-breaking space," rather than a normal space. The text you posted here begins with normal space (0x20), not 0xa0.

I strongly expect that Flutter interprets this string as UTF-8, which means it is 20c2a1c2a2c2a3c2a4c2a5c2a6c2a7c2b1c2b2c2b3c2b4c2b5c2b6c2b7c2b0 (31 bytes). It is not possible to convert your plaintext hex into an equivalent UTF-8 string, because it's not valid UTF-8.

You will need to consult the documentation or source code of your package to figure out how to pass arbitrary bytes to the encryptor. If it cannot handle that, you will need to fix it or replace it. Without knowing the specific package you're using, with a link to the documentation or its source code, I don't believe we can guide you.

Assuming you mean this encrypt, then the method you want is encryptBytes.

  • Related