Example #1
0
        /// <summary>
        /// Encrypts, formats, and sends a message.
        /// </summary>
        /// <param name="message">Message to be sent.</param>
        /// <returns>Byte array containing the returned message.</returns>
        public string send(JObject message, bool isLogin = false)
        {
            bool tryAgain = true;
            while(tryAgain)
            {
                tryAgain = false;
                //Get a public key for RSA to use for encryption.
                string publickey = getKey();

                using (WebClient web = new WebClient())
                {
                    using (AES_THL messageCryptor = new AES_THL())
                    {
                        //Place this inside our encrypted message.
                        string outgoingkey = GenerateKey();

                        message["Pub"] = outgoingkey;

                        //This will encrypt our message with a key from the server.
                        var data = PrepOutMessage(message, publickey, messageCryptor);

                        //Let's derive out outgoing key now and prep AES to handle the incoming message.
                        NameValueCollection aesComponents = DeriveKey(outgoingkey);

                        if (isLogin)
                            data["lgn"] = "t";

                        messageCryptor.Key = Md5ToBytes(aesComponents["key"]);
                        messageCryptor.IV = Md5ToBytes(aesComponents["iv"]);
                    
                        //Data ready to be sent.
                        byte[] response;
                        try
                        {
                            response = web.UploadValues(destination, "POST", data);
                        }
                        catch (Exception ex)
                        {
                            return "Failure.";
                        }
                        string b64Response = Encoding.ASCII.GetString(response);

                        if (b64Response.Equals("Forbidden."))
                        {
                            DBMethods.deleteKey();
                            return b64Response;
                        }
                        else if (b64Response.StartsWith("{\"Pub\"")) 
                        {
                            DBMethods.updateKey(JsonHandler.extractValue(b64Response, "Pub"));
                            tryAgain = true;
                            continue;
                        }

                        byte[] unEncoded = Convert.FromBase64String(b64Response);

                        string inMessage = messageCryptor.Decrypt(unEncoded);

                        DBMethods.updateKey(JsonHandler.extractValue(inMessage, "Pub"));

                        return JsonHandler.extractValue(inMessage, "Operations");
                    }

                }
            }
            return null;
        }
Example #2
0
        private static NameValueCollection PrepOutMessage(JObject message, string publickey, AES_THL messageCryptor)
        {
            //Prep AES
            NameValueCollection aesComponents = DeriveKey(publickey);
            messageCryptor.Key = Md5ToBytes(aesComponents["key"]);
            messageCryptor.IV = Md5ToBytes(aesComponents["iv"]);

            //Encrypt the message.
            byte[] AES_Message = messageCryptor.Encrypt(message.ToString());
            string b64Message = Convert.ToBase64String(AES_Message);

            //Now, let's prepare the POST data.
            var data = new NameValueCollection();
            data["pub"] = publickey;
            data["m"] = b64Message;
            return data;
        }