我有一个PEM格式的公钥(从iOS设备接收):
String pemPubKey = ""+
"-----BEGIN RSA PUBLIC KEY-----\n"+
"MIIBCgKCAQEA07ACQHTTrgX7ddNtyamh58xwD+S+pSrJz/Rah4zj0HIg4V/Ok5vk\n"+
"Wx6y4UyuKLCtefeiB2ipg/n1ZZ0eRac1B4UwPhAtILGQzgIUgOp0cQ3Cb94ugq92\n"+
"wxkxeEdWmIFIlXgWOf6I8yWp9DZaigrRhA2kPbY01zKxCsX1ZxKMVu2sU/HM1hJy\n"+
"aebLLND002yLzuRDLXbacmCt5U6vDQDjBmm3uZ26fEMF+GTCnn6fJBq5RDfRKjpS\n"+
"fVM0mCePO9RHiwu3oHfqoyLA2QGlCexXcIYq7KbJjC9vcamAWRqQdHlsSj5ezDTR\n"+
"GofA6HtQ+zNdGHOvqsYtbN8MJSlUXXy39wIDAQAB\n"+
"-----END RSA PUBLIC KEY-----";
如果我尝试使用KeyFactory这样将其解析为PublicKey
:
KeyFactory kf = KeyFactory.getInstance("RSA");
Pattern parse = Pattern.compile("(?m)(?s)^---*BEGIN.*---*$(.*)^---*END.*---*$.*");
String encoded = parse.matcher(pemPubKey).replaceFirst("$1");
byte[] pem = Base64.getMimeDecoder().decode(encoded);
PublicKey pubKey = kf.generatePublic(new X509EncodedKeySpec(pem));
我得到:java.security.InvalidKeyException: IOException: algid parse error, not a sequence
。
但是当我像这样使用bouncycastle
时:
SubjectPublicKeyInfo subjectPublicKeyInfo =
(SubjectPublicKeyInfo) new PEMParser(new StringReader(pemPubKey)).readObject();
PublicKey pubKey;
if (PKCSObjectIdentifiers.rsaEncryption == subjectPublicKeyInfo.getAlgorithm().getAlgorithm()) {
DLSequence der = (DLSequence) subjectPublicKeyInfo.parsePublicKey().toASN1Primitive();
ASN1Object modulus, exponent;
modulus = (ASN1Object) der.getObjectAt(0);
exponent = (ASN1Object) der.getObjectAt(1);
RSAPublicKeySpec spec =
new RSAPublicKeySpec(new BigInteger(modulus.toString()), new BigInteger(exponent.toString()));
KeyFactory factory = KeyFactory.getInstance("RSA");
pubKey = factory.generatePublic(spec);
} else {
// Throw some exception
}
我得到一个有效的PublicKey
,并且正确识别了算法。
为什么Java的解析器在这里失败?我是否正在正确地进行从SubjectPublicKeyInfo
到PublicKey
的迁移?
更新:
我尝试使用openssl
验证密钥:
$ openssl rsa-通知PEM -pubin -in pub.pem -text -noout 无法加载公钥 140735659656136:错误:0906D06C:PEM例程:PEM_read_bio:无法启动 行:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.50.2/libressl/crypto/pem/pem_lib.c:704:预期: 公钥
然后从页眉/页脚中删除RSA
:
$ openssl rsa-通知PEM -pubin -in pub.pem -text -noout无法 加载公钥140735659656136:错误:0D0680A8:asn1编码 例程:ASN1_CHECK_TLEN:错误 标签:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.50.2/libressl/crypto/asn1/tasn_dec.c:1164: 140735659656136:错误:0D07803A:asn1编码 例程:ASN1_ITEM_EX_D2I:嵌套asn1 错误:/ BuildRoot /库/缓存/com.apple.xbs/源/libressl/libressl-22.50.2/libressl/crypto/asn1/tasn_dec.c:314:Type=X509_ALGOR 140735659656136:错误:0D08303A:asn1编码 例程:ASN1_TEMPLATE_NOEXP_D2I:嵌套asn1 错误:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.50.2/libressl/crypto/asn1/tasn_dec.c:653:Field = algor, 类型= X509_PUBKEY 140735659656136:错误:0906700D:PEM 例程:PEM_ASN1_read_bio:ASN1 lib:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.50.2/libressl/crypto/pem/pem_oth.c:84:
答案 0 :(得分:1)
Java的解析器没有失败,您的公钥不是Java的X509EncodedKeySpec
期望的已编码SubjectPublicKeyInfo结构的实例。我没有遍历Bouncycastle例程来了解它为什么成功,但是PEMParser旨在解析许多不同种类的所谓的“ PEM”文件。
在RFC 5280中将SubjectPublicKeyInfo定义为:
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING
}
您的公钥是一个简单的PKCS#1 RSAPublicKey,在RFC 8017中定义为:
RSAPublicKey ::= SEQUENCE {
modulus INTEGER, -- n
publicExponent INTEGER -- e
}
关于“ PEM”文件的最后一句话。在将某些内容描述为“ PEM”格式时,请务必小心,因为PEM并不是真正的特定格式。 “ PEM”只不过是在base64中编码真实格式,然后将base64包装在“ ----- BEGIN -----”和“ ----- END -----”行中,其中< em>希望唯一地描述了base64编码的数据。