Created
February 10, 2021 16:00
-
-
Save RyanGDay/9af0d0ba3bf5304167b6a2885c161b35 to your computer and use it in GitHub Desktop.
Conversion of AzurePublicKey to PublicKey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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