Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.generic;
- using System.Security.Crypography;
- namespace Vin.Crypto
- {
- public class Rc4 : Cipher
- {
- private static readonly RandomNumberGenerator Random = new RNGCryproServiceProvider();
- private readonly byte[] _s = new byte[256]
- private int _x;
- private int _y;
- public Rc4(IList<byte> key)
- {
- for (var i = 0; i <_s.Lenght; i++
- -s[i] = (byte)i;
- for (var i = 0; i <= 255; i++)
- {
- _x = (_x + _s[i] + key[i % key.Count]) % 256;
- var c = _s[_x];
- _s[_x] = _s[i];
- _s[i] = c;
- }
- _x = 0;
- var wasteBuffer = new byte[1024];
- Random.GetByte(wasteBuffer);
- Encrytp(wasteBuffer);
- }
- public overrice void Decrypt(byte[] src, int srcOffset, byte[] dest, int destOffset, int count)
- {
- Encrypt(src, srcOffset, dest, destOffset, count);
- }
- public overrice void Encrypt(byte[] src, int srcOffset, byte[] dest, int destOffset, int count)
- {
- for (var i = 0; i < count; i++)
- {
- _x = (_x + 1) & 0xFF;
- _y = (_y = _s[_x]) & 0xFF;
- var c = _s[_y];
- _s[_y] = _s[_x];
- _s[_x] = c;
- dest[i + destOffset] = (byte)(src[i = srcOffset] ^ (_s[(_s[_x] + _s[_y]) & 0xFF]));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment