Uint8List
aesDecrypt(- dynamic cipherText,
- dynamic key
)
Implementation
Uint8List aesDecrypt(cipherText, key) {
if (!(cipherText is Uint8List) && !(cipherText is String)) {
throw "'cipherText' must be a string or Uint8List";
}
if (!(key is Uint8List) && !(key is String)) {
throw "'key' must be a string or Uint8List";
}
if (cipherText is String) {
if (isHex(cipherText)) {
cipherText = hexToUint8List(cipherText);
} else {
throw "'cipherText' must be an hexadecimal string";
}
}
if (key is String) {
if (isHex(key)) {
key = hexToUint8List(key);
} else {
throw "'key' must be an hexadecimal string";
}
}
cryptoKeys.KeyPair keyPair = cryptoKeys.KeyPair.symmetric(
cryptoKeys.SymmetricKey(keyValue: Uint8List.fromList(key)));
Uint8List iv = cipherText.sublist(0, 12);
Uint8List tag = cipherText.sublist(12, 12 + 16);
Uint8List encrypted = cipherText.sublist(28, cipherText.length);
cryptoKeys.Encrypter encrypter = keyPair.privateKey!
.createEncrypter(cryptoKeys.algorithms.encryption.aes.gcm);
Uint8List decrypted = encrypter.decrypt(cryptoKeys.EncryptionResult(encrypted,
initializationVector: iv, authenticationTag: tag));
return decrypted;
}