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.

Parameters:

  • src: The data block to be decrypted.
  • dst: (Optional) The destination for the decrypted block.
  • paddingStyle: (Optional) The padding style to be applied after decryption (default is PKCS#7).

Throws:

  • CryptoException 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.

Implementation

@override
List<int> decryptBlock(
  List<int> src, [
  List<int>? dst,
  PaddingAlgorithm? paddingStyle = PaddingAlgorithm.pkcs7,
]) {
  if ((src.length % blockSize) != 0) {
    throw ArgumentException.invalidOperationArguments(
      "decryptBlock",
      name: "src",
      reason: "Incorrect source length.",
    );
  }
  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 ArgumentException.invalidOperationArguments(
        "decryptBlock",
        name: "dst",
        reason: "Incorrect destination length.",
      );
    }
    dst.setAll(0, out);
    return dst;
  }
  return out;
}