I uploaded a certificate to the Azure KeyVault in pfx format, with a password and I want to use it in my java application. Here is how I download the certificate:
CertificateClient certificateClient = new CertificateClientBuilder()
.vaultUrl("<kv-url>")
.credential(new DefaultAzureCredentialBuilder()
.authorityHost("https://login.microsoftonline.com/")
.build())
.buildClient();
KeyVaultCertificateWithPolicy certificate = certificateClient
.getCertificate("<cert-name>");.
I want to convert my certificate back to pfx, but it downloads only in CER format.
In the portal there are two possibilities:
If I download in CER format then I get back the exact string if I would do the following in java:
String s = DatatypeConverter.printBase64Binary(certificate.getCer());
System.out.println(s); // this gives me back the same as download CER
but... I need the pfx format, which is available in the portal but I don't see any options in java.
I also tried to convert the downloaded CER to PFX like this:
Base64.Decoder decoder = Base64.getDecoder();
byte[] decode = decoder.decode(s);
InputStream inputStream = new ByteArrayInputStream(decode);
try {
KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(inputStream,"<password>".toCharArray());
} catch (CertificateException | KeyStoreException | IOException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
but then I get the following exception:
Exception in thread "main" java.lang.RuntimeException: java.io.IOException: DER input, Integer tag error
at main.Application.main(Application.java:57)
Caused by: java.io.IOException: DER input, Integer tag error
at sun.security.util.DerInputStream.getInteger(DerInputStream.java:195)
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1948)
at java.security.KeyStore.load(KeyStore.java:1445)
at main.Application.main(Application.java:54)
Can you please help me either a way to download directly in pfx format from the KeyVault or to convert the CER to PFX?