85 lines
2.2 KiB
C#
85 lines
2.2 KiB
C#
using System.Text;
|
|
|
|
namespace BF
|
|
{
|
|
internal class NetRc4Encrypt : INetMessageProvider
|
|
{
|
|
private byte[] keyStream;
|
|
public string Key
|
|
{
|
|
set => keyStream = Encoding.UTF8.GetBytes(value);
|
|
}
|
|
|
|
private byte[] Encrypt(byte[] pwd, byte[] data) {
|
|
int a, i, j, k, tmp;
|
|
int[] key, box;
|
|
byte[] cipher;
|
|
|
|
key = new int[256];
|
|
box = new int[256];
|
|
cipher = new byte[data.Length];
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
key[i] = pwd[i % pwd.Length];
|
|
box[i] = i;
|
|
}
|
|
for (j = i = 0; i < 256; i++) {
|
|
j = (j + box[i] + key[i]) % 256;
|
|
tmp = box[i];
|
|
box[i] = box[j];
|
|
box[j] = tmp;
|
|
}
|
|
for (a = j = i = 0; i < data.Length; i++) {
|
|
a++;
|
|
a %= 256;
|
|
j += box[a];
|
|
j %= 256;
|
|
tmp = box[a];
|
|
box[a] = box[j];
|
|
box[j] = tmp;
|
|
k = box[((box[a] + box[j]) % 256)];
|
|
cipher[i] = (byte)(data[i] ^ k);
|
|
}
|
|
return cipher;
|
|
}
|
|
|
|
private byte[] Decrypt(byte[] pwd, byte[] data) {
|
|
return Encrypt(pwd, data);
|
|
}
|
|
|
|
public byte[] EncryptBytes(byte[] bytes)
|
|
{
|
|
return Encrypt(keyStream, bytes);
|
|
}
|
|
|
|
internal byte[]DecryptBytes(byte[] bytes)
|
|
{
|
|
return Decrypt(keyStream, bytes);
|
|
}
|
|
|
|
public byte[] EncryptStr(string data)
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes(data);
|
|
|
|
return Encrypt(keyStream, bytes);
|
|
}
|
|
|
|
public byte[] DecryptStr(string data)
|
|
{
|
|
byte[] bytes = Encoding.UTF8.GetBytes(data); //Convert.FromBase64String(Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(data)));
|
|
|
|
return Decrypt(keyStream, bytes);
|
|
}
|
|
|
|
public byte[] Pack(byte[] buffer, string key)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
public byte[] UnPack(byte[] buffer, string key)
|
|
{
|
|
throw new System.NotImplementedException();
|
|
}
|
|
|
|
}
|
|
} |