How to create AES encryption 256 bit key in C# (2024)

Post Views: 32,499

Last Updated on February 11, 2024


After explaining the concept and purpose of AES encryption in the previous article, this guide has for goal to give practical steps about how to create and use an AES 256-bit key.

AES encryption key size

When performing AES encryption, it’s important to pay attention to the key size that you are using. As a matter of fact, the key size has an impact on the strength of the algorithm.

In fact, the AES algorithm supports 3 different key sizes: 128-bit key, 192-bit key, and 256-bit key. Nowadays, it is recommended to always use a 256-bit key to make sure that your data is fully protected.

In most documentation, the term AES-256 is used to refer to an AES encryption using a 256-bit key. So, wherever you see that term, it simply means an AES encryption with a key size of 256 bits.

Create an AES 256-bit key in C#

The easiest way to create an AES 256-bit key is to use the Aes.Create method. That method initializes a cryptographic object implementing the AES algorithm. With that instance, you will be able to call the GenerateKey method to create a new AES key.

Here is an example of the output on the console:

Creating Aes Encryption 256 bit keyAes Key Size : 256Here is the Aes key in Base64:PJC7HnliwcxXw4FM8Ep3sX9NIL3R5CZnDvp8IyyCSlg=

As you can see in this sample code, we are setting the key size to 256 bits before calling the GenerateKey method. And then, we convert the key to a readable Base64 text format.

Once the key is created, you need to save it in a safe place in order to be able to retrieve it for encryption and decryption purposes. You need to remember that the AES key is a secret, so it should NEVER be stored in a text file or database in clear text. Here are the best places to store the key:

  • Store the key as a secret in a key management system: you can store the AES key as a secret in a cloud KMS, like Azure key vault, AWS KMS, or Google Cloud Key Management.
  • Encrypt the key with an asymmetric key and save it in a datastore: You can store the AES key in a database only if the key itself is encrypted. In this case, the best way to protect the AES key is to encrypt it with an asymmetric key (i.e an RSA key for example). Furthermore, the asymmetric key used for that purpose should be stored in a KMS.

Using an AES 256-bit key to encrypt data

In the following example, we are using the AES encryption key to encrypt data:

The following text is an example output of that code:

Welcome to the Aes Encryption Test toolProvide the Aes Key in base64 format :PJC7HnliwcxXw4FM8Ep3sX9NIL3R5CZnDvp8IyyCSlg=--------------------------------------------------------------Please enter the text that you want to encrypt:this is an example plain text that we want to encrypt.--------------------------------------------------------------Aes Cipher Mode : CBCAes Padding Mode: PKCS7Aes Key Size : 256--------------------------------------------------------------Here is the cipher text:TWgNFEi/9kZjDiQTPjCx91H0c/CXtjiVAVV/Kkg9VqcZqmt9K9+/gT/eg9qWCyaqNxpSrKtT+Rbd03NrqEJyzw==--------------------------------------------------------------Here is the Aes IV in Base64:h9GeWnVeV2no4pkircgXAg==

As you noticed, we provided the key in Base64 format and then we loaded the key after the Aes.Create method.

An important factor to consider is the initialization vector. Since it is not recommended to always use the same initialization vector, we are calling the GenerateIV method to create a new initialization vector for every plain text data that we want to encrypt.

Using an AES 256-bit key to decrypt data

Following the same principle, here is the code sample that we are using to decrypt the cipher text.

Here is the console output when we decrypt the previous cipher text with the same AES key:

How to create AES encryption 256 bit key in C# (1)
Welcome to the Aes Encryption Test toolPlease enter the text that you want to decrypt:TWgNFEi/9kZjDiQTPjCx91H0c/CXtjiVAVV/Kkg9VqcZqmt9K9+/gT/eg9qWCyaqNxpSrKtT+Rbd03NrqEJyzw==--------------------------------------------------------------Provide the Aes Key:PJC7HnliwcxXw4FM8Ep3sX9NIL3R5CZnDvp8IyyCSlg=--------------------------------------------------------------Provide the initialization vector:h9GeWnVeV2no4pkircgXAg==--------------------------------------------------------------Aes Cipher Mode : CBCAes Padding Mode: PKCS7Aes Key Size : 256Aes Block Size : 128--------------------------------------------------------------Here is the decrypted data:this is an example plain text that we want to encrypt.

For decryption, we need the following information:

  • The encryption key.
  • The initialization vector.
  • The configuration used to encrypt the data, such as the cipher mode and padding mode.

If you need more information on these concepts, you can look at our previous article about AES encryption.

To sum it up, by following these steps, you will be able to create an AES 256-bit key and use that key to encrypt and decrypt data.

How to create AES encryption 256 bit key in C# (2024)
Top Articles
South Korea Grading System
Are You a Bottom-Up or Top-Down Thinker?
St Thomas Usvi Craigslist
Knoxville Tennessee White Pages
Libiyi Sawsharpener
13 Easy Ways to Get Level 99 in Every Skill on RuneScape (F2P)
Voordelige mode in topkwaliteit shoppen
The Powers Below Drop Rate
Embassy Suites Wisconsin Dells
Baseball-Reference Com
Ave Bradley, Global SVP of design and creative director at Kimpton Hotels & Restaurants | Hospitality Interiors
C Spire Express Pay
My.doculivery.com/Crowncork
United Dual Complete Providers
Uhcs Patient Wallet
Nalley Tartar Sauce
Powerball winning numbers for Saturday, Sept. 14. Check tickets for $152 million drawing
Curver wasmanden kopen? | Lage prijs
Red8 Data Entry Job
3Movierulz
Sam's Club Gas Price Hilliard
Kroger Feed Login
Hrconnect Kp Login
Delta Math Login With Google
Yu-Gi-Oh Card Database
Dairy Queen Lobby Hours
Perry Inhofe Mansion
3 Bedroom 1 Bath House For Sale
Giantess Feet Deviantart
Uhaul Park Merced
Despacito Justin Bieber Lyrics
Nacho Libre Baptized Gif
Craigslist Greencastle
That1Iggirl Mega
Smith And Wesson Nra Instructor Discount
Nancy Pazelt Obituary
Captain Billy's Whiz Bang, Vol 1, No. 11, August, 1920
America's Magazine of Wit, Humor and Filosophy
Pay Entergy Bill
World Social Protection Report 2024-26: Universal social protection for climate action and a just transition
11526 Lake Ave Cleveland Oh 44102
Oppenheimer Showtimes Near B&B Theatres Liberty Cinema 12
The Conners Season 5 Wiki
Setx Sports
Sofia With An F Mugshot
Kutty Movie Net
3 Zodiac Signs Whose Wishes Come True After The Pisces Moon On September 16
St Anthony Hospital Crown Point Visiting Hours
Barber Gym Quantico Hours
Is My Sister Toxic Quiz
What Time Do Papa John's Pizza Close
Craigslist Pets Lewiston Idaho
Jesus Calling Oct 6
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6397

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.