为什么 Bouncycastle 的 PEMReader 无法读取这个 M2Crypto 生成的 PEM?
我使用以下 Python 代码(使用 M2Crypto)生成 PEM 格式的 RSA 密钥对:
bio = BIO.MemoryBuffer()
key_pair = RSA.gen_key(1024, 65537)
key_pair.save_key_bio(bio, cipher=None)
return bio.read()
以及以下 Java 代码(使用 Bouncycastle)尝试读取它:
String signPem = ...;
PEMReader pemReader = new PEMReader(new StringReader(signPem));
Object signingKey = pemReader.readObject();
signPem
字符串与bio.read()
返回;我看到两个程序之间没有数据被破坏;它是 -----BEGIN RSA PRIVATE KEY-----\n
等。
但是,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)
所以显然 BC 错误地识别了 PEM 中的数据不是密钥出于某种原因配对 - 但为什么呢?
I use the following Python code, using M2Crypto, to generate an RSA key pair in PEM format:
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:
String signPem = ...;
PEMReader pemReader = new PEMReader(new StringReader(signPem));
Object signingKey = pemReader.readObject();
The signPem
string is the same as what bio.read()
returned; no data that I can see is getting munged between the two programs; it's -----BEGIN RSA PRIVATE KEY-----\n
etc.
However, the readObject() call throws a 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在使用 Bouncycastle 进行调试后,我发现了“原因”,但这只会让我更加困惑;尽管所有类型似乎都是正确的,但演员阵容还是失败了。
如果 A 扩展 B 扩展 C,为什么我可以强制转换为 A,但会得到 ClassCastException 强制转换为 C?
After taking a debugger to Bouncycastle, I have discovered the "cause", but it only puzzles me more; 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?