fromSafe method

String fromSafe(
  1. String password
)

Decrypts a base64-encoded AES-encrypted string using a provided password.

This method uses AES decryption in CBC mode. The password is adjusted to be exactly 32 bytes long, with the first 16 bytes used as the initialization vector (IV).

Example:

var decrypted = encryptedText.fromSafe('your-password-here');
print(decrypted); // Outputs original decrypted string

password The password used for decryption. It is truncated or padded to 32 bytes.

Returns the decrypted string, or an error message if decryption fails.

Implementation

String fromSafe(String password) {
  try {
    if (password.length > 32) {
      password = password.substring(0, 32);
    } else if (password.length < 32) {
      while (password.length != 32) {
        password += '-';
      }
    }

    var key = Key.fromUtf8(password);

    password = password.substring(0, 16);
    final iv = IV.fromUtf8(password);

    final e = Encrypter(AES(key, mode: AESMode.cbc));
    final decryptedData = e.decrypt(Encrypted.fromBase64(this), iv: iv);
    return decryptedData;
  } catch (e) {
    return e.toString();
  }
}