Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- Byte[] Key = new Byte[256];
- Byte[] Content = new Byte[256];
- string line = "";
- int stringDlzka = 0;
- int keyDlzka = 0;
- Console.Write(" Zadaj string:\n ");
- line = Console.ReadLine();
- int index = 0;
- // pre kazdy (ASCII hodnota) znak v zadanom stringu
- foreach (int c in line)
- {
- // max 256 znakov; znak je A - Z alebo a - z alebo medzera
- if (index < 256 && c >= 65 && c <= 90 || c >= 97 && c <= 122 || c == 32)
- {
- Content[index] = (Byte)c;
- index++;
- }
- }
- stringDlzka = index;
- Console.Write(" Zadaj kluc:\n ");
- line = Console.ReadLine();
- index = 0;
- // pre kazdy (ASCII hodnota) znak v zadanom stringu
- foreach (int c in line)
- {
- // max 256 znakov; znak je A - Z alebo a - z
- if (index < 256 && c >= 65 && c <= 90 || c >= 97 && c <= 122)
- {
- Key[index] = (Byte)c;
- index++;
- }
- }
- keyDlzka = index;
- // encrypt
- RC4(ref Content, Key, stringDlzka, keyDlzka);
- Console.Write(" Encrytped:\n ");
- for (int i = 0; i < stringDlzka; i++)
- Console.Write((char)Content[i]);
- Console.WriteLine();
- Console.ReadKey(true);
- // decrypt
- RC4(ref Content, Key, stringDlzka, keyDlzka);
- Console.Write(" Decrytped:\n ");
- for (int i = 0; i < stringDlzka; i++)
- Console.Write((char)Content[i]);
- Console.Write("\n ");
- Console.ReadKey(true);
- }
- public static void RC4(ref Byte[] bytes, Byte[] key, int stringDlzka, int keyDlzka)
- {
- Byte[] s = new Byte[256];
- Byte[] k = new Byte[256];
- Byte temp;
- int i, j;
- for (i = 0; i < 256; i++)
- {
- s[i] = (Byte)i;
- k[i] = key[i % keyDlzka];
- }
- j = 0;
- for (i = 0; i < 256; i++)
- {
- j = (j + s[i] + k[i]) % 256;
- temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- }
- i = j = 0;
- for (int x = 0; x < stringDlzka; x++)
- {
- i = (i + 1) % 256;
- j = (j + s[i]) % 256;
- temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- int t = (s[i] + s[j]) % 256;
- bytes[x] ^= s[t];
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment