How To Use Advanced Encryption Standard(AES) In C#


hey everyone , i got something interesting now :) ok now what is Advanced Encryption Standard(AES)
The Advanced Encryption Standard (AES) is a specification for the encryption of electronic data established by the U.S. National Institute of Standards and Technology (NIST) in 2001.Based on the Rijndael cipher developed by two Belgian cryptographers, Joan Daemen and Vincent Rijmen, who submitted a proposal which was evaluated by the NIST during the AES selection process.-Wikipedia
ok now you know AES lets try it in visual c# . create a console application and name it :) now here is my codes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

//Author : Mohamed Shimran
//Blog : http://www.ultimateprogrammingtutorials.blogspot.com

namespace AES
{
    class Program
    {
        static void Main(string[] args)
        {
            UTF8Encoding UTF8 = new UTF8Encoding();

            Console.WriteLine("Shims Encryption UPT");

            string text = "Ultimate programming tutorials";
            byte[] txt = UTF8.GetBytes(text);
            Console.WriteLine("Text: " + text);

            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            byte[] key = aes.Key;
            byte[] iv = aes.IV;
            
            byte[] n;

            ICryptoTransform ict = aes.CreateEncryptor(key, iv);

            MemoryStream ms = new MemoryStream();

            CryptoStream cs = new CryptoStream(ms, ict, CryptoStreamMode.Write);

            StreamWriter sw = new StreamWriter(cs);
            sw.Write(text);
            sw.Close();

            n = ms.ToArray();

            Console.WriteLine("Encrypted : " + Convert.ToBase64String(n));

            ICryptoTransform icrt = aes.CreateDecryptor(key, iv);

            MemoryStream mms = new MemoryStream(n);

            CryptoStream cts = new CryptoStream(mms, icrt, CryptoStreamMode.Read);

            StreamReader sr = new StreamReader(cts);

            Console.WriteLine("Decrypted : " + sr.ReadToEnd());

            sr.Close();

            aes.Clear();
            Array.Clear(key, 0, key.Length);
            Array.Clear(iv, 0, iv.Length);

            Console.Read();
        }
    }
}

now string text is your text that is going to be encrypted using the program so replace Ultimate programming tutorials and now debug the program and you will the text encrypted and the text decrypted . how awesome :)

1 comments:

Hi MOHAMED SHIMRAN can you help me for file locking code using AES Encryption...

Reply

Post a Comment

Note: Only a member of this blog may post a comment.