当前位置:网站首页>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 .
边栏推荐
- 数组对象方法 常用遍历方法&高阶函数
- Wu Enda writes: how to establish projects to adapt to AI career
- Simulation implementation vector
- Typora 它依然是我心中的YYDS 最优美也是颜值最高的文档编辑神器 相信你永远不会抛弃它
- Sword finger offer 21. adjust the array order so that odd numbers precede even numbers
- Growth of operation and maintenance Xiaobai - week 8 of Architecture
- JS to achieve progress steps (small exercise)
- Ionic4 learning notes 12 - a east project grid completes the list of goods
- JMeter -- prometheus+grafana server performance visualization
- XSS绕过姿势总结
猜你喜欢

Flink operation Hudi data table

如何用WebGPU流畅渲染百万级2D物体?

Ionic4 learning notes 5-- custom public module

Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures

L4L7负载均衡

Handwritten blog platform ~ the next day

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Oracle EBS form common objects and their relationships

Mysql——》BufferPool相关信息

EasyUI framework dialog repeated loading problem
随机推荐
The collapse of margin
Ionic4 learning notes 12 - a east project grid completes the list of goods
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Go language file operation
Template syntax [easy to understand]
文件上传漏洞——.user.ini与.htaccess
Growth of operation and maintenance Xiaobai - week 8 of Architecture
Show or hide password plaintext + password box verification information
undefined reference to H5PTopen
[record of question brushing] 20. Valid brackets
【obs】视频、音频编码与rtmp发送的配合
Inoic4 learning notes 2
Framework introduction
Custom web framework
EasyUI adds row level buttons to the DataGrid
Namespace: cluster environment sharing and isolation
How to quickly upload files to Google Lab
ORM introduction and database operation
颜色的13 个必备方法!
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面