Click here to Skip to main content
14,691,972 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
using System;
using System.IO;
using System.Security.Cryptography;

namespace Aes_Examples
{
    class AesExample
    {
        public static void Main()
        {
            try
            {

                string original = "818181818181818181818181818181818282828282828282828282828282828281828384858687888990919293949596";

                // Create a new instance of the Aes
                // class.  This generates a new key and initialization 
                // vector (IV).
                //Aes myAes = Aes.Create();
                string key =   "7a14bf74728c5f3414f184257f3fa8d1" ;
                int i = 0;
                // x variable used to hold byte array element position
                int x = 0;
                // allocate byte array based on half of string length
                byte[] bytes = new byte[(key.Length) / 2];
                // loop through the string - 2 bytes at a time converting
                //  it to decimal equivalent and store in byte array
                while (key.Length > i + 1)
                {
                    long lngDecimal = Convert.ToInt32(key.Substring(i, 2), 16);
                    bytes[x] = Convert.ToByte(lngDecimal);
                    i = i + 2;
                    ++x;
                }


                string iv = "00000000000000000000000000000000";
                Aes aesAlg;
                 aesAlg = Aes.Create();
                aesAlg.Mode = CipherMode.CBC;
                int i1 = 0;
                // x variable used to hold byte array element position
                int x1 = 0;
                // allocate byte array based on half of string length
                byte[] bytes1 = new byte[(iv.Length) / 2];
                // loop through the string - 2 bytes at a time converting
                //  it to decimal equivalent and store in byte array
                while (iv.Length > i1 + 1)
                {
                    long lngDecimal = Convert.ToInt32(iv.Substring(i1, 2), 16);
                    bytes1[x1] = Convert.ToByte(lngDecimal);
                    i1 = i1 + 2;
                    ++x1;
                }
                byte[] encrypted = encryptStringToBytes_AES(original,  bytes, bytes1);
                Console.WriteLine("Original:   {0}", original);
                Console.WriteLine("Original:   {0}", encrypted[1]);
                 }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
                static byte[] encryptStringToBytes_AES(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");

            // Declare the streams used
            // to encrypt to an in memory
            // array of bytes.
            MemoryStream msEncrypt = null;
            CryptoStream csEncrypt = null;
            StreamWriter swEncrypt = null;

            // Declare the Aes object
            // used to encrypt the data.
            Aes aesAlg = null;

            // Declare the bytes used to hold the
            // encrypted data.
            byte[] encrypted = null;

            try
            {
                // Create an Aes object
                // with the specified key and IV.
                aesAlg = Aes.Create();
                aesAlg.Key = Key;
                aesAlg.IV = IV; 
                //aesAlg.BlockSize = 128;

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

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

                //Write all data to the stream.
               swEncrypt.Write(plainText);

            }
            finally
            {
                // Clean things up.

                // Close the streams.
                if (swEncrypt != null)
                    swEncrypt.Close();
                if (csEncrypt != null)
                    csEncrypt.Close();
                if (msEncrypt != null)
                    msEncrypt.Close();

                // Clear the Aes object.
                if (aesAlg != null)
                    aesAlg.Clear();
            }

            // Return the encrypted bytes from the memory stream.
            return msEncrypt.ToArray();

        }

               
    }
}

In this code data is only 48 byte but after apply encrption with padding byte none in aes mode then cipher text meets 96 byte. but I want only 48 bytes of cipher text. how it can be passible.
Posted
Updated 17-Jan-12 0:06am
v4
Comments
Anuja Pawar Indore 16-Jan-12 8:10am
   
Added pre tag
OriginalGriff 16-Jan-12 8:24am
   
You forgot to ask a question.
That is just a code dump of your app, with no clue as to your problem.
Use the "Improve question" widget to edit your question and provide better information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900