{"record":{"id":"8d529b625375e538","repo":"TheAlgorithms/C-Sharp","slug":"invalid-block-size-blocksize","errorCode":null,"errorMessage":"Invalid block size: {blockSize}","messagePattern":"Invalid block size: (.+?)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Pkcs7Padding.cs","lineNumber":29,"sourceCode":"/// </para>\n/// <para>\n/// The padding can be easily removed after decryption by looking at the last byte and subtracting that many bytes from the\n/// end of the data.\n/// </para>\n/// <para>\n/// This class supports any block size from 1 to 255 bytes, and can be used with any encryption algorithm that requires\n/// padding, such as AES.\n/// </para>\n/// </summary>\npublic class Pkcs7Padding : IBlockCipherPadding\n{\n    private readonly int blockSize;\n\n    public Pkcs7Padding(int blockSize)\n    {\n        if (blockSize is < 1 or > 255)\n        {\n            throw new ArgumentOutOfRangeException(nameof(blockSize), $\"Invalid block size: {blockSize}\");\n        }\n\n        this.blockSize = blockSize;\n    }\n\n    /// <summary>\n    /// Adds padding to the end of a byte array according to the PKCS#7 standard.\n    /// </summary>\n    /// <param name=\"input\">The byte array to be padded.</param>\n    /// <param name=\"inputOffset\">The offset from which to start padding.</param>\n    /// <returns>The padding value that was added to each byte.</returns>\n    /// <exception cref=\"ArgumentException\">\n    /// If the input array does not have enough space to add <c>blockSize</c> bytes as padding.\n    /// </exception>\n    /// <remarks>\n    /// The padding value is equal to the number of of bytes that are added to the array.\n    /// For example, if the input array has a length of 16 and the input offset is 10,\n    /// then 6 bytes with the value 6 will be added to the end of the array.","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Pkcs7Padding.cs#L11-L47","documentation":"Pkcs7Padding's constructor validates that the block size is between 1 and 255, since PKCS#7 encodes the padding length as a single byte. Passing any other value makes valid PKCS#7 padding impossible, so the library throws ArgumentOutOfRangeException immediately at construction rather than failing later during padding operations.","triggerScenarios":"Calling new Pkcs7Padding(n) with n < 1 (e.g. 0 or negative) or n > 255 (e.g. 512, or a bit size like 128 mistaken for a byte size).","commonSituations":"Developers confusing block size in bits (AES 128-bit) with bytes and passing 128 in bits-based units while intending 16; copying initialization code and forgetting to set the size; reading a config value of 0 when the block size is unset.","solutions":["Pass a block size in bytes between 1 and 255 (16 for AES, 8 for DES/3DES).","If you have a bit-based size, divide by 8 before constructing.","Validate configuration values before constructing the padding.","If you need larger blocks than 255 bytes, use a different padding scheme or library."],"exampleFix":"// before\nvar padding = new Pkcs7Padding(128); // bits, throws\n// after\nvar padding = new Pkcs7Padding(128 / 8); // 16 bytes","handlingStrategy":"validation","validationCode":"if (blockSize is < 1 or > 255)\n    throw new ArgumentOutOfRangeException(nameof(blockSize), blockSize, \"Block size must be 1..255 bytes\");\nvar padding = new Pkcs7Padding(blockSize);","typeGuard":"static bool IsValidBlockSize(int size) => size is >= 1 and <= 255;","tryCatchPattern":"try { padding = new Pkcs7Padding(cfg.BlockSize); }\ncatch (ArgumentOutOfRangeException ex) { /* surface config error to operator */ throw new InvalidOperationException(\"Invalid configured block size\", ex); }","preventionTips":["Express block sizes in bytes, not bits (AES=16, DES=8).","Validate config values at startup before constructing crypto primitives.","Use a validated factory method that clamps/rejects bad sizes."],"tags":["csharp","cryptography","argument-out-of-range","padding"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}