ISO10126d2Padding.cs 1.8 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 ISO10126-2 padding to a block.
  9. */
  10. public class ISO10126d2Padding: IBlockCipherPadding
  11. {
  12. private SecureRandom random;
  13. /**
  14. * Initialise the padder.
  15. *
  16. * @param random a SecureRandom if available.
  17. */
  18. public void Init(
  19. SecureRandom random)
  20. //throws ArgumentException
  21. {
  22. this.random = (random != null) ? random : new SecureRandom();
  23. }
  24. /**
  25. * Return the name of the algorithm the cipher implements.
  26. *
  27. * @return the name of the algorithm the cipher implements.
  28. */
  29. public string PaddingName
  30. {
  31. get { return "ISO10126-2"; }
  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. byte code = (byte)(input.Length - inOff);
  42. while (inOff < (input.Length - 1))
  43. {
  44. input[inOff] = (byte)random.NextInt();
  45. inOff++;
  46. }
  47. input[inOff] = code;
  48. return code;
  49. }
  50. /**
  51. * return the number of pad bytes present in the block.
  52. */
  53. public int PadCount(byte[] input)
  54. //throws InvalidCipherTextException
  55. {
  56. int count = input[input.Length - 1] & 0xff;
  57. if (count > input.Length)
  58. {
  59. throw new InvalidCipherTextException("pad block corrupted");
  60. }
  61. return count;
  62. }
  63. }
  64. }
  65. #endif