Skip to content

Instantly share code, notes, and snippets.

@RyanGDay
Created February 10, 2021 16:00
Show Gist options
  • Save RyanGDay/9af0d0ba3bf5304167b6a2885c161b35 to your computer and use it in GitHub Desktop.
Save RyanGDay/9af0d0ba3bf5304167b6a2885c161b35 to your computer and use it in GitHub Desktop.
Conversion of AzurePublicKey to PublicKey
private PublicKey convertKey(AzurePublicKey azKey) {
PublicKey publicKey = null;
BigInteger modulus = new BigInteger(1, Base64.getUrlDecoder().decode(azKey.getN()));
BigInteger exponent = new BigInteger(1, Base64.getUrlDecoder().decode(azKey.getE()));
try {
publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent));
// load cert
CertificateFactory factory;
X509Certificate cert = null;
try {
factory = CertificateFactory.getInstance("X.509");
cert = (X509Certificate) factory.generateCertificate(new ByteArrayInputStream(DatatypeConverter.parseBase64Binary(azKey.getX5c().iterator().next())));
} catch (CertificateException e) {
e.printStackTrace();
}
// grab public key
publicKey = (RSAPublicKey)cert.getPublicKey();
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return publicKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment