Guest User

Untitled

a guest
Apr 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Byte[] Key = new Byte[256];
  13. Byte[] Content = new Byte[256];
  14. string line = "";
  15. int stringDlzka = 0;
  16. int keyDlzka = 0;
  17.  
  18. Console.Write(" Zadaj string:\n ");
  19. line = Console.ReadLine();
  20. int index = 0;
  21. // pre kazdy (ASCII hodnota) znak v zadanom stringu
  22. foreach (int c in line)
  23. {
  24. // max 256 znakov; znak je A - Z alebo a - z alebo medzera
  25. if (index < 256 && c >= 65 && c <= 90 || c >= 97 && c <= 122 || c == 32)
  26. {
  27. Content[index] = (Byte)c;
  28. index++;
  29. }
  30. }
  31. stringDlzka = index;
  32.  
  33. Console.Write(" Zadaj kluc:\n ");
  34. line = Console.ReadLine();
  35. index = 0;
  36. // pre kazdy (ASCII hodnota) znak v zadanom stringu
  37. foreach (int c in line)
  38. {
  39. // max 256 znakov; znak je A - Z alebo a - z
  40. if (index < 256 && c >= 65 && c <= 90 || c >= 97 && c <= 122)
  41. {
  42. Key[index] = (Byte)c;
  43. index++;
  44. }
  45. }
  46. keyDlzka = index;
  47.  
  48. // encrypt
  49. RC4(ref Content, Key, stringDlzka, keyDlzka);
  50. Console.Write(" Encrytped:\n ");
  51. for (int i = 0; i < stringDlzka; i++)
  52. Console.Write((char)Content[i]);
  53.  
  54. Console.WriteLine();
  55. Console.ReadKey(true);
  56.  
  57. // decrypt
  58. RC4(ref Content, Key, stringDlzka, keyDlzka);
  59. Console.Write(" Decrytped:\n ");
  60. for (int i = 0; i < stringDlzka; i++)
  61. Console.Write((char)Content[i]);
  62.  
  63. Console.Write("\n ");
  64. Console.ReadKey(true);
  65. }
  66.  
  67. public static void RC4(ref Byte[] bytes, Byte[] key, int stringDlzka, int keyDlzka)
  68. {
  69. Byte[] s = new Byte[256];
  70. Byte[] k = new Byte[256];
  71. Byte temp;
  72. int i, j;
  73.  
  74. for (i = 0; i < 256; i++)
  75. {
  76. s[i] = (Byte)i;
  77. k[i] = key[i % keyDlzka];
  78. }
  79.  
  80. j = 0;
  81. for (i = 0; i < 256; i++)
  82. {
  83. j = (j + s[i] + k[i]) % 256;
  84. temp = s[i];
  85. s[i] = s[j];
  86. s[j] = temp;
  87. }
  88.  
  89. i = j = 0;
  90. for (int x = 0; x < stringDlzka; x++)
  91. {
  92. i = (i + 1) % 256;
  93. j = (j + s[i]) % 256;
  94. temp = s[i];
  95. s[i] = s[j];
  96. s[j] = temp;
  97. int t = (s[i] + s[j]) % 256;
  98. bytes[x] ^= s[t];
  99. }
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment