decrypt static method

String decrypt(
  1. String encryptedData, {
  2. String? key,
  3. int round = 10_000,
})

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);

    return utf8.decode(decryptedBytes);
  } catch (e) {
    return '';
  }
}