setKey method

AES setKey(
  1. List<int> key, [
  2. bool noDecryption = false
])

Initializes the AES cipher with the provided encryption key.

Parameters:

  • key: The encryption key. It must be 16, 24, or 32 bytes in length for AES-128, AES-192, or AES-256, respectively.
  • noDecryption: An optional boolean flag. If set to true, it disables decryption functionality by securely wiping the decryption key schedule.

Throws:

  • ArgumentException if the provided key size is invalid or if the instance was previously initialized with a different key size.

Implementation

AES setKey(List<int> key, [bool noDecryption = false]) {
  if (key.length != 16 && key.length != 24 && key.length != 32) {
    throw ArgumentException.invalidOperationArguments(
      "setKey",
      reason: "Invalid key bytes length.",
    );
  }
  if (_keyLen != key.length) {
    throw ArgumentException.invalidOperationArguments(
      "setKey",
      reason: "aes Initialized with different key size.",
    );
  }

  _encKey ??= List<int>.filled(key.length + 28, 0, growable: false);
  if (noDecryption) {
    if (_decKey != null) {
      BinaryOps.zero(_decKey!);
      _decKey = null;
    }
  } else {
    _decKey ??= List<int>.filled(key.length + 28, 0, growable: false);
  }
  _lib.expandKey(key, _encKey!, _decKey);
  return this;
}