Skip to content

Instantly share code, notes, and snippets.

@ryctabo
Created February 25, 2018 23:28
Show Gist options
  • Save ryctabo/c8553f9da607c67a8c159e1da0509cdc to your computer and use it in GitHub Desktop.
Save ryctabo/c8553f9da607c67a8c159e1da0509cdc to your computer and use it in GitHub Desktop.
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// Clean public key information
String publicKeyContent = new String(getBytesFromUri("id_rsa.pub"));
publicKeyContent = publicKeyContent
.replace("----BEGIN RSA PUBLIC KEY----", "")
.replace("----END RSA PUBLIC KEY----", "")
.replace("\n", "");
LOG.info("Public key content: " + publicKeyContent);
// Get all bytes decoded from the public key content
byte[] publicKeyDecoded = Base64.getDecoder()
.decode(publicKeyContent);
// Generate the public key object from bytes
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyDecoded);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment