Pkcs7Padding.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Crypto;
  4. using Org.BouncyCastle.Security;
  5. namespace Org.BouncyCastle.Crypto.Paddings
  6. {
  7. /**
  8. * A padder that adds Pkcs7/Pkcs5 padding to a block.
  9. */
  10. public class Pkcs7Padding
  11. : IBlockCipherPadding
  12. {
  13. /**
  14. * Initialise the padder.
  15. *
  16. * @param random - a SecureRandom if available.
  17. */
  18. public void Init(
  19. SecureRandom random)
  20. {
  21. // nothing to do.
  22. }
  23. /**
  24. * Return the name of the algorithm the cipher implements.
  25. *
  26. * @return the name of the algorithm the cipher implements.
  27. */
  28. public string PaddingName
  29. {
  30. get { return "PKCS7"; }
  31. }
  32. /**
  33. * add the pad bytes to the passed in block, returning the
  34. * number of bytes added.
  35. */
  36. public int AddPadding(
  37. byte[] input,
  38. int inOff)
  39. {
  40. byte code = (byte)(input.Length - inOff);
  41. while (inOff < input.Length)
  42. {
  43. input[inOff] = code;
  44. inOff++;
  45. }
  46. return code;
  47. }
  48. /**
  49. * return the number of pad bytes present in the block.
  50. */
  51. public int PadCount(
  52. byte[] input)
  53. {
  54. byte countAsByte = input[input.Length - 1];
  55. int count = countAsByte;
  56. if (count < 1 || count > input.Length)
  57. throw new InvalidCipherTextException("pad block corrupted");
  58. for (int i = 2; i <= count; i++)
  59. {
  60. if (input[input.Length - i] != countAsByte)
  61. throw new InvalidCipherTextException("pad block corrupted");
  62. }
  63. return count;
  64. }
  65. }
  66. }
  67. #endif