using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Chop.Game.Encryption
{
class RC4
{
private int _i = 0;
private int _j = 0;
private readonly int[] _table = new int[256];
///
/// Init RC4
///
/// Init RC4 using Byte
public void Init(byte[] a)
{
int k = a.Length;
this._i = 0;
while (this._i < 256)
{
this._table[this._i] = this._i;
this._i++;
}
this._j = 0;
this._i = 0;
while (this._i < 256)
{
this._j = ((this._j + this._table[this._i]) + (a[this._i % k] & 0xff)) % 256;
this.Swamp(this._i, this._j);
this._i++;
}
this._i = 0;
this._j = 0;
}
///
/// Encode and Decode
///
/// Encode/Decode Byte
public void Parse(byte[] b)
{
for (int a = 0; a < b.Length; a++)
{
this._i = (this._i + 1) % 256;
this._j = (this._j + this._table[this._i]) % 256;
this.Swamp(this._i, this._j);
b[a] = (byte)((b[a] & 0xff) ^ this._table[(this._table[this._i] + this._table[this._j]) % 256]);
}
}
///
/// Swamp
///
/// Int 1
/// Int 2
private void Swamp(int a1, int a2)
{
int k = this._table[a1];
this._table[a1] = this._table[a2];
this._table[a2] = k;
}
}
}