Exemple #1
0
        public CipherMessage EncryptMessage(string text)
        {
            // Convert string to a byte array
            CipherMessage message = new CipherMessage();

            byte[] plainBytes = Encoding.Unicode.GetBytes(text.ToCharArray());

            // A new key and iv are generated for every message
            rc2.GenerateKey();
            rc2.GenerateIV();

            // The rc2 initialization doesnt need to be encrypted, but will
            // be used in conjunction with the key to decrypt the message.
            message.rc2IV = rc2.IV;
            try
            {
                // Encrypt the RC2 key using RSA encryption
                message.rc2Key = rsa.Encrypt(rc2.Key, false);
            }
            catch (CryptographicException e)
            {
                // The High Encryption Pack is required to run this  sample
                // because we are using a 128-bit key. See the readme for
                // additional information.
                Console.WriteLine("Encryption Failed. Ensure that the" +
                                  " High Encryption Pack is installed.");
                Console.WriteLine("Error Message: " + e.Message);
                Environment.Exit(0);
            }
            // Encrypt the Text Message using RC2 (Symmetric algorithm)
            ICryptoTransform sse = rc2.CreateEncryptor();
            MemoryStream     ms  = new MemoryStream();
            CryptoStream     cs  = new CryptoStream(ms, sse, CryptoStreamMode.Write);

            try
            {
                cs.Write(plainBytes, 0, plainBytes.Length);
                cs.FlushFinalBlock();
                message.cipherBytes = ms.ToArray();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                ms.Close();
                cs.Close();
            }
            return(message);
        } // method EncryptMessage
Exemple #2
0
        } // method EncryptMessage

        public void DecryptMessage(CipherMessage message)
        {
            // Get the RC2 Key and Initialization Vector
            rc2.IV = message.rc2IV;
            try
            {
                // Try decrypting the rc2 key
                rc2.Key = rsa.Decrypt(message.rc2Key, false);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Decryption Failed: " + e.Message);
                return;
            }

            ICryptoTransform ssd = rc2.CreateDecryptor();
            // Put the encrypted message in a memorystream
            MemoryStream ms = new MemoryStream(message.cipherBytes);
            // the CryptoStream will read cipher text from the MemoryStream
            CryptoStream cs = new CryptoStream(ms, ssd, CryptoStreamMode.Read);

            byte[] initialText = new Byte[message.cipherBytes.Length];

            try
            {
                // Decrypt the message and store in byte array
                cs.Read(initialText, 0, initialText.Length);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                ms.Close();
                cs.Close();
            }

            // Display the message received
            Console.WriteLine(name + " received the following message:");
            Console.WriteLine("  " + Encoding.Unicode.GetString(initialText));
        } // method DecryptMessage