decryptWithPassword function
Decrypts the data using the provided password and salt.
This function assumes that the first 16 bytes of data are the IV,
and the rest is the AES-CBC encrypted payload.
If the password is empty, the data is assumed to be UTF-8 encoded JSON
and is decoded directly.
The decryption process is run in a separate Isolate.
Implementation
Future<dynamic> decryptWithPassword(
Uint8List data,
String password,
String salt,
) async {
return Isolate.run(() {
if (password.isEmpty) return jsonDecode(utf8.decode(data));
final iv = encrypt.IV(data.sublist(0, 16));
final cipherBytes = data.sublist(16);
final key = encrypt.Key(deriveKey(password, salt));
final encrypter = encrypt.Encrypter(
encrypt.AES(key, mode: encrypt.AESMode.cbc),
);
final decrypted = encrypter.decrypt(encrypt.Encrypted(cipherBytes), iv: iv);
return jsonDecode(decrypted);
});
}