RC4Engine.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  7. {
  8. public class RC4Engine
  9. : IStreamCipher
  10. {
  11. private readonly static int STATE_LENGTH = 256;
  12. /*
  13. * variables to hold the state of the RC4 engine
  14. * during encryption and decryption
  15. */
  16. private byte[] engineState;
  17. private int x;
  18. private int y;
  19. private byte[] workingKey;
  20. /**
  21. * initialise a RC4 cipher.
  22. *
  23. * @param forEncryption whether or not we are for encryption.
  24. * @param parameters the parameters required to set up the cipher.
  25. * @exception ArgumentException if the parameters argument is
  26. * inappropriate.
  27. */
  28. public virtual void Init(
  29. bool forEncryption,
  30. ICipherParameters parameters)
  31. {
  32. if (parameters is KeyParameter)
  33. {
  34. /*
  35. * RC4 encryption and decryption is completely
  36. * symmetrical, so the 'forEncryption' is
  37. * irrelevant.
  38. */
  39. workingKey = ((KeyParameter)parameters).GetKey();
  40. SetKey(workingKey);
  41. return;
  42. }
  43. throw new ArgumentException("invalid parameter passed to RC4 init - " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(parameters));
  44. }
  45. public virtual string AlgorithmName
  46. {
  47. get { return "RC4"; }
  48. }
  49. public virtual byte ReturnByte(
  50. byte input)
  51. {
  52. x = (x + 1) & 0xff;
  53. y = (engineState[x] + y) & 0xff;
  54. // swap
  55. byte tmp = engineState[x];
  56. engineState[x] = engineState[y];
  57. engineState[y] = tmp;
  58. // xor
  59. return (byte)(input ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
  60. }
  61. public virtual void ProcessBytes(
  62. byte[] input,
  63. int inOff,
  64. int length,
  65. byte[] output,
  66. int outOff)
  67. {
  68. Check.DataLength(input, inOff, length, "input buffer too short");
  69. Check.OutputLength(output, outOff, length, "output buffer too short");
  70. for (int i = 0; i < length ; i++)
  71. {
  72. x = (x + 1) & 0xff;
  73. y = (engineState[x] + y) & 0xff;
  74. // swap
  75. byte tmp = engineState[x];
  76. engineState[x] = engineState[y];
  77. engineState[y] = tmp;
  78. // xor
  79. output[i+outOff] = (byte)(input[i + inOff]
  80. ^ engineState[(engineState[x] + engineState[y]) & 0xff]);
  81. }
  82. }
  83. public virtual void Reset()
  84. {
  85. SetKey(workingKey);
  86. }
  87. // Private implementation
  88. private void SetKey(
  89. byte[] keyBytes)
  90. {
  91. workingKey = keyBytes;
  92. // System.out.println("the key length is ; "+ workingKey.Length);
  93. x = 0;
  94. y = 0;
  95. if (engineState == null)
  96. {
  97. engineState = new byte[STATE_LENGTH];
  98. }
  99. // reset the state of the engine
  100. for (int i=0; i < STATE_LENGTH; i++)
  101. {
  102. engineState[i] = (byte)i;
  103. }
  104. int i1 = 0;
  105. int i2 = 0;
  106. for (int i=0; i < STATE_LENGTH; i++)
  107. {
  108. i2 = ((keyBytes[i1] & 0xff) + engineState[i] + i2) & 0xff;
  109. // do the byte-swap inline
  110. byte tmp = engineState[i];
  111. engineState[i] = engineState[i2];
  112. engineState[i2] = tmp;
  113. i1 = (i1+1) % keyBytes.Length;
  114. }
  115. }
  116. }
  117. }
  118. #pragma warning restore
  119. #endif