当前位置:网站首页>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 .
边栏推荐
- Framework introduction
- 第五届数字中国建设峰会在福建福州开幕
- Template syntax [easy to understand]
- Mid year inventory | in 2022, PAAS will be upgraded again
- 【校验】只能输入数字(正负数)
- QT—动画框架
- Problems needing attention in writing pages
- Mysql——》数据类型隐式转换
- 关于接口的写法 1链式判读 ?. 2方法执行 (finally)一定会执行
- Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it
猜你喜欢

根证书的有效期与服务器SSL证书一样长吗?

Mozilla foundation released 2022 Internet health report: AI will contribute 15.7 trillion yuan to the global economy in 2030, and the investment in AI in the United States last year was nearly three t

开窗函数(1)-部门工资前三员工

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery

6126. 设计食物评分系统

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

Mysql——》BufferPool相关信息

第五届数字中国建设峰会在福建福州开幕

缺失值处理

6126. Design food scoring system
随机推荐
数组常用方法(2)
Typora is still the most beautiful and beautiful document editing artifact of yyds in my heart. I believe you will never abandon it
球面上绘制圆matlab仿真
Baidu touch.js
L4L7负载均衡
无关的表进行关联查询及null=null条件
Windowing function (1) - top three employees of department salary
Custom web framework
奶头乐理论介绍及个人感悟
The difference between KIB and MIB and KB and MB
5. Reference type and value type as function parameters?
pycharm配置opencv库
CF. Bits And Pieces(子集状压dp + 剪枝)
Install jumpserver
13. What is the difference between onkeydown, up and onkeypress?
Flink operation Hudi data table
CF Lomsat gelral(启发式合并)
The drop-down list component uses iscrol JS to achieve the rolling effect of the pit encountered
Show or hide password plaintext + password box verification information
Getting started with MySQL database