Some AES encryption modes need a random generated nonce. The codes will generate an nonce with size of 12 byte = 96 bit. The nonce is especially used for AES in mode GCM programs.
The nonce output format is a byte array but when sending this nonce to another system or persons you often need another (string based) format - so I'm providing the nonce also as a Base64 encoded string.
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 CpcJavaGenerateRandomNonce#Main.java |
PHP | ✅ | replit.com CpcPhpGenerateNonce#main.php |
C# | ✅ | replit.com CpcCsharpGenerateRandomNonce#main.cs |
Javascript CryptoJs | ✅ | replit.com CpcCryptoJsGenerateRandomNonce |
NodeJS Crypto | ✅ | replit.com CpcNodeJsCryptoGenerateRandomNonce |
NodeJS forge | ✅ | replit.com CpcNodeJsGenerateRandomNonce |
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 12 byte long nonce for AES GCM
generated nonce length: 12 base64: iK+wXGH26uFN+XFX
Last update: Oct. 18th 2021
Back to the main page: readme.md