Skip to content

Generate A Public And Private Key Pair

There are multiple ways to generate keys and certificates. Below are three standard methods: RSA, EC (Elliptic Curve), and Java keytool (for Java environments).

Cactus Custody strongly recommends that all public and private keys be generated on the server side and securely assigned to users, in order to ensure the highest level of security.


Generate RSA private key and self-signed certificate

Generate a 4096-bit RSA private key along with a self-signed certificate valid for 365 days.

Generate RSA private key + self-signed cert
openssl req -x509 -nodes -days 365 -newkey rsa:4096 \
  -keyout <PRIVATE_KEY_FILENAME>.key \
  -out <CERT_FILENAME>.crt \
  -subj "/C=<COUNTRY>/ST=<STATE>/L=<CITY>/O=<ORG>/OU=<DEPT>/CN=<DOMAIN>"

Parameters to replace:

  • <PRIVATE_KEY_FILENAME> → e.g., server
  • <CERT_FILENAME> → e.g., server
  • /C=<COUNTRY> → 2-letter country code (e.g., SG)
  • /ST=<STATE> → State/Province (e.g., Singapore)
  • /L=<CITY> → City (e.g., Singapore)
  • /O=<ORG> → Organization name (e.g., Example Inc)
  • /OU=<DEPT> → Department (e.g., IT)
  • /CN=<DOMAIN> → Common Name/Domain (e.g., example.com)

The country code in /C=... must be exactly 2 letters.

Export into PKCS#12 keystore

Convert your private key and certificate into a PKCS#12 keystore format.

Export to PKCS#12
openssl pkcs12 -export \
  -inkey <PRIVATE_KEY_FILENAME>.key \
  -in <CERT_FILENAME>.crt \
  -out <KEYSTORE_FILENAME>.p12 \
  -name <ALIAS>

Parameters to replace:

  • <PRIVATE_KEY_FILENAME> → Same as Step 1 (e.g., server)
  • <CERT_FILENAME> → Same as Step 1 (e.g., server)
  • <KEYSTORE_FILENAME> → e.g., keystore
  • <ALIAS> → Keystore alias (e.g., mykey_rsa)

Extract the public key

Extract the public key from the certificate in PEM format.

Extract public key (PEM)
openssl x509 -in <CERT_FILENAME>.crt -pubkey -noout > <CERT_FILENAME>.pub.pem

Parameters to replace:

  • <CERT_FILENAME> → Same as previous steps (e.g., server)

Generate EC private key

Generate an Elliptic Curve private key using the prime256v1 curve.

Generate EC private key
openssl ecparam -name prime256v1 -genkey -noout -out <PRIVATE_KEY_FILENAME>.key

Parameters to replace:

  • <PRIVATE_KEY_FILENAME> → e.g., server_ec

Generate Certificate Signing Request (CSR)

Create a CSR using your EC private key.

Generate CSR
openssl req -new -key <PRIVATE_KEY_FILENAME>.key -out <CSR_FILENAME>.csr \
  -subj "/C=<COUNTRY>/ST=<STATE>/L=<CITY>/O=<ORG>/OU=<DEPT>/CN=<DOMAIN>"

Parameters to replace:

  • <PRIVATE_KEY_FILENAME> → Same as Step 1 (e.g., server_ec)
  • <CSR_FILENAME> → e.g., server_ec
  • /C=<COUNTRY> → 2-letter country code (e.g., SG)
  • /ST=<STATE> → State/Province (e.g., Singapore)
  • /L=<CITY> → City (e.g., Singapore)
  • /O=<ORG> → Organization name (e.g., Example Inc)
  • /OU=<DEPT> → Department (e.g., IT)
  • /CN=<DOMAIN> → Common Name/Domain (e.g., example.com)

The country code in /C=... must be exactly 2 letters.

Generate self-signed certificate

Create a self-signed certificate valid for 365 days.

