Examples
Scenario 1: Encrypt Decrypt 6 digit code using AES Algorithm
Given:
Step 1: Generate the Key Using keytool
keytoolGenerate the AES key and store it in a JCEKS keystore with the .jceks extension.
keytool -genseckey -alias myaeskey -keyalg AES -keysize 256 -keystore mykeystore.jceks -storetype JCEKS
Step 2: Export the Key from the Keystore
Since keytool does not directly support exporting secret keys, we need to use a Java program to export the key from the keystore.

A .key file, when it contains a symmetric key like an AES key, is typically in binary format. This binary format is not human-readable and cannot be meaningfully viewed or edited with a text editor.
Convert Binary Key to Hex and Base64 for Viewing

Step 3: Load the key and use it for encryption decryption
Method 1: Using static Initialization Vector (IV)

Usually, Base64 excrypted text is shared among applications (say Web/Mobile Application sending to Backend in an API or vice versa)
Method 2: Using dynamic Initialization Vector (IV) with key in hex format
Always ensure that the IV used for encryption is the same one used for decryption. Proper handling and storage of the IV are essential for secure cryptographic operations.
Note that default blocksize of AES is 16 byte (128 bits)

Last updated