java 将字节数组转换为 X.509 证书
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2534127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a byte array to a X.509 certificate
提问by ddd
I'm trying to port a piece of Java code into .NET that takes a Base64 encoded string, converts it to a byte array, and then uses it to make a X.509 certificate to get the modulus & exponent for RSA encryption.
我正在尝试将一段 Java 代码移植到 .NET 中,该代码采用 Base64 编码的字符串,将其转换为字节数组,然后使用它来制作 X.509 证书以获得 RSA 加密的模数和指数。
This is the Java code I'm trying to convert:
这是我要转换的 Java 代码:
byte[] externalPublicKey = Base64.decode("base 64 encoded string");
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(externalPublicKey);
Key publicKey = keyFactory.generatePublic(publicKeySpec);
RSAPublicKey pbrtk = (java.security.interfaces.RSAPublicKey) publicKey;
BigInteger modulus = pbrtk.getModulus();
BigInteger pubExp = pbrtk.getPublicExponent();
I've been trying to figure out the best way to convert this into .NET. So far, I've come up with this:
我一直试图找出将其转换为 .NET 的最佳方法。到目前为止,我想出了这个:
byte[] bytes = Convert.FromBase64String("base 64 encoded string");
X509Certificate2 x509 = new X509Certificate2(bytes);
RSA rsa = (RSA)x509.PrivateKey;
RSAParameters rsaParams = rsa.ExportParameters(false);
byte[] modulus = rsaParams.Modulus;
byte[] exponent = rsaParams.Exponent;
Which to me looks like it should work, but it throws a CryptographicExceptionwhen I use the base 64 encoded string from the Java code to generate the X509 certificate. The exact message I receive is:
在我看来它应该可以工作,但是当我使用 Java 代码中的 base 64 编码字符串生成 X509 证书时,它会抛出CryptographicException。我收到的确切消息是:
Cannot find the requested object.
找不到请求的对象。
Is Java's X.509 implementation just incompatible with .NET's, or am I doing something wrong in my conversion from Java to .NET?
Java 的 X.509 实现是否与 .NET 不兼容,或者我在从 Java 到 .NET 的转换中做错了什么?
Or is there simply no conversion from Java to .NET in this case?
或者在这种情况下根本没有从 Java 到 .NET 的转换?
回答by dtb
It seems your base64-encoded data does not represent an X.509 certificate:
您的 base64 编码数据似乎并不代表 X.509 证书:
[The X509EncodedKeySpec class] represents the ASN.1 encoding of a public key
[ X509EncodedKeySpec 类] 表示公钥的 ASN.1 编码
Export the whole X.509 certificate in Java, or try to find an equivalent of the X509EncodedKeySpec class in the .NET framework.
用 Java 导出整个 X.509 证书,或尝试在 .NET 框架中找到 X509EncodedKeySpec 类的等效项。
回答by laher
I have encountered a similar issue, and in my case it boiled down to an 'endian' problem.
我遇到了类似的问题,在我的情况下,它归结为“字节序”问题。
The solution was simply to reverse the byte array (Array.Reverse in .NET)
解决方案只是反转字节数组(.NET 中的 Array.Reverse)
I don't have the 2 IDEs in front of me to show a proof, but if you get stuck, give it a try!
我面前没有 2 个 IDE 来证明,但如果您遇到困难,请尝试一下!

