RC4Engine.cs 3.8 KB

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