Реализация криптоалгоритма RC4 для .NET:
using System;
using System.Security.Cryptography;
using Utils;
namespace Crypto {
internal class RC4Cryptor : ICryptoTransform {
internal byte[] S;
public bool CanReuseTransform { get { throw new NotImplementedException(); } }
public bool CanTransformMultipleBlocks { get { throw new NotImplementedException(); } }
public int InputBlockSize { get { throw new NotImplementedException(); } }
public int OutputBlockSize { get { throw new NotImplementedException(); } }
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) {
throw new NotImplementedException();
}
public void Dispose() {
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount) {
byte[] r = new byte[inputCount];
for (int i = 0, j = 0; i < inputCount; ) {
j += S[++i % 256];
Ut.Swap(ref S[i % 256], ref S[j % 256]);
int t = S[i % 256] + S[j % 256];
r[i-1] = inputBuffer[inputOffset + i - 1] ^ S[t % 256];
}
return r;
}
}
public class RC4 : SymmetricAlgorithm {
byte[] S = new byte[256];
byte[] K = new byte[256];
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV) {
throw new NotImplementedException();
}
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV) {
throw new NotImplementedException();
}
public override void GenerateIV() {
throw new NotImplementedException();
}
public override void GenerateKey() {
var rnd = new Random();
var key = new byte[16];
rnd.NextBytes(key);
Key = key;
}
public override ICryptoTransform CreateEncryptor() {
return new RC4Cryptor() { S = (byte[])S.Clone() };
}
public override ICryptoTransform CreateDecryptor() {
return new RC4Cryptor() { S = (byte[])S.Clone() };
}
public override byte[] Key {
set {
base.Key = value;
for (int i = 0; i < S.Length; ++i) {
S[i] = (byte)i;
K[i] = Key[i % Key.Length];
}
for (int i=0, j=0; i < S.Length; ++i)
Ut.Swap(ref S[i], ref S[j = (j + S[i] + K[i]) % 256]);
}
}
}
}