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
Animal welfare regulations
CD Early Withdrawal Penalty: What to Know and How to Avoid It - NerdWallet
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
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.