两个实用的方法从Base64字符串生成RSAPublicKey及RSAPrivatekey
1 public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) { 2 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(CodeUtils.base64Decode(base64s)); 3 RSAPublicKey publicKey = null; 4 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 5 try { 6 publicKey = (RSAPublicKey)keyFactory.generatePublic(keySpec); 7 } catch (InvalidKeySpecException var4) { 8 LOGGER.error("base64编码=" + base64s + "转RSA公钥失败", var4); 9 } 10 11 return publicKey; 12 } 13 14 public static RSAPrivateKey getRSAPrivateKeyBybase64(String base64s) { 15 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(CodeUtils.base64Decode(base64s)); 16 RSAPrivateKey privateKey = null; 17 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 18 try { 19 privateKey = (RSAPrivateKey)keyFactory.generatePrivate(keySpec); 20 } catch (InvalidKeySpecException var4) { 21 LOGGER.error("base64编码=" + base64s + "转RSA私钥失败", var4); 22 } 23 24 return privateKey; 25 }