Skip to content

Instantly share code, notes, and snippets.

@msauza
Last active July 26, 2017 22:13
Show Gist options
  • Save msauza/056070d555d8f1f5d1e480f99e012413 to your computer and use it in GitHub Desktop.
Save msauza/056070d555d8f1f5d1e480f99e012413 to your computer and use it in GitHub Desktop.
Key Store, Certificate
### Create keystore
keytool -genkey -alias ALIAS_NAME -keypass nosecret -keyalg RSA -keystore KEYSTORE_NAME.jks -storepass nostoresecret
### Show Alias
keytool -list -v -keystore KEYSTORE_NAME.jks
## Delete alias
keytool -delete -alias ALIAS_NAME -keystore KEYSTORE_NAME.jks -storepass nostoresecret
### Generate certificate file
keytool -export -alias ALIAS_NAME -file CERTIFICATE_NAME.cer -keystore KEYSTORE_NAME.jks
### Go to
C:%JAVA_HOME%/jre/lib/security
### Option 1 - Import certificate from keystore
keytool -import -alias alias_name -file certificate_name -keystore keystore location
### Option 2 - Import certificate
keytool -import -keystore cacerts -file test.cer
### Generate Public Key
keytool -list -rfc --keystore jwt-test.jks | openssl x509 -inform pem -pubkey
### Clone certificate info: new alias, new password
keytool -keyclone -alias CURRENT_ALIAS_NAME -dest NEW_ALIAS_NAME -keypass currentsecret -new newsecret -keystore KEYSTORE_NAME.jks -storepass currentstoresecret
### Since Java 6, you can import/export private keys into PKCS#12 (.p12)
### The PKCS12 keystore type is supported as a standard keystore type in the default Oracle/Sun security provider.
keytool -importkeystore -srcalias ALIAS_NAME -srckeystore KEYSTORE_NAME.jks -destkeystore NEW_KEYSTORE_NAME.p12 -deststoretype PKCS12 -deststorepass newstoresecret -destkeypass newsecret
### Export certificate using openssl
openssl pkcs12 -in KEYSTORE_NAME.p12 -nokeys -out cert.pem
### Export unencrypted private key
openssl pkcs12 -in KEYSTORE_NAME.p12 -nodes -nocerts -out key.pem
### More info
https://docs.oracle.com/javase/8/docs/technotes/tools/unix/keytool.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment