mirror of
https://github.com/thebreaddev/Supercell.GUT.git
synced 2024-11-10 07:44:37 +00:00
55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
|
namespace Supercell.GUT.Titan.Encryption;
|
|||
|
|
|||
|
public class RC4Encrypter
|
|||
|
{
|
|||
|
private byte[]? m_key;
|
|||
|
private byte m_x;
|
|||
|
private byte m_y;
|
|||
|
|
|||
|
public RC4Encrypter(string baseKey, string nonce)
|
|||
|
{
|
|||
|
this.InitState(baseKey, nonce);
|
|||
|
}
|
|||
|
|
|||
|
public void InitState(string baseKey, string nonce)
|
|||
|
{
|
|||
|
string key = baseKey + nonce;
|
|||
|
|
|||
|
this.m_key = new byte[256];
|
|||
|
this.m_x = 0;
|
|||
|
this.m_y = 0;
|
|||
|
|
|||
|
for (int i = 0; i < 256; i++)
|
|||
|
{
|
|||
|
this.m_key[i] = (byte)i;
|
|||
|
}
|
|||
|
|
|||
|
for (int i = 0, j = 0; i < 256; i++)
|
|||
|
{
|
|||
|
j = (byte)(j + this.m_key[i] + key[i % key.Length]);
|
|||
|
|
|||
|
(this.m_key[j], this.m_key[i]) = (this.m_key[i], this.m_key[j]);
|
|||
|
}
|
|||
|
|
|||
|
for (int i = 0; i < key.Length; i++)
|
|||
|
{
|
|||
|
this.m_x += 1;
|
|||
|
this.m_y += this.m_key[this.m_x];
|
|||
|
|
|||
|
(this.m_key[this.m_x], this.m_key[this.m_y]) = (this.m_key[this.m_y], this.m_key[this.m_x]);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void Encrypt(byte[] input)
|
|||
|
{
|
|||
|
for (int i = 0; i < input.Length; i++)
|
|||
|
{
|
|||
|
this.m_x += 1;
|
|||
|
this.m_y += this.m_key![this.m_x];
|
|||
|
|
|||
|
(this.m_key[this.m_x], this.m_key[this.m_y]) = (this.m_key[this.m_y], this.m_key[this.m_x]);
|
|||
|
input[i] = (byte)(input[i] ^ this.m_key[(byte)(this.m_key[this.m_x] + this.m_key[this.m_y])]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|