{"record":{"id":"f62af03c1e3f588b","repo":"TheAlgorithms/C-Sharp","slug":"not-enough-space-in-input-array-for-padding-iso7816d4padding","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/Iso7816D4Padding.cs","lineNumber":48,"sourceCode":"{\n    /// <summary>\n    /// Adds padding to the input data according to the ISO 7816-4 standard.\n    /// </summary>\n    /// <param name=\"inputData\">The input data array that needs padding.</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 or when the input offset is invalid.\n    /// </exception>\n    public int AddPadding(byte[] inputData, int inputOffset)\n    {\n        // Calculate the number of padding bytes based on the input data length and offset.\n        var code = (byte)(inputData.Length - inputOffset);\n\n        // Check if the padding bytes are valid and fit in the input array.\n        if (code == 0 || inputOffset + code > inputData.Length)\n        {\n            throw new ArgumentException(\"Not enough space in input array for padding\");\n        }\n\n        // Set the first padding byte to 80. This marks the start of padding in the ISO 7816-4 standard.\n        inputData[inputOffset] = 80;\n        inputOffset++;\n\n        // Set the remaining padding bytes to 0.\n        while (inputOffset < inputData.Length)\n        {\n            inputData[inputOffset] = 0;\n            inputOffset++;\n        }\n\n        // Return the number of padding bytes.\n        return code;\n    }\n\n    /// <summary>","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Crypto/Paddings/Iso7816D4Padding.cs#L30-L66","documentation":"ISO 7816-4 AddPadding computes the space between inputOffset and the array end and throws ArgumentException when there is no byte left to write the mandatory 0x80 marker (code == 0) or the bounds check fails. The caller must pre-size the buffer to hold data plus at least one padding byte.","triggerScenarios":"Calling AddPadding on a completely full buffer (inputOffset == inputData.Length), or with an offset that makes inputOffset + code exceed the array (offset overflow).","commonSituations":"Encrypting whole blocks of data without reserving an extra block for padding; miscounting buffer offsets when accumulating partial blocks in streaming code.","solutions":["Allocate the array with room for at least one extra byte beyond the data","Ensure 0 < inputData.Length - inputOffset before calling","If the buffer is exactly a multiple of the block size, add one extra block for padding"],"exampleFix":"// before\nvar buf = new byte[data.Length]; // full — no padding space\npadding.AddPadding(buf, data.Length);\n// after\nvar buf = new byte[data.Length + (blockSize - data.Length % blockSize)];\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 7816-4 padding\");","typeGuard":null,"tryCatchPattern":"try { padding.AddPadding(buf, offset); }\ncatch (ArgumentException) { /* grow buffer to next block multiple and retry */ }","preventionTips":["Reserve one extra byte/block for the 0x80 marker","Test with inputs exactly one block long","Keep offset arithmetic in one validated helper"],"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"}