[英]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.