Supercell.GUT/Supercell.GUT.Titan/Encryption/RC4Encrypter.cs
BreadDEV ad23f95319 [v0.0.1] very early state server
only basic messages, wip.
2024-03-04 20:19:32 +07:00

54 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])]);
}
}
}