Skip to content

Files

Latest commit

 

History

History

GenerateAesKey

Cross-platform cryptography

Generate a secure random AES key

One of the most important fact for secure cryptography is to use an encryption key that is generated randomly and not by taking a passwordphrase and simply convert it to a key.

The codes will generate an AES key with size of 32 byte = 256 bit that is the maximum key size available for AES encryption.

The key output format is a byte array but when sending this key to another system or persons you often need another (string based) format - so I'm providing the key also as a Base64 encoded string.

⚠️ Security warning ⚠️

This is a serious warning regarding the security of the programs shown in these article series. Always keep in mind my disclaimer regarding my programs: All programs are for educational purposes and are not intended to use in production or any other programs where a secure solution is needed. The programs do not have proper exceptional/error handling and in some cases they use insecure key lengths or other methods that are insecure. Never ever use the programs in real life unless checked by a qualified professional cryptographer.

The following links provide the solution in code and within an online compile that runs the code.

Language available Online-compiler
Java replit.com CpcJavaGenerateRandomAesKey#Main.java
PHP replit.com CspPhpGenerateRandomAesKey
C# replit.com CpcCsharpGenerateRandomAesKey#main.cs
Javascript CryptoJs replit.com CpcCryptoJsGenerateRandomAesKey
NodeJS Crypto replit.com CpcNodeJsGenerateRandomAesKey#index.js
NodeJS forge replit.com CpcNodeJsGenerateRandomAesKey#index.js
Python replit.com CpcPythonGenerateRandomKeyIvNonce#main.py
Go *2) replit.com CpcGoGenerateRandomAesKey#Main.go
Dart *1) no online compiler available

*1) you need the external library pointycastle version 3.1.1

*2) the Go version is a combined one: generate AES key, IV and nonce in one program

This is an output (as there is a random element your output will differ):

Generate a 32 byte long AES key
generated key length: 32 base64: a9VVidAcHk6mv09zGSfH6ZiWNWSsjt/gryi6UcnMdAg=

Last update: Oct. 18th 2021

Back to the main page: readme.md