Skip to content

Instantly share code, notes, and snippets.

@djangofan
Created December 20, 2012 16:10
Show Gist options
  • Save djangofan/4346246 to your computer and use it in GitHub Desktop.
Save djangofan/4346246 to your computer and use it in GitHub Desktop.
Get private key from a Java keystore file using Groovy
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-----"
@madhusudhanmodupalli
Copy link

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

@madhusudhanmodupalli
Copy link

here is the screen shot

test

@miketzian
Copy link

Worked for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment