decrypt static method
Decrypts encryptedData, a base64-encoded string created by encrypt,
using an optional key. Returns the decrypted utf8-decoded string,
or an empty string on failure.
Implementation
static String decrypt(String encryptedData, {String? key, int round = 10_000}) {
try {
final encryptionKey = key ?? _defaultKey;
final keyBytes = utf8.encode(encryptionKey);
final combined = base64Decode(encryptedData);
if (combined.length < 16) return '';
final iv = combined.sublist(0, 16);
final encryptedBytes = combined.sublist(16);
final derivedKey = _deriveKey(keyBytes, iv, round);
final decryptedBytes = _decryptBytes(encryptedBytes, derivedKey, iv);
final decoded = utf8.decode(decryptedBytes);
return decoded;
} on FormatException catch (e) {
throw Exception(
'Malformed encrypted data, May be the key is incorrect or the data is corrupted: $e',
);
} catch (e) {
throw Exception(
'Error decrypting data: $e, May be the key is incorrect or the data is corrupted',
);
}
}