1

Closed

Wrong PKCS7 padding in DES algorithm

description

public class PKCS7Padding : CipherPadding
{
    /// <summary>
    /// Transforms the specified input.
    /// </summary>
    /// <param name="blockSize">Size of the block.</param>
    /// <param name="input">The input.</param>
    /// <returns>
    /// Padded data array.
    /// </returns>
    public override byte[] Pad(int blockSize, byte[] input)
    {
        var numOfPaddedBytes = blockSize - (input.Length % blockSize);

        var output = new byte[input.Length + numOfPaddedBytes];
        Buffer.BlockCopy(input, 0, output, 0, input.Length);
        for (int i = 0; i < numOfPaddedBytes; i++)
        {
            //Wrong
            //output[input.Length + i] = output[input.Length - 1];

            //Should be like this
            output[input.Length + i] = (byte)numOfPaddedBytes;
        }

        return output;
    }
}
Closed Apr 6 at 2:56 PM by drieseng

comments