0

I am researching and studying about encryption and decryption in rijndael and AES. I have a question.how should we decrypt data in socket programming ? how should we send and use KEY and IV in a client and server program. I`ve read these codes in MSDN but in this example all codes are are run on one file and Key and IV is generated in the same place but I don't know how should we transmit a KEY and IV to server in a safe and secure manner...thanks a lot.

here is also MSDN example :

using System;
using System.IO;
using System.Security.Cryptography;

namespace RijndaelManaged_Example
{
    class RijndaelExample
    {
        public static void Main()
        {
            try
            {



            string original = "Here is some data to encrypt!";

            // Create a new instance of the RijndaelManaged 
            // class.  This generates a new key and initialization  
            // vector (IV). 
            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {

                myRijndael.GenerateKey();
                myRijndael.GenerateIV();
                // Encrypt the string to an array of bytes. 
                byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                // Decrypt the bytes to a string. 
                string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                //Display the original data and the decrypted data.
                Console.WriteLine("Original:   {0}", original);
                Console.WriteLine("Round Trip: {0}", roundtrip);
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }
    }
    static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");
        byte[] encrypted;
        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption. 
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
        }


        // Return the encrypted bytes from the memory stream. 
        return encrypted;

    }

    static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("IV");

        // Declare the string used to hold 
        // the decrypted text. 
        string plaintext = null;

        // Create an RijndaelManaged object 
        // with the specified key and IV. 
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream 
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;

    }
}

}

  • 1
    Not really an answer but quite an interesting read on the hidden complications of actually "sharing" a shared secret: http://blogs.msdn.com/b/ericlippert/archive/2011/09/27/keep-it-secret-keep-it-safe.aspx – Corak Feb 03 '15 at 10:20
  • As far as i know you dont need to hide the IV, you should generate a new IV for each encryption, then send IV and the encrypted message to your destination. so you just need to share the key. – BoeseB Feb 03 '15 at 10:30
  • 1
    But i would advice you to not implement your own encryption protocol for the reasons stated here http://security.stackexchange.com/questions/2202/lessons-learned-and-misconceptions-regarding-encryption-and-cryptology/2210#2210 – BoeseB Feb 03 '15 at 10:39
  • @BoeseB : ok so how should share the key? – Mohammad ALZ Feb 03 '15 at 12:07
  • I am not aware of a absolut safe place to store the key. If your program needs to use the key there is always the chance it leaks. if you have control over both "client" and "server" or whatever part and wont need to alternate the used key per instace, you could store it in the sourcecode. A bit better would be to store it in a encrypted part of the webconfig or something. – BoeseB Feb 03 '15 at 12:11
  • @BoeseB : OK thanks. is there a reference or example or project that can help me with it ? – Mohammad ALZ Feb 04 '15 at 03:50
  • Sorry i don't know a good tutorial for this. But you could find more information from this answer http://stackoverflow.com/a/9512595/4369295 – BoeseB Feb 04 '15 at 06:50

0 Answers0