decrypt<Text, Key> method
- Text? ciphertext,
- Key? key,
- {CipherOptions? options}
override
The decrypt
method takes in cipher text, a key, and optionally an initialization vector (iv) and cipher options.
It returns a Uint8List
which contains the decrypted data.
The method is generic and can work with any type of data for the cipher text, key, and iv.
Implementation
@override
Uint8List decrypt<Text, Key>(Text? ciphertext, Key? key,
{CipherOptions? options}) {
areParamsVaild(ciphertext, key, options: options);
final Uint8List decrypted;
final mode = options?.mode ?? Mode.CBC;
final paddingused = options?.padding ?? pad.Padding.PKCS7;
final padding = getPadding(paddingused);
final Uint8List _key;
final Uint8List _iv;
Uint8List? saltbytes;
if (options?.salt != null) {
if (options!.salt is String) {
saltbytes =
getEncoder(options.saltEncoding ?? 'hex').parse(options.salt);
} else {
saltbytes = getSalt(options.salt, _SALT_SIZE);
}
}
if (key is String && options?.keyEncoding == null) {
var ctBytes = enc.Base64.parse(ciphertext as String);
final cipherTextBytes =
saltbytes == null ? ctBytes.sublist(_IV_SIZE) : ctBytes;
saltbytes ??= ctBytes.sublist(_SALT_SIZE, _IV_SIZE);
final keyAndIV = CryptoDart.EvpKDF(
password: enc.Utf8.parse(key),
keySize: _KEY_SIZE,
ivSize: _IV_SIZE,
hasher: _KDF_DIGEST,
salt: saltbytes,
iterations: 1,
);
_key = keyAndIV.key;
_iv = keyAndIV.iv;
decrypted = _runAes(
key: _key,
iv: _iv,
plaintext: cipherTextBytes,
mode: mode,
forEncryption: false,
padding: padding);
} else {
_iv = getIV(options?.iv, _IV_SIZE, options?.ivEncoding);
_key = getKey(key, options?.keyEncoding);
saltbytes = null;
decrypted = _runAes(
key: _key,
iv: _iv,
plaintext: getCipherText(ciphertext, options?.textEncoding),
mode: mode,
forEncryption: false,
padding: padding,
);
}
return decrypted;
}