aesDecrypt function

Uint8List aesDecrypt(
  1. dynamic cipherText,
  2. 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";
    }
  }

  final cryptoKeys.KeyPair keyPair = cryptoKeys.KeyPair.symmetric(
      cryptoKeys.SymmetricKey(keyValue: Uint8List.fromList(key)));
  final Uint8List iv = cipherText.sublist(0, 12);
  final Uint8List tag = cipherText.sublist(12, 12 + 16);
  final Uint8List encrypted = cipherText.sublist(28, cipherText.length);
  final cryptoKeys.Encrypter encrypter = keyPair.privateKey!
      .createEncrypter(cryptoKeys.algorithms.encryption.aes.gcm);
  final Uint8List decrypted = encrypter.decrypt(cryptoKeys.EncryptionResult(
      encrypted,
      initializationVector: iv,
      authenticationTag: tag));

  return decrypted;
}