兩個實用的方法從Base64字串生成RSAPublicKey及RSAPrivatekey

FrankYou發表於2015-07-21
 1     public static RSAPublicKey getRSAPublidKeyBybase64(String base64s) {
 2         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(CodeUtils.base64Decode(base64s));
 3         RSAPublicKey publicKey = null;
 4 
 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 
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     }

 

相關文章