Created
December 20, 2012 16:10
-
-
Save djangofan/4346246 to your computer and use it in GitHub Desktop.
Get private key from a Java keystore file using Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.Key | |
import java.security.KeyStore | |
if (args.length < 3) | |
throw new IllegalArgumentException('Expected args: <Keystore file> <Keystore format> <Keystore password> <alias> <key password>') | |
def keystoreName = args[0] | |
def keystoreFormat = args[1] | |
def keystorePassword = args[2] | |
def alias = args[3] | |
def keyPassword = args[4] | |
def keystore = KeyStore.getInstance(keystoreFormat) | |
keystore.load(new FileInputStream(keystoreName), keystorePassword.toCharArray()) | |
def key = keystore.getKey(alias, keyPassword.toCharArray()) | |
println "-----BEGIN PRIVATE KEY-----" | |
println key.getEncoded().encodeBase64() | |
println "-----END PRIVATE KEY-----" |
Worked for me, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getting null point exception when i ran the below code in groovy script
import java.security.Key
import java.security.KeyStore
def keystorePassword ="XXXXXXXX"
def keystoreName = "C:\Users\mmodupalli\Desktop\qeee.jks"
def keystore = KeyStore.getInstance("JKS")
keystore.load(new FileInputStream(keystoreName), keystorePassword.toCharArray())
def key = keystore.getKey("testhie.itsguardian.com", keystorePassword.toCharArray())
log.info key.getEncoded().encodeBase64()