Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Latest commit

History

History
40 lines (27 loc) * 3.33 KB

generate_initvector.md

File metadata and controls

40 lines (27 loc) * 3.33 KB

Cross-platform cryptography

Generate a secure random initialization vector (IV)

Some AES encryption modes (e.g. CBC) need a random generated initialization vector (iv). The codes will generate an iv with size of 16 byte = 128 bit.

The iv output format is a byte array but when sending this iv to another system or persons you often need another (string based) format - so I'm providing the iv 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 CpcJavaGenerateRandomInitvector#Main.java
PHP replit.com CpcPhpGenerateInitvector#main.php
C# replit.com CpcCsharpGenerateRandomInitvector#main.cs
Javascript CryptoJs replit.com CpcCryptoJsGenerateRandomInitvector
NodeJS CryptoJs replit.com CpcNodeJsGenerateRandomInitvector
NodeJS forge replit.com CpcNodeJsGenerateRandomInitvector
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 16 byte long Initialization vector (IV)
generated iv length: 16 base64: PnujuygIgaMxwdQRWz9wvA==

Last update: Oct. 18th 2021

Back to the main page: readme.md