Last active
May 24, 2024 04:18
-
-
Save bitbeans/fb2bf80a47b1af31b07c to your computer and use it in GitHub Desktop.
Revisions
-
bitbeans revised this gist
Aug 16, 2015 . 1 changed file with 14 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,11 @@ /// <exception cref="ArgumentNullException"></exception> public static byte[] RemovePkcs7Padding(byte[] paddedByteArray) { if (paddedByteArray == null) { throw new ArgumentNullException("paddedByteArray", "paddedByteArray can not be null"); } var last = paddedByteArray[paddedByteArray.Length - 1]; if (paddedByteArray.Length <= last) { @@ -31,6 +36,15 @@ public static byte[] RemovePkcs7Padding(byte[] paddedByteArray) /// <exception cref="ArgumentOutOfRangeException"></exception> public static byte[] AddPkcs7Padding(byte[] data, int paddingLength) { if (data.Length > 256) { throw new ArgumentOutOfRangeException("data", "data must be <= 256 in length"); } if (paddingLength > 256) { throw new ArgumentOutOfRangeException("paddingLength", "paddingLength must be <= 256"); } if (paddingLength <= data.Length) { // there is nothing to pad -
bitbeans created this gist
Aug 16, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ /// <summary> /// Removes the Pkcs7 padding of an array. /// </summary> /// <param name="paddedByteArray">The padded array.</param> /// <returns>The unpadded array.</returns> /// <exception cref="OverflowException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentNullException"></exception> public static byte[] RemovePkcs7Padding(byte[] paddedByteArray) { var last = paddedByteArray[paddedByteArray.Length - 1]; if (paddedByteArray.Length <= last) { // there is nothing to unpad return paddedByteArray; } return SubArray(paddedByteArray, 0, (paddedByteArray.Length - last)); } /// <summary> /// Fill up an array with Pkcs7 padding. /// </summary> /// <param name="data">The source array.</param> /// <param name="paddingLength">The length of the padding.</param> /// <returns>The padded array.</returns> /// <exception cref="OverflowException"></exception> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public static byte[] AddPkcs7Padding(byte[] data, int paddingLength) { if (paddingLength <= data.Length) { // there is nothing to pad return data; } var output = new byte[paddingLength]; Buffer.BlockCopy(data, 0, output, 0, data.Length); for (var i = data.Length; i < output.Length; i++) { output[i] = (byte) (paddingLength - data.Length); } return output; } /// <summary> /// Extract a part of a byte array from another byte array. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="arr">A byte array.</param> /// <param name="start">Position to start extraction.</param> /// <param name="length">The length of the extraction started at start.</param> /// <returns>A part with the given length of the byte array.</returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentException"></exception> /// <exception cref="ArgumentOutOfRangeException"></exception> public static T[] SubArray<T>(T[] arr, int start, int length) { var result = new T[length]; Buffer.BlockCopy(arr, start, result, 0, length); return result; }