Generate Random Aes 256 Key C#

Posted By admin On 11.12.20
Encrypt, decrypt and generate a key in C# using AES256.
encryption.cs
#regionEncryption
/// <summary>
/// Generate a private key
/// From : www.chapleau.info/blog/2011/01/06/usingsimplestringkeywithaes256encryptioninc.html
/// </summary>
privatestaticstringGenerateKey(intiKeySize)
{
RijndaelManagedaesEncryption=newRijndaelManaged();
aesEncryption.KeySize=iKeySize;
aesEncryption.BlockSize=128;
aesEncryption.Mode=CipherMode.CBC;
aesEncryption.Padding=PaddingMode.PKCS7;
aesEncryption.GenerateIV();
stringivStr=Convert.ToBase64String(aesEncryption.IV);
aesEncryption.GenerateKey();
stringkeyStr=Convert.ToBase64String(aesEncryption.Key);
stringcompleteKey=ivStr+','+keyStr;
returnConvert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(completeKey));
}
/// <summary>
/// Encrypt
/// From : www.chapleau.info/blog/2011/01/06/usingsimplestringkeywithaes256encryptioninc.html
/// </summary>
privatestaticstringEncrypt(stringiPlainStr, stringiCompleteEncodedKey, intiKeySize)
{
RijndaelManagedaesEncryption=newRijndaelManaged();
aesEncryption.KeySize=iKeySize;
aesEncryption.BlockSize=128;
aesEncryption.Mode=CipherMode.CBC;
aesEncryption.Padding=PaddingMode.PKCS7;
aesEncryption.IV=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[0]);
aesEncryption.Key=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[1]);
byte[] plainText=ASCIIEncoding.UTF8.GetBytes(iPlainStr);
ICryptoTransformcrypto=aesEncryption.CreateEncryptor();
byte[] cipherText=crypto.TransformFinalBlock(plainText, 0, plainText.Length);
returnConvert.ToBase64String(cipherText);
}
/// <summary>
/// Decrypt
/// From : www.chapleau.info/blog/2011/01/06/usingsimplestringkeywithaes256encryptioninc.html
/// </summary>
privatestaticstringDecrypt(stringiEncryptedText, stringiCompleteEncodedKey, intiKeySize)
{
RijndaelManagedaesEncryption=newRijndaelManaged();
aesEncryption.KeySize=iKeySize;
aesEncryption.BlockSize=128;
aesEncryption.Mode=CipherMode.CBC;
aesEncryption.Padding=PaddingMode.PKCS7;
aesEncryption.IV=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[0]);
aesEncryption.Key=Convert.FromBase64String(ASCIIEncoding.UTF8.GetString(Convert.FromBase64String(iCompleteEncodedKey)).Split(',')[1]);
ICryptoTransformdecrypto=aesEncryption.CreateDecryptor();
byte[] encryptedBytes=Convert.FromBase64CharArray(iEncryptedText.ToCharArray(), 0, iEncryptedText.Length);
returnASCIIEncoding.UTF8.GetString(decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length));
}
#endregion

How to create a secure random AES key in Java? Ask Question. @HemanthPeela It defines the length of the keys to generate. 256-bit keys are stronger than 128-bit keys.

commented Jun 6, 2014

hi fairly new to the cryptography...
when im implementing the above code im getting a error while decrypting..
'Padding is invalid and cannot be removed.'

please suggest a resolution
thanks and regards

AesAes 256 encryption key generator

commented Oct 9, 2017
edited

  • Dec 31, 2014  I was looking for some simple examples of using AES symmetric encryption to encrypt and decrypt data in C#. Though there are some very helpful resources out there, what I needed were basic routines that: - Take clear text and key as byte arrays and return encrypted text as a byte array. Take encrypted.
  • I need to generate a key to use when encrypting a file symmetrically using AES256/CBC The key itself will be encrypted with RSA public/private so I don't need a password applied. In Java, this se.
  • C# (CSharp) System.Security.Cryptography AesManaged.GenerateKey - 30 examples found. These are the top rated real world C# (CSharp) examples of System.Security.Cryptography.AesManaged.GenerateKey extracted from open source projects. You can rate examples to help us improve the quality of examples.
  • Hashing, Encryption and Random in ASP.NET Core. This post look at hashing, encryption and random string generation in ASP.NET Core. We examine a few different approaches and explain why some common techniques should be avoided in modern applications. Generating a random string. It is a very common requirement to generate random strings.
  • In the beginning the two nodes will create a shared session key by using Deffie-Helman protocol, then one of them will genreate AES key and send it to the other node through the secure channel(i.e. DH protocol). So Could you please help in generating AES-256 bits in C# without using it to encrypt any plaintext. I just want the key itself.
  • I am doing AES Key Generation in c# and passing the key generated for AES 128 bit Encryption. The case is while generating the key I am getting byte length as 16 while the key string length is getting higher than 16. While trying online I am getting length as 16 itself. AES 256 file encryption key mistake. How does an AES work?

C# Aes Key

How-to save -safely- the private key ? Windows registry ? in disk ?

I use ASP.NET applications.

Test your code

Pgp

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment