public override void Serialize(object messageObject, Stream stream) { IMessage message = (IMessage)messageObject; string descriptorName = message.DescriptorForType.FullName; if (encryptionEnabled) { string privateKey = serverAuthority.GenerateAgreementValue(remotePublicKey).ToString(16); RijndaelCrypto crypto = new RijndaelCrypto(); string encryptedName = crypto.Encrypt(descriptorName, privateKey); stream.Write(encryptedName.SerializeUTF()); //StringBuilder str = new StringBuilder(); //str.AppendFormat("{0}\r\n", encryptedName); //byte[] nameBytes = MessageEncoding.GetBytes(str.ToString()); //stream.Write(nameBytes, 0, nameBytes.Length); byte[] messageBytes = message.ToByteArray(); byte[] encryptedBytes = crypto.Encrypt(messageBytes, privateKey); stream.Write(encryptedBytes.Serialize()); } else { //StringBuilder str = new StringBuilder(); //str.AppendFormat("{0}\r\n", descriptorName); stream.Write(descriptorName.SerializeUTF()); //byte[] nameBytes = MessageEncoding.GetBytes(str.ToString()); //stream.Write(nameBytes, 0, nameBytes.Length); byte[] messageBytes = message.ToByteArray(); stream.Write(messageBytes.Serialize()); } }
private void ProcessResponse(ResponseHeader header, string rawRequestString) { if (header.MessageHeader.EncryptionHeader.EncryptionType != EncryptionTypes.None) { DHPublicKeyParameters publicKey = new DHPublicKeyParameters( ((DHPublicKeyParameters)PublicKeyFactory.CreateKey(_serverPublicKey)).Y, _serverAuthority.Parameters); BigInteger agreementValue = _serverAuthority.GenerateAgreementValue(publicKey); RijndaelCrypto crypto = new RijndaelCrypto(); rawRequestString = crypto.Decrypt(rawRequestString, agreementValue.ToString(16)); } switch (header.ResponseType) { case ResponseTypes.LoginResponse: { LoginResponse response = XmlSerializationHelper.DeSerialize <LoginResponse>(rawRequestString); OnServerResponse(new ServerResponseEventArgs() { Response = response }); } break; case ResponseTypes.JoinRoomEvent: { JoinRoomEvent evt = XmlSerializationHelper.DeSerialize <JoinRoomEvent>(rawRequestString); OnServerEvent(new ServerEventEventArgs() { ServerEvent = evt }); } break; case ResponseTypes.RoomUserUpdateEvent: { RoomUserUpdateEvent evt = XmlSerializationHelper.DeSerialize <RoomUserUpdateEvent>(rawRequestString); OnServerEvent(new ServerEventEventArgs() { ServerEvent = evt }); } break; } }
/// <summary> /// Processes the request. /// </summary> /// <param name="header">The header.</param> /// <param name="publicKeyEncoded">The public key encoded.</param> /// <param name="response">The response.</param> /// <returns></returns> public static String ProcessRequest(ServerAuthority serverAuthority, byte[] publicKeyEncoded, RequestHeader header, String request) { if (header.MessageHeader.EncryptionHeader.EncryptionType != EncryptionTypes.None) { //byte[] publicKeyEncoded = client.RequestHeader.MessageHeader.EncryptionHeader.PublicKey; DHPublicKeyParameters publicKey = new DHPublicKeyParameters( ((DHPublicKeyParameters)PublicKeyFactory.CreateKey(publicKeyEncoded)).Y, serverAuthority.Parameters); BigInteger agreementValue = serverAuthority.GenerateAgreementValue(publicKey); RijndaelCrypto crypto = new RijndaelCrypto(); return(crypto.Encrypt(request, agreementValue.ToString(16))); } else { return(request); } }