aesGcmDecrypt function

Uint8List? aesGcmDecrypt(
  1. Uint8List key,
  2. Uint8List input,
  3. Uint8List aad
)

AES-256-GCM decrypt for aesGcmEncrypt output. aad must match the bytes used at encryption time or decryption fails (returns null).

Implementation

Uint8List? aesGcmDecrypt(Uint8List key, Uint8List input, Uint8List aad) {
  if (key.length != _aesKeySize || input.length < _nonceSize + 16) {
    return null;
  }

  final nonce = input.sublist(0, _nonceSize);
  final encrypted = input.sublist(_nonceSize);
  final cipher = GCMBlockCipher(AESEngine())
    ..init(
      false,
      AEADParameters(KeyParameter(key), _macBits, nonce, aad),
    );

  try {
    return cipher.process(encrypted);
  } on InvalidCipherTextException {
    return null;
  } on StateError {
    return null;
  }
}