leocavalcante / encrypt

🔒 A set of high-level APIs over PointyCastle for two-way cryptography.
BSD 3-Clause "New" or "Revised" License
337 stars 134 forks source link

Invalid argument(s): Invalid or corrupted pad block #337

Open AhmedTawfiqM opened 3 weeks ago

AhmedTawfiqM commented 3 weeks ago
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;
  }
}
Mohamed1226 commented 3 weeks ago

the same issue is there any solution?

apackin commented 3 weeks ago

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

Mr-yuwei commented 1 week ago

same issue,need help

neoacevedo commented 1 week ago

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.