djson method

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

Decrypts JSON data using AES and returns the decrypted bytes

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;
}