+ Reply to Thread
Results 1 to 2 of 2

Thread: Encrypting & Decrypting using AES

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    17

    Encrypting & Decrypting using AES

    I'd like a tutorial showing how to use AES to encrypt and decrypt text , it would be awesome if you provided an example and thanks a lot in advance.
    Last edited by jemmy; 11-02-2012 at 11:38 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    36
    Here is the encryption and decryption method along with an example.

    Encryption Function :
    Code:
            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("Key");
                byte[] encrypted;
                // Create an RijndaelManaged object
                // with the specified key and IV.
                using (RijndaelManaged rijAlg = new RijndaelManaged())
                {
                    rijAlg.Key = Key;
                    rijAlg.IV = IV;
                    rijAlg.Mode = CipherMode.CBC;
                    rijAlg.Padding = PaddingMode.Zeros;
    
                    // 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;
    
            }
    Decryption Function :
    Code:
            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("Key");
    
                // 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;
                    rijAlg.Mode = CipherMode.CBC;
                    rijAlg.Padding = PaddingMode.Zeros;
    
                    // 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;
            }
    The Example :
    Code:
                try
                {
                    string data = "Hi i am here to teach you AES Encryption";
                    MessageBox.Show("Original text :   " + data);
    
                    // 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(data, myRijndael.Key, myRijndael.IV);
    
                        StringBuilder s = new StringBuilder();
                        foreach (byte item in encrypted)
                        {
                            s.Append(item.ToString("X2") + " ");
                        }
                        MessageBox.Show("Encrypted text :   " + s);
                        
                        // Decrypt the bytes to a string.
                        string decrypted = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
    
                        //Display the original data and the decrypted data.
                        MessageBox.Show("Decrypted text:    " + decrypted);
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: {0}", ex.Message);
                }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts