{"record":{"id":"b124dd9f43b98a78","repo":"TheAlgorithms/C-Sharp","slug":"input-length-must-be-a-multiple-of-block-size","errorCode":null,"errorMessage":"Input length must be a multiple of block size","messagePattern":"Input length must be a multiple of block size","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Pkcs7Padding.cs","lineNumber":87,"sourceCode":"        }\n\n        return code;\n    }\n\n    /// <summary>\n    /// Removes the PKCS7 padding from the given input data.\n    /// </summary>\n    /// <param name=\"input\">The input data with PKCS7 padding. Must not be null and must have a valid length and padding.</param>\n    /// <returns>The input data without the padding as a new byte array.</returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown if the input data is null, has an invalid length, or has an invalid padding.\n    /// </exception>\n    public byte[] RemovePadding(byte[] input)\n    {\n        // Check if input length is a multiple of blockSize\n        if (input.Length % blockSize != 0)\n        {\n            throw new ArgumentException(\"Input length must be a multiple of block size\");\n        }\n\n        // Get the padding length from the last byte of input\n        var paddingLength = input[^1];\n\n        // Check if padding length is valid\n        if (paddingLength < 1 || paddingLength > blockSize)\n        {\n            throw new ArgumentException(\"Invalid padding length\");\n        }\n\n        // Check if all padding bytes have the correct value\n        for (var i = 0; i < paddingLength; i++)\n        {\n            if (input[input.Length - 1 - i] != paddingLength)\n            {\n                throw new ArgumentException(\"Invalid padding\");\n            }","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Pkcs7Padding.cs#L69-L105","documentation":"RemovePadding requires the input length to be an exact multiple of blockSize, because PKCS#7 padding only exists at block boundaries and the last byte defines the padding length. A length that is not a multiple means the input is not a padded block sequence, so the method throws ArgumentException.","triggerScenarios":"Calling RemovePadding on a byte array whose Length % blockSize != 0 — e.g. decrypting raw (non-block-aligned) data, passing ciphertext instead of decrypted plaintext, or truncating the buffer before removing padding.","commonSituations":"Stream decryption that didn't accumulate a full final block; passing ciphertext that was never decrypted; a mismatch between the padding's blockSize and the cipher's actual block size; concatenating partially decrypted chunks incorrectly.","solutions":["Decrypt the data with the matching block cipher before calling RemovePadding.","Ensure the input length is a multiple of blockSize (e.g. 16 bytes for AES).","Verify the padding instance's blockSize equals the cipher's block size.","Check that stream/chunked decryption buffers all bytes before unpadding."],"exampleFix":"// before\npadding.RemovePadding(cipherBytes); // wrong input\n// after\nvar plain = aes.Decrypt(cipherBytes);\nvar unpadded = padding.RemovePadding(plain);","handlingStrategy":"validation","validationCode":"if (input is null || input.Length == 0 || input.Length % blockSize != 0)\n    throw new ArgumentException(\"Input must be a non-empty multiple of the block size before unpadding\");","typeGuard":"static bool IsBlockAligned(byte[] input, int blockSize) => input is not null && input.Length > 0 && input.Length % blockSize == 0;","tryCatchPattern":"try { plain = padding.RemovePadding(decrypted); }\ncatch (ArgumentException ex) { throw new CryptographicException(\"Data is not block-aligned — was it decrypted first?\", ex); }","preventionTips":["Always decrypt before removing padding.","Keep cipher block size and padding block size in sync.","Accumulate full blocks in streaming decryption before unpadding."],"tags":["csharp","cryptography","block-size","padding"],"backgroundTag":"invalid-argument-value","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"}