{"record":{"id":"6558fcd794bb1db9","repo":"TheAlgorithms/C-Sharp","slug":"not-enough-space-in-input-array-for-padding-pkcs7padding","errorCode":null,"errorMessage":"Not enough space in input array for padding","messagePattern":"Not enough space in input array for padding","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Algorithms/Crypto/Paddings/Pkcs7Padding.cs","lineNumber":62,"sourceCode":"    /// <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.\n    /// </remarks>\n    public int AddPadding(byte[] input, int inputOffset)\n    {\n        // Calculate how many bytes need to be added to reach the next multiple of block size.\n        var code = (byte)((blockSize - (input.Length % blockSize)) % blockSize);\n\n        // If no padding is needed, add a full block of padding.\n        if (code == 0)\n        {\n            code = (byte)blockSize;\n        }\n\n        if (inputOffset + code > input.Length)\n        {\n            throw new ArgumentException(\"Not enough space in input array for padding\");\n        }\n\n        // Add the padding\n        for (var i = 0; i < code; i++)\n        {\n            input[inputOffset + i] = code;\n        }\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.","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Pkcs7Padding.cs#L44-L80","documentation":"AddPadding writes `code` padding bytes (equal to blockSize, or the remainder) starting at inputOffset within the caller-supplied array. If the array does not have room for those bytes beyond inputOffset, the method throws ArgumentException instead of silently truncating or overflowing the buffer.","triggerScenarios":"Calling AddPadding(input, inputOffset, inputLength) where inputOffset + paddingBytes > input.Length — e.g. an array sized exactly to the data with no padding room, or a non-zero inputOffset on a fully-sized array.","commonSituations":"Encrypting in-place without allocating an array with extra space for a full block of padding; an off-by-one when the caller sized the array; reusing a source buffer that was sized for unpadded data.","solutions":["Allocate the input array with room for at least one extra block: inputLength + blockSize.","Recheck that inputOffset points within the array and leaves room for padding.","Pad into a larger copy of the data before encryption.","Confirm the array length you pass matches the padded length the block cipher expects (multiple of blockSize)."],"exampleFix":"// before\nvar buf = new byte[data.Length]; // no padding room\npadding.AddPadding(buf, 0, data.Length);\n// after\nvar buf = new byte[data.Length + blockSize];\nArray.Copy(data, buf, data.Length);\npadding.AddPadding(buf, 0, data.Length);","handlingStrategy":"validation","validationCode":"var paddedLen = dataLength + blockSize;\nif (buffer is null || buffer.Length < paddedLen || inputOffset + paddedLen > buffer.Length)\n    throw new ArgumentException(\"Buffer too small for data plus one block of PKCS7 padding\");","typeGuard":"static bool HasPaddingRoom(byte[] buf, int offset, int len, int blockSize) => buf is not null && offset >= 0 && len >= 0 && offset + len + blockSize <= buf.Length;","tryCatchPattern":"try { padding.AddPadding(buf, offset, len); }\ncatch (ArgumentException) { buf = new byte[len + blockSize]; Array.Copy(data, buf, len); padding.AddPadding(buf, 0, len); }","preventionTips":["Always allocate buffers with one extra blockSize of headroom.","Prefer the library's padding-then-copy flow over in-place padding on exact-sized arrays.","Unit-test AddPadding with offset > 0 cases."],"tags":["csharp","cryptography","buffer-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"}