从 DER 格式的字符串 base64 编码创建 PrivateKey 和 PublicKey
我的私钥和公钥位于 base64 的字符串中,使用 ANS1 DER 进行编码。我尝试创建 java PrivateKey
和 PublicKey
的实例:
byte [] llave2 = DatatypeConverter.parseBase64Binary(key);
PKCS8Key pkcs8 = new PKCS8Key( llave2, password.toCharArray()); //line 2
llave2 = pkcs8.getDecryptedBytes(); //line 3
certificado = DatatypeConverter.parseBase64Binary(cer);
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(llave2);
PrivateKey privateKey = kf.generatePrivate(ks);
X509EncodedKeySpec x = new X509EncodedKeySpec(certificado);
PublicKey publicKey = kf.generatePublic(x);
我在 PublicKey publicKey = kf.generatePublic(x)
中收到以下错误。
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(Unknown Source)
at vital.cancelaciones.GeneraXMLCancelacion.main(GeneraXMLCancelacion.java:118)
Caused by: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
at sun.security.x509.X509Key.decode(Unknown Source)
at sun.security.x509.X509Key.decode(Unknown Source)
at sun.security.rsa.RSAPublicKeyImpl.<init>(Unknown Source)
at sun.security.rsa.RSAKeyFactory.generatePublic(Unknown Source)
... 3 more
我想我应该对公钥执行类似于第 2 行和第 3 行中的私钥执行的操作。因为证书也是加密的。有什么建议吗?
I have my Private and Public keys in a String in base64 which where encoded using ANS1 DER. I tried creating the instance of a java PrivateKey
and PublicKey
:
byte [] llave2 = DatatypeConverter.parseBase64Binary(key);
PKCS8Key pkcs8 = new PKCS8Key( llave2, password.toCharArray()); //line 2
llave2 = pkcs8.getDecryptedBytes(); //line 3
certificado = DatatypeConverter.parseBase64Binary(cer);
KeyFactory kf = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(llave2);
PrivateKey privateKey = kf.generatePrivate(ks);
X509EncodedKeySpec x = new X509EncodedKeySpec(certificado);
PublicKey publicKey = kf.generatePublic(x);
I get the following error in PublicKey publicKey = kf.generatePublic(x)
.
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(Unknown Source)
at java.security.KeyFactory.generatePublic(Unknown Source)
at vital.cancelaciones.GeneraXMLCancelacion.main(GeneraXMLCancelacion.java:118)
Caused by: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)
at sun.security.x509.X509Key.decode(Unknown Source)
at sun.security.x509.X509Key.decode(Unknown Source)
at sun.security.rsa.RSAPublicKeyImpl.<init>(Unknown Source)
at sun.security.rsa.RSAKeyFactory.generatePublic(Unknown Source)
... 3 more
I guess I should do something similar with the public key as done with the private key in lines 2 and 3. Because the certificate is also encrypted. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了测试您的场景,我使用
openssl
创建了一个 RSA 私钥。然后我将此密钥转换为 PKCS#8 DER 格式。
openssl
手册将 PKCS#8 和 DER 都称为格式,因此就我而言,会发生以下情况:pkcs8
告诉openssl
我想使用 PKCS#8 格式的私钥。-topk8
告诉它我要使用-in
指定的私钥不是在 PKCS#8 中(否则它会假设这是)。-inform
和-in
指定我想要将 (PEM) 私钥转换为 PKCS#8(没有-topk8
它会尝试将已采用 PKCS#8 格式的密钥转换为标准密钥格式)。-outform
和-out
告诉它我想要 DER 格式的密钥作为输出。-nocrypt
告诉它我不想加密密钥。然后,使用我的 RSA 密钥(标准格式)创建了一个证书。
该证书包含与我的私钥对应的公钥。
在完成所有这些操作之后,我已经使用 Base64 对私钥和证书进行了编码。
生成了以下文件。
主要问题是您拥有证书而不是公钥。证书包含公钥,但无法使用
X509EncodedKeySpec(...)
加载,这就是必须使用CertificateFactory
的原因。(顺便说一句,这里是一篇关于< code>openssl 和 Java 加密的使用情况。)
To test your scenario, I've created an RSA private key with
openssl
.Then I've converted this key to PKCS#8 DER format.
The manual of
openssl
refers to PKCS#8 and DER both as formats, so as far as I'm concerned the following happens:pkcs8
tellsopenssl
that I want to work with private keys in PKCS#8 format.-topk8
tells it that the private key I'm going to specify with-in
is not in PKCS#8 (otherwise it'll assume it is).-inform
and-in
specify that I want to convert the (PEM) private key to PKCS#8 (without-topk8
it'll try to convert a key already in PKCS#8 format to a standard key format).-outform
and-out
tells it I want a DER formatted key as output.-nocrypt
tells it that I don't want to encrypt the key.Then, with my RSA key (in standard format) I've created a certificate.
The certificate contains the public key corresponding to my private key.
After all of these, I've encoded both the private key and the certificate with Base64.
The following files were generated.
The main problem was that you had a certificate instead of a public key. The certificate contains the public key, but it cannot be loaded with
X509EncodedKeySpec(...)
, this is why theCertificateFactory
has to be used instead.(By the way here is a great article/tutorial on
openssl
and Java cryptography usage. I've got my info partly from there.)