Examples

Scenario 1: Encrypt Decrypt 6 digit code using AES Algorithm

Given:

Inputs given to us ->

Algorithm for the key: AES

Key size: 256

Keystore type: JCEKS

Keystore Password: changeit

Key Password: changeit

Mode of operation: CBC (Cipher Block Chaining)

Padding Scheme: PKCS7Padding

Cryptographic Provider: BC (BouncyCastle)

Step 1: Generate the Key Using keytool

Generate 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)

Method 2: Using dynamic Initialization Vector (IV) with key in hex format

Last updated