public ComClient(bool userSyncContext = true) { IsOpened = false; this.userSyncContext = userSyncContext; AddChannel(new DataChannel <IssueIdData>((short)PreservedChannelId.IssueId, QosType.Reliable, Compression.None, Encryption.None, (node, data) => { UserId = data.Id; bool isValid = ValidateRsaKey(data.PublicKey); if (isValid) { rsaEncrypter = new RsaEncrypter(); rsaEncrypter.FromPublicKeyXmlString(data.PublicKey); AesKeyPair pair = GenerateAesKey(); SendInternal((short)PreservedChannelId.KeyExchange, pair); } else { Disconnect(); } })); AddChannel(new DataChannel <byte[]>((short)PreservedChannelId.Health, QosType.Unreliable, Compression.None, Encryption.None, (node, data) => { //Util.Log("Health"); healthLostCount = 0; byte[] encrypted = EncrypteTmpKey(data); SendInternal((short)PreservedChannelId.Health, encrypted); })); AddChannel(new DataChannel <IssueIdData>((short)PreservedChannelId.UdpNotify, QosType.Unreliable, Compression.None, Encryption.None, (node, data) => { })); AddChannel(new DataChannel <int>((short)PreservedChannelId.UdpNotifyAck, QosType.Reliable, Compression.None, Encryption.None, (node, data) => { udpAck = true; IsConnected = true; if (OnConnected != null) { OnConnected(serverNode); } })); AddChannel(new DataChannel <AesKeyPair>((short)PreservedChannelId.KeyExchange, QosType.Reliable, Compression.None, Encryption.Rsa, (node, data) => { })); AddChannel(new DataChannel <int>((short)PreservedChannelId.KeyExchangeAck, QosType.Reliable, Compression.None, Encryption.None, (node, data) => { udpAck = false; })); healthTimer = new System.Timers.Timer(healthIntervalMs); healthTimer.Elapsed += OnHealthCheck; }
void OnKeyExchange(ComNode node, AesKeyPair data) { var aes = Aes.Create(); aes.Padding = PaddingMode.PKCS7; aes.Key = data.Key; aes.IV = data.IV; node.AesEncrypter = new AesEncrypter(aes); node.AesDecrypter = new AesDecrypter(aes); SendInternal(node, (short)PreservedChannelId.KeyExchangeAck, 0); }
AesKeyPair GenerateAesKey() { var aes = Aes.Create(); aes.GenerateIV(); aes.GenerateKey(); aes.Padding = PaddingMode.PKCS7; serverNode.AesEncrypter = new AesEncrypter(aes); serverNode.AesDecrypter = new AesDecrypter(aes); AesKeyPair pair = new AesKeyPair(); pair.Key = aes.Key; pair.IV = aes.IV; return(pair); }