两个实用的方法从Base64字符串生成RSAPublicKey及RSAPrivatekey

时间:2021-05-18 03:23:42
     public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(CodeUtils.base64Decode(base64s));
RSAPublicKey publicKey = null;
     KeyFactory keyFactory = KeyFactory.getInstance("RSA");
try {
publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec);
} catch (InvalidKeySpecException var4) {
LOGGER.error("base64编码=" + base64s + "转RSA公钥失败", var4);
} return publicKey;
} public static RSAPrivateKey getRSAPrivateKeyBybase64(String base64s) {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(CodeUtils.base64Decode(base64s));
RSAPrivateKey privateKey = null;
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
try {
privateKey = (RSAPrivateKey)keyFactory.generatePrivate(keySpec);
} catch (InvalidKeySpecException var4) {
LOGGER.error("base64编码=" + base64s + "转RSA私钥失败", var4);
} return privateKey;
}