| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | namespace Renci.SshNet.Security.Cryptography.Ciphers.Paddings |
| | | 4 | | { |
| | | 5 | | /// <summary> |
| | | 6 | | /// Implements PKCS5 cipher padding. |
| | | 7 | | /// </summary> |
| | | 8 | | public class PKCS5Padding : CipherPadding |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Pads the specified input to match the block size. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="blockSize">The size of the block.</param> |
| | | 14 | | /// <param name="input">The input.</param> |
| | | 15 | | /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</par |
| | | 16 | | /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param> |
| | | 17 | | /// <returns> |
| | | 18 | | /// The padded data array. |
| | | 19 | | /// </returns> |
| | | 20 | | public override byte[] Pad(int blockSize, byte[] input, int offset, int length) |
| | 15 | 21 | | { |
| | 15 | 22 | | var numOfPaddedBytes = blockSize - (length % blockSize); |
| | 15 | 23 | | return Pad(input, offset, length, numOfPaddedBytes); |
| | 15 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Pads the specified input with a given number of bytes. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="input">The input.</param> |
| | | 30 | | /// <param name="offset">The zero-based offset in <paramref name="input"/> at which the data to pad starts.</par |
| | | 31 | | /// <param name="length">The number of bytes in <paramref name="input"/> to take into account.</param> |
| | | 32 | | /// <param name="paddinglength">The number of bytes to pad the input with.</param> |
| | | 33 | | /// <returns> |
| | | 34 | | /// The padded data array. |
| | | 35 | | /// </returns> |
| | | 36 | | public override byte[] Pad(byte[] input, int offset, int length, int paddinglength) |
| | 18 | 37 | | { |
| | 18 | 38 | | var output = new byte[length + paddinglength]; |
| | 18 | 39 | | Buffer.BlockCopy(input, offset, output, 0, length); |
| | | 40 | | |
| | 192 | 41 | | for (var i = 0; i < paddinglength; i++) |
| | 78 | 42 | | { |
| | 78 | 43 | | output[length + i] = (byte) paddinglength; |
| | 78 | 44 | | } |
| | | 45 | | |
| | 18 | 46 | | return output; |
| | 18 | 47 | | } |
| | | 48 | | } |
| | | 49 | | } |