zied khmili
zied khmili

Reputation: 53

Keytool, retrieve the alias value as string from a JKS or from a PFX

I'm running a Powershell script that is updating a properties file in a java application and using Keytool to convert keys from PFX to JKS format.

I'm trying to convert a PFX to a JKS. I can't specify -"destalias" without specifying "-srcalias". The certificate being a PFX, I don't think it has an alias, but only a fingerprint.

However Keytool seems to see that the PFX has an Alias and it's using that value to auto-populate the Alias value of the JKS file.

I need the value of the Alias as a string to update my properties file.

Question:

Is there a way using Keytool or Powershell to either get the alias value from a PFX or a JKS as a String value.

Thank you!

Upvotes: 3

Views: 9186

Answers (2)

Charlieface
Charlieface

Reputation: 71579

You can use keytool -list to get the information out of the PFX, and use Select-String with a Regex expression to read it automatically from keytool.

$alias = (
        keytool -list -keystore $pfxFile -storepass $pass -v |
        Select-String -Pattern "Alias name: (.+)"
    ).Matches.Groups |
    select -Skip 1 -First 1 -ExpandProperty Value;

This looks for a string beginning Alias name: , followed by any characters in a group, and then takes the first group ignoring the outer match group.

Check $LASTEXITCODE -ne 0 to see if it failed.

Upvotes: 2

Soumen Mukherjee
Soumen Mukherjee

Reputation: 3262

You can use this command

keytool -v -list -storetype pkcs12 -keystore x.pfx

To see the Alias , generally it will be some number like 1 or 2 , you can then use this in your command for "-srcalias"

Upvotes: 3

Related Questions