Generate Random Aes 256 Key C
Chilkat • HOME • Android™ • Classic ASP • C • C++ • C# • Mono C# • .NET Core C# • C# UWP/WinRT • DataFlex • Delphi ActiveX • Delphi DLL • Visual FoxPro • Java • Lianja • MFC • Objective-C • Perl • PHP ActiveX • PHP Extension • PowerBuilder • PowerShell • PureBasic • CkPython • Chilkat2-Python • Ruby • SQL Server • Swift 2 • Swift 3/4 • Tcl • Unicode C • Unicode C++ • Visual Basic 6.0 • VB.NET • VB.NET UWP/WinRT • VBScript • Xojo Plugin • Node.js • Excel • Go
| Discusses symmetric encryption key generation techniques for block encryption algorithms such as AES, Blowfish, and Twofish, or for other algorithms such as ChaCha20.
|
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? I want to use this page to create password. Is there any way to impor. So a value must generate the same encrypted string each time it is encrypted. Currently I'm using a 32 char key working on the possibly incorrect assumption this is 256 bits? So, I would want 'the quick brown fox' to be converted to a suitable AES 256 bit key?
© 2000-2020 Chilkat Software, Inc. All Rights Reserved.
#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... please suggest a resolution |
commented Oct 9, 2017 • edited
edited
How-to save -safely- the private key ? Windows registry ? in disk ? I use ASP.NET applications. Test your code |