Skip to content

[QUERY] How to fetch the private key from Azure Keyvault Secret and convert it to a "java.security.PrivateKey" Java object? #39756

Open
@vijayaraghavan-tvr

Description

@vijayaraghavan-tvr

Query/Question
How to fetch the private key from Azure Keyvault Secret and convert it to a "java.security.PrivateKey" Java object? I've tried a sample code as follows, but, it was throwing an exception. Could you please help?

KeyVaultSecret secret = secretClient.getSecret(name, version);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(secret.getValue())));

It threw

Caused by: java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: unknown version: 3
at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:253)
at java.base/java.security.KeyFactory.generatePrivate(KeyFactory.java:389)

Why is this not a Bug or a feature Request?
I just want to understand how to convert the private key value from the Keyvault secret to a "java.security.PrivateKey" Java object. It's not a bug or a feature request.

Setup (please complete the following information if applicable):
I'm using

  • Azure Keyvault SDK (v 1.2.22) to fetch the secret.
  • Java 17

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • [y] Query Added
  • [y] Setup information Added

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.KeyVaultcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

Status

Not Started

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions