Advertisement
Razali

RC4 Encrypt/Decrypt

Aug 13th, 2015
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1.  public byte[] RC4EncryptDecrypt(byte[] bytes, string Key)
  2.  {
  3.             byte[] key = System.Text.Encoding.ASCII.GetBytes(Key);
  4.             byte[] s = new byte[256];
  5.             byte[] k = new byte[256];
  6.             byte temp;
  7.             int i, j;
  8.  
  9.             for (i = 0; i < 256; i++)
  10.             {
  11.                 s[i] = (byte)i;
  12.                 k[i] = key[i % key.GetLength(0)];
  13.             }
  14.  
  15.             j = 0;
  16.             for (i = 0; i < 256; i++)
  17.             {
  18.                 j = (j + s[i] + k[i]) % 256;
  19.                 temp = s[i];
  20.                 s[i] = s[j];
  21.                 s[j] = temp;
  22.             }
  23.  
  24.             i = j = 0;
  25.             for (int x = 0; x < bytes.GetLength(0); x++)
  26.             {
  27.                 i = (i + 1) % 256;
  28.                 j = (j + s[i]) % 256;
  29.                 temp = s[i];
  30.                 s[i] = s[j];
  31.                 s[j] = temp;
  32.                 int t = (s[i] + s[j]) % 256;
  33.                 bytes[x] ^= s[t];
  34.             }
  35.             return bytes;
  36.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement