public static void InitRC4Encryption(byte[] secretKey, byte[] pubKeyIn, byte[] pubKeyOut, RC4_KEY rc4keyIn, RC4_KEY rc4keyOut) { var sha256 = new HMACSHA256(secretKey); var digest = sha256.TransformFinalBlock(pubKeyIn, 0, pubKeyIn.Length); Prepare_key(rc4keyOut, digest, 16); digest = sha256.TransformFinalBlock(pubKeyOut, 0, pubKeyOut.Length); Prepare_key(rc4keyIn, digest, 16); }
public static void RC4(BufferWithOffset buffer, RC4_KEY key, long length) { var state = new byte[256]; short counter; byte x = key.x; byte y = key.y; Buffer.BlockCopy(key.data, 0, state, 0, 256); for (counter = 0; counter < length; counter++) { x = (byte)((x + 1) % 256); y = (byte)((state[x] + y) % 256); var temp = state[x]; state[x] = state[y]; state[y] = temp; var xorIndex = (byte)((state[x] + state[y]) % 256); buffer[counter] ^= state[xorIndex]; } Buffer.BlockCopy(state, 0, buffer.Buffer, 0, 256); key.x = x; key.y = y; }
private static void Prepare_key(RC4_KEY key, byte[] key_data_ptr, int key_data_len) { var state = key.data;; short counter; for (counter = 0; counter < 256; counter++) { state[counter] = (byte)counter; } key.x = 0; key.y = 0; byte index1 = 0; byte index2 = 0; for (counter = 0; counter < 256; counter++) { index2 = (byte)((key_data_ptr[index1] + state[counter] + index2) % 256); var temp = state[counter]; state[counter] = state[index2]; state[index2] = temp; index1 = (byte)((index1 + 1) % key_data_len); } }