Advertisement
Guest User

rc4provider.cs

a guest
Jul 12th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. #region Fields
  2.         /// <summary>
  3.         /// Multi-user integer.
  4.         /// </summary>
  5.         private int i;
  6.         /// <summary>
  7.         /// Multi-use integer.
  8.         /// </summary>
  9.         private int j;
  10.  
  11.         private int[] key = new int[256];
  12.         private int[] table = new int[256];
  13.         #endregion
  14.  
  15.         #region Constructors
  16.         public rc4Provider(string publicKey)
  17.         {
  18.             // Reset work integers
  19.             this.i = 0;
  20.             this.j = 0;
  21.  
  22.             string decodedKey = this.decodeKey(publicKey); // Decode public key
  23.             this.Initialize(decodedKey); // Initialize work integers, tables etc
  24.             this.premixTable(this.premixString); // Premix tables
  25.         }
  26.         #endregion
  27.  
  28.         #region Methods
  29.         #region Initializing & premix
  30.         private void Initialize(string Key)
  31.         {
  32.             int keyValue = int.Parse(Key);
  33.             int keyLength = (keyValue & 0xf8) / 8;
  34.             if (keyLength < 20)
  35.                 keyLength += 20;
  36.             int keyOffset = keyValue % keyWindow.Length;
  37.             int tGiven = keyValue;
  38.             int tOwn = 0;
  39.  
  40.             int[] w = new int[keyLength];
  41.  
  42.             for (int a = 0; a < keyLength; a++)
  43.             {
  44.                 tOwn = keyWindow[Math.Abs((keyOffset + a) % keyWindow.Length)];
  45.                 w[a] = Math.Abs(tGiven ^ tOwn);
  46.                 if (a == 31)
  47.                     tGiven = keyValue;
  48.                 else
  49.                     tGiven = (tGiven / 2);
  50.             }
  51.  
  52.             for (int b = 0; b < 256; b++)
  53.             {
  54.                 key[b] = w[b % w.Length];
  55.                 table[b] = b;
  56.             }
  57.  
  58.             int t = 0;
  59.             int u = 0;
  60.             for (int a = 0; a < 256; a++)
  61.             {
  62.                 u = (int)((u + table[a] + key[a]) % 256);
  63.                 t = table[a];
  64.                 table[a] = table[u];
  65.                 table[u] = t;
  66.             }
  67.         }
  68.         private void premixTable(string s)
  69.         {
  70.             for (int a = 0; a < 17; a++)
  71.             {
  72.                 this.Encipher(s);
  73.             }
  74.         }
  75.         #endregion
  76.  
  77.         #region Ciphering
  78.         public string Encipher(string s)
  79.         {
  80.             StringBuilder Ret = new StringBuilder(s.Length * 2);
  81.  
  82.             int t = 0;
  83.             int k = 0;
  84.  
  85.             for (int a = 0; a < s.Length; a++)
  86.             {
  87.                 i = (i + 1) % 256;
  88.                 j = (j + table[i]) % 256;
  89.                 t = table[i];
  90.                 table[i] = table[j];
  91.                 table[j] = t;
  92.  
  93.                 k = table[(table[i] + table[j]) % 256];
  94.  
  95.                 int c = (char)s.Substring(a, 1).ToCharArray()[0] ^ k;
  96.  
  97.                 if (c <= 0)
  98.                     Ret.Append("00");
  99.                 else
  100.                 {
  101.                     Ret.Append(di[c >> 4 & 0xf]);
  102.                     Ret.Append(di[c & 0xf]);
  103.                 }
  104.  
  105.             }
  106.  
  107.             return Ret.ToString();
  108.         }
  109.         public string Decipher(string s)
  110.         {
  111.             try
  112.             {
  113.                 StringBuilder Ret = new StringBuilder(s.Length);
  114.                 int t = 0;
  115.                 int k = 0;
  116.                 for (int a = 0; a < s.Length; a += 2)
  117.                 {
  118.                     i = (i + 1) % 256;
  119.                     j = (j + table[i]) % 256;
  120.                     t = table[i];
  121.                     table[i] = table[j];
  122.                     table[j] = t;
  123.                     k = table[(table[i] + table[j]) % 256];
  124.                     t = System.Convert.ToInt32(this.JavaSubstring(s, a, a + 2), 16);
  125.                     Ret = Ret.Append((char)(t ^ k));
  126.                 }
  127.  
  128.                 return Ret.ToString();
  129.             }
  130.             catch { return ""; }
  131.         }
  132.         #endregion
  133.         #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement