djson method

Future<Uint8List> djson(
  1. Uint8List encryptedData,
  2. String password
)

Decrypts encrypted JSON data using AES and returns the decrypted bytes.

The initialization vector (IV) is extracted from the first 16 bytes of the encryptedData. The remaining bytes are considered the encrypted content.

  • Parameters:

    • encryptedData: The data to be decrypted, which includes the IV.
    • password: The password used for decryption, which should match the password used during encryption.
  • Returns: The decrypted bytes of the JSON data.

Implementation

Future<Uint8List> djson(Uint8List encryptedData, String password) async {
  final key = _generateKey(password);
  final iv = encrypt.IV(Uint8List.fromList(encryptedData.sublist(0, 16)));
  final encryptedContent = encryptedData.sublist(16);
  final encrypter =
      encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
  final decryptedChunks = <Uint8List>[];
  for (int i = 0; i < encryptedContent.length; i += 4096) {
    final chunk = encryptedContent.sublist(
        i,
        i + 4096 > encryptedContent.length
            ? encryptedContent.length
            : i + 4096);
    final decryptedChunk =
        encrypter.decryptBytes(encrypt.Encrypted(chunk), iv: iv);
    decryptedChunks.add(Uint8List.fromList(decryptedChunk));
  }
  final result =
      Uint8List.fromList(decryptedChunks.expand((e) => e).toList());
  return result;
}