Thursday , February 21 2019
Home / Dot Net / ASP.NET / AES Encryption and Decryption (Cryptography) Tutorial with example

AES Encryption and Decryption (Cryptography) Tutorial with example

AES Encryption and Decryption:

AES stands for Advanced Encryption Standard and it was originally called the Rijndael cipher, it is based off a combination of the two Belgian author’s names Joan Daemen and Vincent Rijmen. After the cipher was accepted in 2002 to replace the aging DES cipher, it became the governments choice for top secret information.

The AES algorithm supports 128, 192 and 256 bit encryption, which is determined from the key size : 128 bit encryption when the key is 16 bytes, 192 when the key is 24 bytes and 256 bit when the key is 32 bytes.

The methods provided by the library accept also a string password instead of a key, which is internally converted to a key with a chosen Hash function.

The optional initialization vector (IV) size is 16 bytes, which is the block size of the algorithm. This is summarized in the table below:

Key size AES encryption
16 bytes (128 bits) 128 bit encryption
24 bytes (192 bits) 192 bit encryption
32 bytes (256 bits) 256 bit encryption

Now we will see an example on AES Encryption and Decryption in C#


1.Create new windows forms application

2.Right click on solution > add new class > name it as EncryptionHelper.cs and write below code

3.Create a form design like below

AES Encryption

4.Add below code

declare a global variable

add below code in Encryption and Decryption button click events

once code completed run the application and We can get below Output:

1.Enter sample Text and click Encrypt Text button

AES Encryption Original Text

2.when we click encrypt text button data will be encrypted

AES Encrypted Text

1.Click decrypt text button it will decrypts and revert back original data

AES Encryption Decrypted Text

Download Full Code for AES Encryption 


Padding and Block modes:

The Padding and Block mode are important settings for the AES class, that affect the produced encrypted output.

The Padding is used to align the input data to the algorithm BlockSize (16 bytes). The default is PKCS7.

The Block Mode determines what transformation is performed on each processed block. The default one is CBC. It is very important to know what block mode was used for encryption, in order to be able to decrypt it! For example if we know that the encryption has used CBC block mode, we shall set the class to use that mode before decrypting as follows:


Did you find this post as useful? I hope you liked the article. Please share your valuable suggestions and feedback in the comments.


Print Friendly, PDF & Email

Give us your valuable feedback


About Murali

Hello everyone I am Software Developer having 3+ years of Experience in Microsoft Technologies and Others. My skills :-, C#,, SQL SERVER, web services, windows Service,Windows applications,HTML,CSS, JavaScript,JQuery,AnjularJs

Check Also

How to Send an Email In Perfect way Using C#

How to Send an Email In Perfect Way using C#: Now a days for every …