Created
August 8, 2018 01:20
-
-
Save vicly/e2041337a9f973b588cdfec42bae30d7 to your computer and use it in GitHub Desktop.
[read public key from PEM string] #Java
This file contains hidden or 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
import java.security.*; | |
import java.security.spec.*; | |
public static java.security.Key publicKeyFromPEMString() | |
{ | |
String devPublicKey = "-----BEGIN PUBLIC KEY-----\n" | |
+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsXn7MZWEOr1egercQtIk\n" | |
+ "......\n" | |
+ "......\n" | |
+ "......\n" | |
+ "nQIDAQAB\n" | |
+ "-----END PUBLIC KEY-----\n"; | |
devPublicKey = devPublicKey | |
.replaceAll("\\n", "") | |
.replace("-----BEGIN PUBLIC KEY-----", "") | |
.replace("-----END PUBLIC KEY-----", "");; | |
try | |
{ | |
X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(Base64.getDecoder().decode(devPublicKey)); | |
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); | |
return keyFactory.generatePublic(publicSpec); | |
} | |
catch (NoSuchAlgorithmException e) | |
{ | |
throw new UnsupportedJwtException("Cannot handle algorithm RSA", e); | |
} | |
catch (InvalidKeySpecException e) | |
{ | |
throw new UnsupportedJwtException("Invalid public key", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment