I am trying to encrypt image by doing this :
Uint8List generateRandomKey() {
final random = Random.secure();
final key = List<int>.generate(32, (_) => random.nextInt(256));
return Uint8List.fromList(key);
}
void encryptFileOk(File inputFile) {
Uint8List enkripppKey = generateRandomKey();
final cipher = pc.BlockCipher('AES')
..init(true, pc.KeyParameter(enkripppKey));
final inputBytes = inputFile.readAsBytesSync();
print("halo1 data awal $inputBytes");
final encryptedBytes = cipher.process(Uint8List.fromList(inputBytes));
// outputFile.writeAsBytesSync(encryptedBytes);
print("halo1 encryptedBytes $encryptedBytes");
decryptFile(encryptedBytes, enkripppKey);
}
and for decrypt :
void decryptFile(Uint8List dummm, Uint8List enkripppKey) {
final cipher = pc.BlockCipher('AES')
..init(false, pc.KeyParameter(enkripppKey));
final encryptedBytes = dummm; //encryptedFile.readAsBytesSync();
final decryptedBytes = cipher.process(Uint8List.fromList(encryptedBytes));
print("halo1 decryptedBytes $decryptedBytes");
// decryptedFile.writeAsBytesSync(decryptedBytes);
}
I follow the step from this https://medium.com/@buddi/how-to-encrypt-a-file-in-flutter-4605e2fb8aec
but when I compare between print(“halo1 data awal $inputBytes”); and print(“halo1 decryptedBytes $decryptedBytes”);, the result is always different. I think it should be the same for inputBytes
and decryptedBytes
. And also the data in inputBytes
has a long list of Uint8list but after doing encrypt, the list of Uint8list become shorter. Is there any documentation or information that I can read for doing encrypt and decrypt in flutter