decrypt method

  1. @override
BuBytes decrypt(
  1. List<int> src, [
  2. List<int>? dst
])

Decrypts src block-by-block in ECB mode, unpadding after if paddingStyle is set. Writes into dst if provided (dst must be exactly the plaintext length — a longer buffer would leave trailing bytes in the returned value).

Implementation

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