private int Decrypt(ref Packet pkt, int Length, uint CipherKey) { if (pkt == null) return 0; int pOff = 2, pLen = Length - pOff; byte[] outdata = pkt.Data; byte[] Plain = new byte[pLen]; Buffer.BlockCopy(outdata, pOff, Plain, 0, pLen); int Off = 0; int Round = pLen / sizeof(uint); for (int i = 0; i < Round; i++) { uint RoundPlain = BitConverter.ToUInt32(Plain, Off); RoundPlain ^= CipherKey; Buffer.BlockCopy(BitConverter.GetBytes(RoundPlain), 0, Plain, Off, sizeof(uint)); Off += sizeof(uint); } int Rest = pLen % sizeof(uint); for (int i = 0; i < Rest; i++) { byte RestPlain = Plain[Off]; RestPlain ^= (byte)CipherKey; Plain[Off] = RestPlain; Off++; } Buffer.BlockCopy(Plain, 0, outdata, pOff, pLen); pkt.SetData(outdata); return Length; }
public int TxEncrypt(ref Packet pkt) { Encrypt(ref pkt, pkt.Data.Length, sendKeyGenerator.Generate()); return 0; }
public int RxDecrypt(ref Packet pkt) { Decrypt(ref pkt, pkt.Data.Length, recvKeyGenerator.Generate()); return 0; }