decrypt method

String decrypt(
  1. String encryptedData,
  2. String key
)

Decrypt data using AES-256-GCM

encryptedData is the encrypted data to decrypt key is the decryption key (must match the encryption key)

Returns the decrypted plain text data

Implementation

String decrypt(String encryptedData, String key) {
  try {
    final keyBytes = _deriveKey(key);
    final data = json.decode(utf8.decode(base64.decode(encryptedData)));

    final encrypted = base64.decode(data['data']);
    final iv = base64.decode(data['iv']);

    // For simplicity, we'll use a basic decryption approach
    // In production, consider using a proper AES implementation
    final decrypted = _simpleDecrypt(encrypted, keyBytes, iv);

    return utf8.decode(decrypted);
  } catch (e) {
    throw EncryptionException('Failed to decrypt data: $e');
  }
}