问题
加密/解密
# 私钥转报algid parse error, not a sequence
代码:
PrivateKey getPrivateKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] keyBytes = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
1
2
3
4
5
6
2
3
4
5
6
错误:
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException : algid parse error, not a sequence
at sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:217)
at java.security.KeyFactory.generatePrivate(KeyFactory.java:372)
1
2
3
2
3
解决:
PrivateKey getPrivateKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] keyBytes = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
1
2
3
4
5
6
7
2
3
4
5
6
7
参考:
https://blog.csdn.net/ls0111/article/details/77533768 (opens new window)
https://blog.csdn.net/qq_29583513/article/details/78866461 (opens new window)
https://www.cnblogs.com/yszr/p/8215075.html (opens new window)