RC4Engine.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace Best.HTTP.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 - " + 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. byte sx = engineState[x];
  75. byte sy = engineState[y];
  76. // swap
  77. engineState[x] = sy;
  78. engineState[y] = sx;
  79. // xor
  80. output[i+outOff] = (byte)(input[i + inOff] ^ engineState[(sx + sy) & 0xff]);
  81. }
  82. }
  83. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  84. public virtual void ProcessBytes(ReadOnlySpan<byte> input, Span<byte> output)
  85. {
  86. Check.OutputLength(output, input.Length, "output buffer too short");
  87. for (int i = 0; i < input.Length; i++)
  88. {
  89. x = (x + 1) & 0xff;
  90. y = (engineState[x] + y) & 0xff;
  91. byte sx = engineState[x];
  92. byte sy = engineState[y];
  93. // swap
  94. engineState[x] = sy;
  95. engineState[y] = sx;
  96. // xor
  97. output[i] = (byte)(input[i] ^ engineState[(sx + sy) & 0xff]);
  98. }
  99. }
  100. #endif
  101. public virtual void Reset()
  102. {
  103. SetKey(workingKey);
  104. }
  105. // Private implementation
  106. private void SetKey(
  107. byte[] keyBytes)
  108. {
  109. workingKey = keyBytes;
  110. // System.out.println("the key length is ; "+ workingKey.Length);
  111. x = 0;
  112. y = 0;
  113. if (engineState == null)
  114. {
  115. engineState = new byte[STATE_LENGTH];
  116. }
  117. // reset the state of the engine
  118. for (int i=0; i < STATE_LENGTH; i++)
  119. {
  120. engineState[i] = (byte)i;
  121. }
  122. int i1 = 0;
  123. int i2 = 0;
  124. for (int i=0; i < STATE_LENGTH; i++)
  125. {
  126. i2 = ((keyBytes[i1] & 0xff) + engineState[i] + i2) & 0xff;
  127. // do the byte-swap inline
  128. byte tmp = engineState[i];
  129. engineState[i] = engineState[i2];
  130. engineState[i2] = tmp;
  131. i1 = (i1+1) % keyBytes.Length;
  132. }
  133. }
  134. }
  135. }
  136. #pragma warning restore
  137. #endif