decryptData function
Implementation
String? decryptData(String? encryptedData, String key) {
if (encryptedData == null || encryptedData.isEmpty) return null;
if (key.isEmpty) {
throw ArgumentError('Se requiere una clave (key) para desencriptar.');
}
final keyBytes = utf8.encode(key);
if (keyBytes.length != 16 && keyBytes.length != 32) {
throw ArgumentError(
'La clave debe tener 16 (AES-128) o 32 (AES-256) caracteres.');
}
try {
final encrKey = encrypt.Key(Uint8List.fromList(keyBytes));
final aes = encrypt.AES(
encrKey,
mode: encrypt.AESMode.ecb,
padding: 'PKCS7',
);
final encrypter = encrypt.Encrypter(aes);
final encrypted = encrypt.Encrypted.fromBase64(encryptedData);
final decrypted = encrypter.decrypt(encrypted, iv: null);
return decrypted;
} catch (e) {
throw Exception('Error al desencriptar los datos: $e');
}
}