Generate self-signed cert from CSR
openssl x509 -req -in <CSR_FILENAME>.csr -signkey <PRIVATE_KEY_FILENAME>.key \
  -out <CERT_FILENAME>.crt -days 365

Parameters to replace:

  • <CSR_FILENAME> → Same as Step 2 (e.g., server_ec)
  • <PRIVATE_KEY_FILENAME> → Same as Step 1 (e.g., server_ec)
  • <CERT_FILENAME> → e.g., server_ec

Export into PKCS#12 keystore

Convert your private key and certificate into a PKCS#12 keystore format.

Export EC to PKCS#12
openssl pkcs12 -export \
  -inkey <PRIVATE_KEY_FILENAME>.key \
  -in <CERT_FILENAME>.crt \
  -out <KEYSTORE_FILENAME>.p12 \
  -name <ALIAS>

Parameters to replace:

  • <PRIVATE_KEY_FILENAME> → Same as Step 1 (e.g., server_ec)
  • <CERT_FILENAME> → Same as Step 3 (e.g., server_ec)
  • <KEYSTORE_FILENAME> → e.g., keystore_ec
  • <ALIAS> → Keystore alias (e.g., mykey_ec)

Extract the public key

Extract the public key from the certificate in PEM format.

Extract public key (PEM)
openssl x509 -in <CERT_FILENAME>.crt -pubkey -noout > <CERT_FILENAME>.pub.pem

Parameters to replace:

  • <CERT_FILENAME> → Same as Step 3 (e.g., server_ec)

This method is specifically for Java environments and creates a JKS (Java KeyStore) file.

Generate keystore with private key

Create a Java KeyStore (JKS) file containing an EC private key.

Generate keystore (keytool)
keytool -genkey -v \
  -alias <YOUR_ALIAS> \
  -keyalg EC \
  -sigalg SHA256withECDSA \
  -deststoretype pkcs12 \
  -keystore <FILE_NAME>.jks \
  -dname "C=<COUNTRY>,ST=<STATE>,L=<CITY>,O=<ORG>,OU=<DEPT>,CN=<DOMAIN>" \
  -validity 365 \
  -storepass <STOREPASS_PASSWORD> \
  -keypass <KEYPASS_PASSWORD>

Parameters to replace:

  • <YOUR_ALIAS> → Keystore alias (e.g., myapikey)
  • <FILE_NAME> → Base filename without extension (e.g., keystore)
  • <STOREPASS_PASSWORD> → Password to protect the keystore file (ASCII only)
  • <KEYPASS_PASSWORD> → Password for the private key (ASCII only)
  • C=<COUNTRY> → 2-letter country code (e.g., SG)
  • ST=<STATE> → State/Province (e.g., Singapore)
  • L=<CITY> → City (e.g., Singapore)
  • O=<ORG> → Organization name (e.g., Example Inc)
  • OU=<DEPT> → Department (e.g., IT)
  • CN=<DOMAIN> → Common Name/Domain (e.g., example.com)

The country code in C=... must be exactly 2 letters. Passwords must use ASCII characters only.

Export the certificate

Export the certificate from the keystore to a file.

Export cert (keytool)
keytool -export -alias <YOUR_ALIAS> -keystore <FILE_NAME>.jks \
  -storepass <STOREPASS_PASSWORD> -rfc -file <FILE_NAME>.cer

Parameters to replace:

  • <YOUR_ALIAS> → Same as Step 1 (e.g., myapikey)
  • <FILE_NAME> → Same as Step 1 (e.g., keystore)
  • <STOREPASS_PASSWORD> → Same as Step 1

Extract the public key

Extract the public key from the certificate in PEM format.

Extract public key from .cer (PEM)
openssl x509 -in <FILE_NAME>.cer -pubkey -noout > <FILE_NAME>.pub.pem

Parameters to replace:

  • <FILE_NAME> → Same as previous steps (e.g., keystore)