ISO7816d4Padding.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 the padding according to the scheme referenced in
  9. * ISO 7814-4 - scheme 2 from ISO 9797-1. The first byte is 0x80, rest is 0x00
  10. */
  11. public class ISO7816d4Padding
  12. : IBlockCipherPadding
  13. {
  14. /**
  15. * Initialise the padder.
  16. *
  17. * @param random - a SecureRandom if available.
  18. */
  19. public void Init(
  20. SecureRandom random)
  21. {
  22. // nothing to do.
  23. }
  24. /**
  25. * Return the name of the algorithm the padder implements.
  26. *
  27. * @return the name of the algorithm the padder implements.
  28. */
  29. public string PaddingName
  30. {
  31. get { return "ISO7816-4"; }
  32. }
  33. /**
  34. * add the pad bytes to the passed in block, returning the
  35. * number of bytes added.
  36. */
  37. public int AddPadding(
  38. byte[] input,
  39. int inOff)
  40. {
  41. int added = (input.Length - inOff);
  42. input[inOff]= (byte) 0x80;
  43. inOff ++;
  44. while (inOff < input.Length)
  45. {
  46. input[inOff] = (byte) 0;
  47. inOff++;
  48. }
  49. return added;
  50. }
  51. /**
  52. * return the number of pad bytes present in the block.
  53. */
  54. public int PadCount(
  55. byte[] input)
  56. {
  57. int count = input.Length - 1;
  58. while (count > 0 && input[count] == 0)
  59. {
  60. count--;
  61. }
  62. if (input[count] != (byte)0x80)
  63. {
  64. throw new InvalidCipherTextException("pad block corrupted");
  65. }
  66. return input.Length - count;
  67. }
  68. }
  69. }
  70. #endif