/// <summary> /// Encryption helper method. /// </summary> private void OnRijndaelExchange(INetworkConnection con, Packet pck) { PacketRijndaelExchange msg = pck as PacketRijndaelExchange; try { m_ConnectionKey = CryptoManager.DecryptRijndaelKey(msg.RijndaelExchangeData); PacketLineSecured p = (PacketLineSecured)CreatePacket((int)PacketType.LineSecured, 0, false, true); p.Key = CryptoManager.RijEncrypt(m_ConnectionKey, 0, m_ConnectionKey.Length, m_ConnectionKey); p.ReplyCode = ReplyType.OK; msg.ReplyPacket = p; RemoteRsaKey = msg.PublicRSAKey; } catch (Exception e) { KillConnection("Encryption key exchange error. Disconnecting client connection. " + e.Message); } }
private void OnRijndaelExchangeRequest(INetworkConnection con, Packet msg) { PacketRijndaelExchangeRequest p = msg as PacketRijndaelExchangeRequest; // Server said hello. Generate, encrypt with public RSA key and finally send the key. this will be our // connection key for as long as this connection is valid // Generate & Store new key m_ConnectionKey = CryptoManager.GetRandomRijndaelKey(); RemoteRsaKey = p.PublicRSAKey; // Encrypt it with the public RSA key from the server byte[] encryptedKey = CryptoManager.EncryptRijndaelKey(p.PublicRSAKey, m_ConnectionKey); // Send it PacketRijndaelExchange re = (PacketRijndaelExchange)CreatePacket((int)PacketType.RijndaelExchange, 0, false, false); re.RijndaelExchangeData = encryptedKey; re.PublicRSAKey = CryptoManager.PublicRSAKey; re.ReplyCode = m_ConnectionKey != null && m_ConnectionKey.Length > 0 ? ReplyType.OK : ReplyType.Failure; re.ReplyPacketType = msg.PacketTypeID; re.ReplyPacketID = msg.PacketID; msg.ReplyPacket = re; }