当前位置:网站首页>Generate publickey with der format public key and report an error
Generate publickey with der format public key and report an error
2022-07-24 18:26:00 【Sun dried old salted fish】
Recent projects use RSA Encrypt and decrypt public and private key pairs , Encounter will der Format public key converted to PublicKey Error reporting , The exception information is as follows :
cn.hutool.crypto.CryptoException: InvalidKeySpecException: encoded key spec not recognized: unknown object in getInstance: org.bouncycastle.asn1.ASN1Integer
at cn.hutool.crypto.KeyUtil.generatePublicKey(KeyUtil.java:355)
at cn.hutool.crypto.KeyUtil.generatePublicKey(KeyUtil.java:335)
at com.unionpay.trust.crypto.util.HsmUtilTest.testGenerateRsaAndPubKeyEncrypt(HsmUtilTest.java:95)
at com.unionpay.trust.crypto.util.HsmUtilTest.main(HsmUtilTest.java:61)
Caused by: java.security.spec.InvalidKeySpecException: encoded key spec not recognized: unknown object in getInstance: org.bouncycastle.asn1.ASN1Integer
at org.bouncycastle.jcajce.provider.asymmetric.util.BaseKeyFactorySpi.engineGeneratePublic(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.rsa.KeyFactorySpi.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(KeyFactory.java:328)
at cn.hutool.crypto.KeyUtil.generatePublicKey(KeyUtil.java:353)Use der Format public key converted to PublicKey This is done as follows :
// It cannot be generated normally publickey
// PublicKey publicKey = KeyUtil.generatePublicKey("RSA", Forms.hexStringToByte(publicKeyDerHex));
// PublicKey publicKey = KeyUtil.generatePublicKey("RSA/ECB/PKCS1Padding", Forms.hexStringToByte(publicKeyDerHex));
// PublicKey publicKey = KeyUtil.generatePublicKey("RSA/ECB/NoPadding", Forms.hexStringToByte(publicKeyDerHex));
// PublicKey publicKey = KeyUtil.generatePublicKey("RSA/None/NoPadding", Forms.hexStringToByte(publicKeyDerHex));
// PublicKey publicKey = KeyUtil.generateRSAPublicKey(Forms.hexStringToByte(publicKeyDerHex));
None of the above methods can be generated normally PublicKey, The errors reported are all the above abnormal information The reasons for the error are as follows :
Generated by encryptor RSA The public key is ANS.1 Coded , The use of conversion here requires that ANS.1 Code to X509 code , The specific conversion method is as follows :
/**
* RSA Public key format conversion ANS1 turn X509
* @param encodedKey Public key
* @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static byte[] ansOneToX509(byte[] encodedKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
// call ASN1Sequence, Yes ASN1 Of RSADER Code to decode
ASN1Sequence asn1 = ASN1Sequence.getInstance(encodedKey);
Enumeration<?> e = asn1.getObjects();
BigInteger modulus = ASN1Integer.getInstance(e.nextElement()).getPositiveValue();
BigInteger publicExponent = ASN1Integer.getInstance(e.nextElement()).getPositiveValue();
// Reconstruct a x509 object , And get the encoded data
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubkey = keyFactory.generatePublic(new RSAPublicKeySpec(modulus, publicExponent));
byte[] x509Encoded = pubkey.getEncoded();
return x509Encoded;
}After the conversion , Let's talk about it again der Format conversion to PublicKey It's normal .
边栏推荐
- Install jumpserver
- Template inheritance and import
- Four ways of simple interest mode
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- 排序的几种方式for while 还有sort
- Getting started with MySQL database
- CF lomsat gelral (heuristic merge)
- 如何用WebGPU流畅渲染百万级2D物体?
- Go language interface and type
- The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
猜你喜欢

Inoic4 learning notes 2

["code" power is fully open, and "chapter" shows strength] list of contributors to the task challenge in the first quarter of 2022

【“码”力全开,“章”显实力】2022年第1季Task挑战赛贡献者榜单
![[OBS] dependency Library: x264 vs Build](/img/24/4caea5dc6ff092a3161f43b6026d25.png)
[OBS] dependency Library: x264 vs Build
![[opencv] - thresholding](/img/4e/88c8c8063de7cb10e44e76e77dbb8e.png)
[opencv] - thresholding

EasyUI adds row level buttons to the DataGrid

Emerging potential of interactive virtual reality technology in drug discovery

pycharm配置opencv库

The 5th Digital China Construction summit opened in Fuzhou, Fujian

CF. Bits And Pieces(子集状压dp + 剪枝)
随机推荐
[opencv] - thresholding
Mid year inventory | in 2022, PAAS will be upgraded again
4. Basic type and reference type?
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
mysql 配置文件
About the writing method of interface 1 chain interpretation 2. Method execution (finally) must be executed
[OBS] cooperation between video and audio coding and RTMP transmission
CF. Bits And Pieces(子集状压dp + 剪枝)
可撤销并查集板子
Baidu touch.js
Show or hide password plaintext + password box verification information
Learn redisson from scratch ------- topics (subscription and distribution)
Namespace:集群环境共享与隔离
Flink operation Hudi data table
XSS bypass pose summary
树链剖分板子
Go language file operation
8. = = and = = =?
13. What is the difference between onkeydown, up and onkeypress?
如何用WebGPU流畅渲染百万级2D物体?