Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

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

Description

@AhmedTawfiqM
  • 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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions