public override object Deserialize(Stream stream)
        {
            if (encryptionEnabled)
            {
                string descriptorName = stream.ReadUTF();
                int length = stream.ReadInt();
                byte[] buffer = stream.Read(length);

                string privateKey = serverAuthority.GenerateAgreementValue(remotePublicKey).ToString(16);
                RijndaelCrypto crypto = new RijndaelCrypto();

                Type t = registry.GetTypeForDescriptor(crypto.Decrypt(descriptorName, privateKey));
                if (t != null)
                {
                    return ReflectionHelper.InvokeStaticMethodOnType(t, "ParseFrom", crypto.Decrypt(buffer, privateKey));
                }

            }
            else
            {
                string descriptorName = stream.ReadUTF();

                int length = stream.ReadInt();
                byte[] buffer = stream.Read(length);

                Type t = registry.GetTypeForDescriptor(descriptorName);
                if (t != null)
                {
                    return ReflectionHelper.InvokeStaticMethodOnType(t, "ParseFrom", buffer);
                }
            }

            return null;
        }
Esempio n. 2
0
        /// <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;
            }
        }
        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());
            }
        }