StreamBlockCipher.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto
  6. {
  7. /**
  8. * a wrapper for block ciphers with a single byte block size, so that they
  9. * can be treated like stream ciphers.
  10. */
  11. public class StreamBlockCipher
  12. : IStreamCipher
  13. {
  14. private readonly IBlockCipher cipher;
  15. private readonly byte[] oneByte = new byte[1];
  16. /**
  17. * basic constructor.
  18. *
  19. * @param cipher the block cipher to be wrapped.
  20. * @exception ArgumentException if the cipher has a block size other than
  21. * one.
  22. */
  23. public StreamBlockCipher(
  24. IBlockCipher cipher)
  25. {
  26. if (cipher == null)
  27. throw new ArgumentNullException("cipher");
  28. if (cipher.GetBlockSize() != 1)
  29. throw new ArgumentException("block cipher block size != 1.", "cipher");
  30. this.cipher = cipher;
  31. }
  32. /**
  33. * initialise the underlying cipher.
  34. *
  35. * @param forEncryption true if we are setting up for encryption, false otherwise.
  36. * @param param the necessary parameters for the underlying cipher to be initialised.
  37. */
  38. public void Init(
  39. bool forEncryption,
  40. ICipherParameters parameters)
  41. {
  42. cipher.Init(forEncryption, parameters);
  43. }
  44. /**
  45. * return the name of the algorithm we are wrapping.
  46. *
  47. * @return the name of the algorithm we are wrapping.
  48. */
  49. public string AlgorithmName
  50. {
  51. get { return cipher.AlgorithmName; }
  52. }
  53. /**
  54. * encrypt/decrypt a single byte returning the result.
  55. *
  56. * @param in the byte to be processed.
  57. * @return the result of processing the input byte.
  58. */
  59. public byte ReturnByte(
  60. byte input)
  61. {
  62. oneByte[0] = input;
  63. cipher.ProcessBlock(oneByte, 0, oneByte, 0);
  64. return oneByte[0];
  65. }
  66. /**
  67. * process a block of bytes from in putting the result into out.
  68. *
  69. * @param in the input byte array.
  70. * @param inOff the offset into the in array where the data to be processed starts.
  71. * @param len the number of bytes to be processed.
  72. * @param out the output buffer the processed bytes go into.
  73. * @param outOff the offset into the output byte array the processed data stars at.
  74. * @exception DataLengthException if the output buffer is too small.
  75. */
  76. public void ProcessBytes(
  77. byte[] input,
  78. int inOff,
  79. int length,
  80. byte[] output,
  81. int outOff)
  82. {
  83. if (outOff + length > output.Length)
  84. throw new DataLengthException("output buffer too small in ProcessBytes()");
  85. for (int i = 0; i != length; i++)
  86. {
  87. cipher.ProcessBlock(input, inOff + i, output, outOff + i);
  88. }
  89. }
  90. /**
  91. * reset the underlying cipher. This leaves it in the same state
  92. * it was at after the last init (if there was one).
  93. */
  94. public void Reset()
  95. {
  96. cipher.Reset();
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif