简体   繁体   English

为什么Bouncycastle的PEMReader无法读取此M2Crypto生成的PEM?

[英]Why can't Bouncycastle's PEMReader read this M2Crypto-generated PEM?

I use the following Python code, using M2Crypto, to generate an RSA key pair in PEM format: 我使用M2Crypto使用以下Python代码生成PEM格式的RSA密钥对:

bio = BIO.MemoryBuffer()
key_pair = RSA.gen_key(1024, 65537)
key_pair.save_key_bio(bio, cipher=None)
return bio.read()

And the following Java code, using Bouncycastle, to try to read it: 下面的Java代码使用Bouncycastle尝试读取它:

String signPem = ...;
PEMReader pemReader = new PEMReader(new StringReader(signPem));
Object signingKey = pemReader.readObject();

The signPem string is the same as what bio.read() returned; signPem字符串与bio.read()返回的字符串相同; no data that I can see is getting munged between the two programs; 在两个程序之间,我看不到任何数据混杂; it's -----BEGIN RSA PRIVATE KEY-----\\n etc. -----BEGIN RSA PRIVATE KEY-----\\n等。

However, the readObject() call throws a ClassCastException: 但是,readObject()调用将引发ClassCastException:

java.lang.ClassCastException: org.bouncycastle.asn1.DERSequence
    at org.bouncycastle.asn1.ASN1Object.fromByteArray(Unknown Source)
    at org.bouncycastle.openssl.PEMReader.readKeyPair(Unknown Source)
    at org.bouncycastle.openssl.PEMReader.readObject(Unknown Source)

So obviously BC misidentifies the data in the PEM is not a key pair for some reason - but why? 因此,很明显BC由于某种原因误认为PEM中的数据不是密钥对-但是为什么呢?

After taking a debugger to Bouncycastle, I have discovered the "cause", but it only puzzles me more; 将调试器带到Bouncycastle之后,我发现了“原因”,但这只会使我感到困惑。 the cast is failing even though all types seem to be correct. 即使所有类型似乎都是正确的,强制转换也会失败。

If A extends B extends C, why can I cast to A but get a ClassCastException casting to C? 如果A扩展了B扩展了C,为什么我可以强制转换为A但将ClassCastException强制强制转换为C?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.bouncycastle.openssl.PEMReader可以读取java.security.PrivateKey吗? - Can org.bouncycastle.openssl.PEMReader read java.security.PrivateKey? 无法从.pem文件中读取DSA密钥 - Can't get read DSA keys from .pem files RSA - bouncycastle PEMReader返回PEMKeyPair而不是AsymmetricCipherKeyPair来读取私钥 - RSA - bouncycastle PEMReader returning PEMKeyPair instead of AsymmetricCipherKeyPair for reading private key 无法以 PEM 文件格式写入使用 org.bouncycastle.asn1.pkcs.CertificationRequest 生成的 CSR - Unable to write CSR generated using org.bouncycastle.asn1.pkcs.CertificationRequest in PEM file format RSA-在Bouncycastle的PEM解析器成功的地方,Java的KeyFactory失败 - RSA - Java's KeyFactory fails where bouncycastle's PEM parser succeeds 如何使用BouncyCastle通过Java中的PEMParser读取没有BEGIN和END的PEM证书 - How to read a PEM certificate without BEGIN and END via PEMParser in Java using BouncyCastle 为什么我的Java程序不能读取Perl的STDERR? - Why can't my Java program read Perl's STDERR? 无法使用BouncyCastle重现openssl命令 - Can't reproduce openssl command with BouncyCastle 读取Java中生成的OpenSSL,PEM / DER格式的S / MIME消息 - Reading OpenSSL generated, PEM/DER formatted, S/MIME messages in Java 使用 BouncyCastle 从 PEM Key 获取 KeyPair - Get KeyPair from PEM Key with BouncyCastle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM