encrypt icon indicating copy to clipboard operation
encrypt copied to clipboard

Invalid argument(s): Invalid or corrupted pad block

Open AhmedTawfiqM opened this issue 1 year ago • 15 comments

  • I can't decrypt encrypted Uint8List by below decrypt function the strange thing is it works fine with some data and not working with others my app is in production and users is crying now :( :) , any solution please ?
class EncryptDataBase {
  static final key =
      encryptLib.Key.fromUtf8('SOMEKEY');
  static final iv = encryptLib.IV.allZerosOfLength(16);
  static final Uint8List encryptionMarker =
      Uint8List.fromList('EN:'.codeUnits);

  static Uint8List encrypt(Uint8List data) {
    final encrypter = encryptLib.Encrypter(encryptLib.AES(key));
    final encrypted = encrypter.encryptBytes(data, iv: iv);
    final markerAndData =
        Uint8List(encryptionMarker.length + encrypted.bytes.length);
    markerAndData.setRange(0, encryptionMarker.length, encryptionMarker);
    markerAndData.setRange(
      encryptionMarker.length,
      markerAndData.length,
      encrypted.bytes,
    );
    return markerAndData;
  }

  static Uint8List decrypt(Uint8List data) {
    if (data.length >= encryptionMarker.length &&
        data.sublist(0, encryptionMarker.length).toString() ==
            encryptionMarker.toString()) {
      final encryptedData = data.sublist(encryptionMarker.length);
      final encrypter = encryptLib.Encrypter(encryptLib.AES(key));
      return Uint8List.fromList(
          encrypter.decryptBytes(encryptLib.Encrypted(encryptedData), iv: iv));
    }
    // If no marker, return the data as it is
    return data;
  }
}

AhmedTawfiqM avatar Jun 08 '24 02:06 AhmedTawfiqM

the same issue is there any solution?

Mohamed1226 avatar Jun 08 '24 02:06 Mohamed1226

We had the same issue. Downgrading to version 5.0.1 worked for us. Looks like there is some kind of breaking change in 5.0.2

apackin avatar Jun 11 '24 00:06 apackin

same issue,need help

Mr-yuwei avatar Jun 24 '24 07:06 Mr-yuwei

We had the same issue. Downgrading to version 5.0.1 worked for us. Looks like there is some kind of breaking change in 5.0.2

I agree. I had the 5.0.3, with some code to decrypt I was receiving the same issue, but in addition to it, with the sample code in the repo but with a tiny modification to decrypt from plain text, not from an Encrypter object, it simply didn't decrypt, instead it left the same crypted value.

neoacevedo avatar Jun 24 '24 17:06 neoacevedo

Same issue, downgrading to 5.0.1 solved it.

plazareff avatar Jul 10 '24 20:07 plazareff

Same issue at 5.0.3

Replaced IV.fromLength(16); with IV.allZerosOfLength(16);

worked for me.

viniciusgiacomelli avatar Jul 11 '24 11:07 viniciusgiacomelli

Downgrading to 5.0.1 in my new app did not work for me. Now I've left with lots of encrypted data only my old app (also using 5.0.1) can "understand".

iamwilph avatar Jul 12 '24 10:07 iamwilph

Same problem here

reesaam avatar Jul 20 '24 08:07 reesaam

fixed mine by using the below dependencies:

dependencies: encrypt: 5.0.1

dependendependency_overrides: pointycastle: 3.5.2

iamwilph avatar Jul 20 '24 08:07 iamwilph

Same issue at 5.0.3

Replaced IV.fromLength(16); with IV.allZerosOfLength(16);

worked for me.

Solved by using this solution, but need first uninstall existing app

lukaskris avatar Aug 13 '24 02:08 lukaskris

The default encryption method is CBC, which requires an initial vector (IV) during encryption. The decryption will fail if provided IV is not the same as the one used in encryption.

It seems that IV.fromLength() creates a random IV, causing the decryption failure.

For me, saving the IV during encryption (same as the key itself), and load the same IV (through IV.fromBase64()) before the decryption works.

Exception0x0194 avatar Aug 23 '24 06:08 Exception0x0194

same issue anyone found any solution ??

I have encrypted data with 5.0.3 and now its not getting decrypted.

Any solution to get encrypted data back

lakhanrathi1 avatar Oct 26 '24 15:10 lakhanrathi1

any solution yet?

jonhcbs avatar Jan 13 '25 07:01 jonhcbs

Solution its working for me

dependency_overrides: pointycastle: 3.2.0 encrypt: 5.0.1

brenomenosso avatar Feb 12 '25 12:02 brenomenosso

Above solution does solve the problem but Is it safe to push new release with this version?

kartikeya-7 avatar Mar 25 '25 08:03 kartikeya-7