C Code Generate 256 Bit Encryption Key
I want to generate AES encryption key to be sent to the the other party in order to communicate securely. 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. Jun 17, 2013 Encrypt, decrypt and generate a key in C# using AES256. OpenSSL is well known for its ability to generate certificates but it can also be used to generate random data. Base64 Generates 32 random bytes (256bits) in a base64 encoded output.
Feels a bit like PHP code. PHP extends the key with zero valued bytes up to 128 bit (then 192 bit or 256 bit depending on the size). It uses the same idea for padding. Both are of course completely idiotic when it comes to cryptographic standards, but there it is. Oh, and many PHP sites use Rijndael with a block size of 256 bit instead of AES too.
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.
| |||||
© 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 |