decryptBlock method

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

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

This method decrypts 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 output data after decryption.

Parameters:

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

Returns:

  • The decrypted data block.

Throws:

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

Note: This method decrypts a single block of data using AES in ECB mode. If padding is applied, it ensures the output data is appropriately unpadded after decryption.

Implementation

@override
List<int> decryptBlock(List<int> src,
    [List<int>? dst,
    PaddingAlgorithm? paddingStyle = PaddingAlgorithm.pkcs7]) {
  if ((src.length % blockSize) != 0) {
    throw ArgumentException("src size must be a multiple of $blockSize");
  }
  List<int> out = List<int>.filled(src.length, 0);

  final numBlocks = src.length ~/ blockSize;
  for (var i = 0; i < numBlocks; i++) {
    final start = i * blockSize;
    final end = (i + 1) * blockSize;
    final enc = super.decryptBlock(src.sublist(start, end));
    out.setRange(start, end, enc);
  }
  if (paddingStyle != null) {
    out = BlockCipherPadding.unpad(out, blockSize, style: paddingStyle);
  }
  if (dst != null) {
    if (dst.length < out.length) {
      throw const ArgumentException("Destination size is small");
    }
    dst.setAll(0, out);
    return dst;
  }
  return out;
}