Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- namespace RC4_compact_
- {
- class RC4
- {
- static public byte[] EnCrypt(byte [] msg , byte[] key)
- {
- int j = 0;
- int i = 0;
- var S = (from n in Enumerable.Range(0, 256) select (byte)n).ToArray();
- var encrypted = new byte[msg.Length];
- for (i = 0; i < 256; i++)
- {
- j = (j + S[i] + key[i % key.Length]) % 256;
- Swap(ref S[i] ,ref S[j]);
- }
- i = j = 0;
- for (int r = 0; r < msg.Length; r++)
- {
- i = (i + 1) % 256;
- j = (j + S[i]) % 256;
- Swap(ref S[i], ref S[j]);
- encrypted[r] = (byte) (msg[r] ^ S[(S[i] + S[j]) % 256]);
- }
- return encrypted;
- }
- static public byte[] DeCrypt(byte[] msg, byte[] key) =>
- EnCrypt(msg, key);
- static void Swap(ref byte a, ref byte b)
- {
- a ^= b;b ^= a;a ^= b;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement