Encryption in Java and Decryption in Flutter (AES-256) Encryption in Java and Decryption in Flutter (AES-256) flutter flutter

Encryption in Java and Decryption in Flutter (AES-256)


The Dart code lacks the separation of salt, IV and ciphertext. Add in decrypt():

var salt = ciphertextlist.sublist(0, 20);var iv = ciphertextlist.sublist(20, 20 + 16);var encrypted = ciphertextlist.sublist(20 + 16);

The salt must be passed to generateKey() and used there to derive the key, iv and ciphertext are applied as follows:

Uint8List key = generateKey("Hello", salt);...//int iv = cipher.blockSize;//Uint8List ivIntList = Uint8List.fromList(iv.toString().codeUnits);ParametersWithIV<KeyParameter> params = new ParametersWithIV<KeyParameter>(new KeyParameter(key), iv);...var val = paddingCipher.process(encrypted);...String decrypted = new String.fromCharCodes(val);print(decrypted);

generateKey() must take the passed salt into account, also digest block size and key size are not correct, so the following changes are necessary:

static Uint8List generateKey(String passphrase, Uint8List salt) {           // pass salt  Uint8List passphraseInt8List = Uint8List.fromList(passphrase.codeUnits);  //Uint8List salt = Uint8List.fromList('20'.codeUnits);  KeyDerivator derivator = PBKDF2KeyDerivator(HMac(SHA1Digest(), 64));      // 64 byte block size  Pbkdf2Parameters params = Pbkdf2Parameters(salt, 65556, 32);              // 32 byte key size  derivator.init(params);  return derivator.process(passphraseInt8List);}

With these changes, decryption works on my machine.