init method

  1. @override
void init(
  1. bool forEncryption,
  2. covariant KeyParameter params
)
override

Init the cipher with its initialization params. The type of CipherParameters depends on the algorithm being used (see the documentation of each implementation to find out more).

Use the argument forEncryption to tell the cipher if you want to encrypt or decrypt data.

Implementation

@override
void init(bool forEncryption, covariant KeyParameter params) {
  var key = params.key;

  var kc = (key.lengthInBytes / 4).floor(); // key length in words
  if (((kc != 4) && (kc != 6) && (kc != 8)) ||
      ((kc * 4) != key.lengthInBytes)) {
    throw ArgumentError('Key length must be 128/192/256 bits');
  }

  _forEncryption = forEncryption;
  _rounds = kc +
      6; // This is not always true for the generalized Rijndael that allows larger block sizes
  _workingKey = List.generate(
      _rounds + 1,
      (int i) =>
          List<int>.filled(4, 0, growable: false)); // 4 words in a block

  // Copy the key into the round key array.
  var keyView = ByteData.view(
      params.key.buffer, params.key.offsetInBytes, params.key.length);
  for (var i = 0, t = 0; i < key.lengthInBytes; i += 4, t++) {
    var value = unpack32(keyView, i, Endian.little);
    _workingKey![t >> 2][t & 3] = value;
  }

  // While not enough round key material calculated calculate values.
  var k = (_rounds + 1) << 2;
  for (var i = kc; i < k; i++) {
    var temp = _workingKey![(i - 1) >> 2][(i - 1) & 3].toInt();
    if ((i % kc) == 0) {
      temp = _subWord(_shift(temp, 8)) ^ _rcon[((i / kc) - 1).floor()];
    } else if ((kc > 6) && ((i % kc) == 4)) {
      temp = _subWord(temp);
    }

    var value = _workingKey![(i - kc) >> 2][(i - kc) & 3] ^ temp;
    _workingKey![i >> 2][i & 3] = value;
  }

  if (!forEncryption) {
    for (var j = 1; j < _rounds; j++) {
      for (var i = 0; i < 4; i++) {
        var value = _invMcol(_workingKey![j][i].toInt());
        _workingKey![j][i] = value;
      }
    }
  }
}