示例#1
0
        /// <summary>
        /// Sends a message to a specific user on the server
        /// </summary>
        /// <param name="email">Email of user </param>
        /// <param name="plaintext">Text message to be sent </param>
        /// <returns></returns>
        private async Task SendMsg(string email, string plaintext)
        {
            try
            {
                //Check if we have the public key on our disk
                var pos      = Directory.GetCurrentDirectory().LastIndexOf("\\");
                var filename = Directory.GetCurrentDirectory().Substring(0, pos) + "\\" + email + ".key";
                if (!File.Exists(filename))
                {
                    Console.WriteLine("Key does not exist for " + email + "\n");
                }
                else
                {
                    //encide text message and decrypt using RSA formula
                    var textBytes = Encoding.ASCII.GetBytes(plaintext);
                    var bigInt    = new BigInteger(textBytes);
                    var key       = File.ReadAllText(filename);

                    //e, E, n, N extraction
                    byte[] keyBytes = Convert.FromBase64String(key);
                    var    eSize    = new byte[4];
                    for (var i = 0; i < 4; i++)
                    {
                        eSize[i] = keyBytes[i];
                    }
                    //if (BitConverter.IsLittleEndian)
                    //    Array.Reverse(eSize);
                    var e     = BitConverter.ToInt32(eSize, 0);
                    var bytes = new byte[e];
                    for (var i = 4; i < (4 + e); i++)
                    {
                        bytes[i - 4] = keyBytes[i];
                    }
                    BigInteger E     = new BigInteger(bytes);
                    var        nSize = new byte[4];
                    for (var i = 4 + e; i < 4 + e + 4; i++)
                    {
                        nSize[i - 4 - e] = keyBytes[i];
                    }
                    //if (BitConverter.IsLittleEndian)
                    //    Array.Reverse(nSize);
                    var n      = BitConverter.ToInt32(nSize, 0);
                    var nBytes = new byte[n];
                    for (var i = 0; i < n; i++)
                    {
                        nBytes[i] = keyBytes[i + 4 + e + 4];
                    }
                    BigInteger N = new BigInteger(nBytes);

                    //Final decryption of text
                    var cipher      = BigInteger.ModPow(bigInt, E, N);
                    var cipherArray = cipher.ToByteArray();
                    var message     = Convert.ToBase64String(cipherArray);
                    var textObj     = new TextMessage();
                    textObj.email   = email;
                    textObj.content = message;
                    var jsonText = JObject.FromObject(textObj);

                    //PUT request sends a JSON object
                    var content  = new StringContent(jsonText.ToString(), Encoding.UTF8, "application/json");
                    var response = await client.PutAsync("http://kayrun.cs.rit.edu:5000/Message/" + email, content);

                    if (!response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("Unable to send Message");
                    }
                    else
                    {
                        Console.WriteLine("Message written");
                    }
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("exception!\n" + e.Message);
            }
        }
示例#2
0
 public TextMessage(TextMessage savedMessage)
 {
     body     = savedMessage.body;
     authorId = savedMessage.authorId;
     datetime = savedMessage.datetime;
 }