{"record":{"id":"9a9950781b416a6c","repo":"TheAlgorithms/C-Sharp","slug":"not-enough-space-in-input-array-for-padding","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/Iso10126D2Padding.cs","lineNumber":38,"sourceCode":"{\n    /// <summary>\n    /// Adds random padding to the input data array to make it a multiple of the block size according to the\n    /// ISO10126d2 standard.\n    /// </summary>\n    /// <param name=\"inputData\">The input data array that needs to be padded.</param>\n    /// <param name=\"inputOffset\">The offset in the input data array where the padding should start.</param>\n    /// <returns>The number of bytes added as padding.</returns>\n    /// <exception cref=\"ArgumentException\">\n    /// Thrown when there is not enough space in the input array for padding.\n    /// </exception>\n    public int AddPadding(byte[] inputData, int inputOffset)\n    {\n        // Calculate how many bytes need to be added to reach the next multiple of block size.\n        var code = (byte)(inputData.Length - inputOffset);\n\n        if (code == 0 || inputOffset + code > inputData.Length)\n        {\n            throw new ArgumentException(\"Not enough space in input array for padding\");\n        }\n\n        // Add the padding.\n        while (inputOffset < (inputData.Length - 1))\n        {\n            inputData[inputOffset] = (byte)RandomNumberGenerator.GetInt32(255);\n            inputOffset++;\n        }\n\n        // Set the last byte of the array to the size of the padding added.\n        inputData[inputOffset] = code;\n\n        return code;\n    }\n\n    /// <summary>\n    /// Removes the padding from the input data array and returns the original data.\n    /// </summary>","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso10126D2Padding.cs#L20-L56","documentation":"AddPadding for ISO 10126 computes the pad length as the distance from inputOffset to the end of the array and throws ArgumentException when there is no room (code == 0) or when offset+code exceeds the array. The caller must supply a buffer that already has space reserved for the padding bytes.","triggerScenarios":"Calling AddPadding on an array whose length equals inputOffset (fully full block), or a jagged/corrupted buffer where inputOffset + code exceeds inputData.Length (only reachable via integer overflow of the offset).","commonSituations":"Encrypting data that is an exact multiple of the block size without allocating an extra block for padding; off-by-one offset calculations when buffering partial blocks.","solutions":["Allocate the input array with at least one extra byte/block of space beyond the data (e.g. length rounded up to next multiple of block size plus one)","Ensure inputOffset is a valid non-negative index strictly less than the array length","Check before calling: if (inputData.Length - inputOffset < 1) grow the array first"],"exampleFix":"// before\nbyte[] buf = new byte[data.Length]; // no room for padding when full\n// after\nint paddedLen = ((data.Length / blockSize) + 1) * blockSize;\nbyte[] buf = new byte[Math.Max(paddedLen, data.Length + 1)];\nArray.Copy(data, buf, data.Length);\npadding.AddPadding(buf, data.Length);","handlingStrategy":"validation","validationCode":"if (inputData == null || inputOffset < 0 || inputOffset >= inputData.Length)\n    throw new ArgumentException(\"Need at least one free byte for ISO 10126 padding\");","typeGuard":null,"tryCatchPattern":"try { padding.AddPadding(buf, offset); }\ncatch (ArgumentException) { /* resize buf to next block multiple + 1 and retry */ }","preventionTips":["Always size buffers to the next block boundary plus one byte","Compute padded length with ((len / block) + 1) * block","Unit-test the exact-multiple-of-block-size edge case"],"tags":["csharp","crypto","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"}