ISO10126d2Padding.cs 1.9 KB

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