encryptBlock method

  1. @override
List<int> encryptBlock(
  1. List<int> src, [
  2. List<int>? dst,
  3. PaddingAlgorithm? paddingStyle = PaddingAlgorithm.pkcs7
])
override

Encrypts a single data block using the Electronic Codebook (ECB) mode.

This method encrypts a single data block using the AES block cipher in Electronic Codebook (ECB) mode. The block size is determined by the AES algorithm. Optionally, a padding style can be applied to the input data before encryption.

Parameters:

  • src: The data block to be encrypted.
  • dst: (Optional) The destination for the encrypted block. If not provided, a new List<int> is created.
  • paddingStyle: (Optional) The padding style to be applied before encryption (default is PKCS#7).

Returns:

  • The encrypted data block.

Throws:

  • ArgumentException if the source data size is not a multiple of the block size or if the destination size does not match the source size.
  • Exceptions related to padding, if padding is applied.

Note: This method encrypts a single block of data using AES in ECB mode. If padding is applied, it ensures the source data is appropriately padded before encryption.

Implementation

@override
List<int> encryptBlock(List<int> src,
    [List<int>? dst,
    PaddingAlgorithm? paddingStyle = PaddingAlgorithm.pkcs7]) {
  if (paddingStyle == null) {
    if ((src.length % blockSize) != 0) {
      throw ArgumentException("src size must be a multiple of $blockSize");
    }
  }
  List<int> input = List<int>.from(src);
  if (paddingStyle != null) {
    input = BlockCipherPadding.pad(input, blockSize, style: paddingStyle);
  }

  final out = dst ?? List<int>.filled(input.length, 0);
  if (out.length != input.length) {
    throw const ArgumentException(
        "The destination size does not match with source size");
  }
  final numBlocks = input.length ~/ blockSize;
  for (var i = 0; i < numBlocks; i++) {
    final start = i * blockSize;
    final end = (i + 1) * blockSize;
    final List<int> block = List<int>.from(input.sublist(start, end));
    final enc = super.encryptBlock(block);
    out.setRange(start, end, enc);
  }
  return out;
}