1

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!

zied khmili
  • 23
  • 1
  • 4

2 Answers2

1

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"

Soumen Mukherjee
  • 2,953
  • 3
  • 22
  • 34
  • 1
    Thank you for your answer! But I need everything to be run by the script. Your solution involves me copying manually the alias value. – zied khmili Sep 25 '18 at 14:13
  • You can run the command within the script and parse out the value , see this stackoverflow question for more info https://stackoverflow.com/questions/13334422/pfx-to-jks-keytool-conversion-alias-does-not-exist – Soumen Mukherjee Sep 25 '18 at 14:16
1

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.

Charlieface
  • 52,284
  • 6
  • 19
  • 43