Generate Random Aes 256 Key C

Posted : admin On 15.12.2020

C# (CSharp) System.Security.Cryptography AesCryptoServiceProvider.GenerateKey - 30 examples found. These are the top rated real world C# (CSharp) examples of System.Security.Cryptography.AesCryptoServiceProvider.GenerateKey extracted from open source projects. 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. Options for generating secure random strong encryption keys. Options for generating secure random strong encryption keys. But it can also be used to generate.

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

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

commented Oct 9, 2017
edited

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

I use ASP.NET applications.

Test your code

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

I need to make strong key for AES-256 in a) Unicode characters, b) key in bytes.

a) I have to generate 50 random Unicode characters and then convert them to bytes. Is this possible to use Unicode characters as AES256 key?For e.g. I want to use this page to create password.is there any way to import all characters from Windows characters table to program in Windows Form App?

b) I'm using this code:

It's enough or I should change something?

Also I have one more question. Making an AES key longer then 43 ASCII characters will be more secure or it will be anyway hashed to 256bit? And there is difference between ASCII key of 43 characters and 100?

1 answers

2

a) I have to generate 50 random Unicode characters and then convert them to bytes. Is this possible to use Unicode characters as AES256 key?

Yes, this is possible. Since you have plenty of space for characters you can just encode it. ceil(32 / 3) * 4 = 44, so you'd have enough characters for this. You would not be using the additional space provided by Unicode encoding though. Obviously you would need to convert it back to binary before using it.

b) is aes.GenerateKey 'enough'?

Yes, aes.GenerateKey is enough to generate a binary AES key.

Fips 140-2

Generate

Generate Aes Key Openssl

c) Making an AES key longer then 43 ASCII characters will be more secure or it will be anyway hashed to 256bit? And there is difference between ASCII key of 43 characters and 100?

An AES key is not hashed at all. It's just 128, 192 or 256 bits (i.e. 16, 24 or 32 bytes) of data that should be indistinguishable from random (to somebody that doesn't know the value, of course). If you want to hash something you'd have to do it yourself - but please read on.

The important thing to understand is that a password is not a key, and that keys for modern ciphers are almost always encoded as binary. For AES there is no such thing as an ASCII key. The sims 3 seasons cd key generator. If you need to encode the key, use base 64.

If you want to use a password then you need to use a key derivation function or KDF. Windows 7 32 bit cd key generator. Furthermore, if you want to protect against dictionary and rainbow table attacks you will want to use a password based key derivation function or PBKDF. Such a KDF is also called a 'password hash'. In case of .NET your best bet is Rfc2898DeriveBytes which implements PBKDF2. PBKDF2 is defined in the RFC 2898 titled: PKCS #5: Password-Based Cryptography Specification Version 2.0 which you may want to read.