Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. /// <summary>
  2. /// RC4 encryption algorithm
  3. /// </summary>
  4. /// <param name="bytes">Input bytes</param>
  5. /// <param name="key">Key bytes</param>
  6. /// <returns>encrypted bytes</returns>
  7. public static byte[] Encrypt(byte[] bytes, byte[] key)
  8. {
  9. byte[] z = new byte[bytes.Length];
  10. byte[] s = new byte[256];
  11. byte[] k = new byte[256];
  12. byte temp;
  13. int i, j;
  14.  
  15. for (i = 0; i < 256; i++)
  16. {
  17. s[i] = (byte)i;
  18. k[i] = key[i % key.GetLength(0)];
  19. }
  20.  
  21. j = 0;
  22. for (i = 0; i < 256; i++)
  23. {
  24. j = (j + s[i] + k[i]) % 256;
  25. temp = s[i];
  26. s[i] = s[j];
  27. s[j] = temp;
  28. }
  29.  
  30. i = j = 0;
  31. for (int x = 0; x < z.GetLength(0); x++)
  32. {
  33. i = (i + 1) % 256;
  34. j = (j + s[i]) % 256;
  35. temp = s[i];
  36. s[i] = s[j];
  37. s[j] = temp;
  38. int t = (s[i] + s[j]) % 256;
  39. z[x] = (byte)(bytes[x] ^ s[t]);
  40. }
  41. return z;
  42. }
  43.  
  44. public static byte[] Encrypt(byte[] bytes, byte[] key)
  45.  
  46. byte[] z = new byte[bytes.Length];
  47.  
  48. byte[] s = new byte[256];
  49. byte[] k = new byte[256];
  50. byte temp;
  51. int i, j;
  52.  
  53. for (i = 0; i < 256; i++)
  54. {
  55. ...
  56. k[i] = key[i % key.GetLength(0)];
  57. }
  58.  
  59. ...
  60. for (i = 0; i < 256; i++)
  61. {
  62. j = (j + s[i] + k[i]) % 256;
  63. ...
  64. }
  65.  
  66. j = (j + s[i] + k[i]) % 256;
  67.  
  68. i = (i + 1) % 256;
  69. j = (j + s[i]) % 256;
  70. ...
  71. int t = (s[i] + s[j]) % 256;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement