public static void RC4Crypt(byte[] pachIn, int in_offset, byte[] pachOut, int out_offset, uint nLen, RC4Key key) { Debug.Assert(key != null && nLen > 0); if (key == null) { return; } byte byX = key.byX;; byte byY = key.byY; byte[] pabyState = key.abyState; byte byXorIndex; for (uint i = 0; i < nLen; i++) { byX = Convert.ToByte(byX + 1); byY = Convert.ToByte(pabyState[byX] + byY); MpdUtilities.SwapByte(ref pabyState[byX], ref pabyState[byY]); byXorIndex = Convert.ToByte(pabyState[byX] + pabyState[byY]); if (pachIn != null) { pachOut[out_offset + i] = Convert.ToByte(pachIn[in_offset + i] ^ pabyState[byXorIndex]); } } key.byX = byX; key.byY = byY; }
public static RC4Key RC4CreateKey(byte[] pachKeyData, uint nLen, ref RC4Key key, bool bSkipDiscard) { byte index1; byte index2; byte[] pabyState; if (key == null) { key = new RC4Key(); } pabyState = key.abyState; for (int i = 0; i < 256; i++) { pabyState[i] = (byte)i; } key.byX = 0; key.byY = 0; index1 = 0; index2 = 0; for (int i = 0; i < 256; i++) { index2 = Convert.ToByte(pachKeyData[index1] + pabyState[i] + index2); MpdUtilities.SwapByte(ref pabyState[i], ref pabyState[index2]); index1 = (byte)((index1 + 1) % nLen); } if (!bSkipDiscard) { RC4Crypt(null, null, 1024, key); } return(key); }