Examples
Scenario 1: Encrypt Decrypt 6 digit code using RSA Algorithm
Given
Step 1: Generate the Key Pair Using Keytool
Generate an RSA key pair and store it in a keystore.
keytool -genkeypair -alias mykey -keyalg RSA -keysize 2048 -validity 365 -keystore mykeystore.jks -storetype JKS -storepass changeit -keypass changeit -dname "CN=example.com, OU=IT, O=Example Corp, L=San Francisco, ST=CA, C=US"-genkeypair: Generates a key pair (public and private key).-alias mykey: The alias for the key pair.-keyalg RSA: The algorithm for the key.-keysize 2048: The size of the key.-validity 365: The validity of the key in days.-keystore mykeystore.jks: The keystore file to store the key pair.-storetype JKS: The type of the keystore.-storepass changeit: The password for the keystore.-keypass changeit: The password for the key.-dname: The Distinguished Name (DN) for the certificate.

Step 2: Export the Public Key
This step is optional. Instead of exporting the key, we can directly both private and public key from the keystore. See Step 4 for more details.
Export the public key to a file.
-exportcert: Exports the certificate (public key).-alias mykey: The alias of the key pair.-keystore mykeystore.jks: The keystore file.-file publickey.cer: The output file for the public key.-storepass changeit: The password for the keystore.

Step 3: Encrypt Decrypt the 6-Digit Code
Encrypt the 6-digit code using the public key and Decrypt via private key.
Step 4: Encrypt Decrypt the 6-Digit Code without using separately generated certificate key
Last updated