将字节数组转换为X.509证书 [英] Converting a byte array to a X.509 certificate

查看:1591
本文介绍了将字节数组转换为X.509证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一段Java代码移植到.NET中,它需要一个Base64编码字符串,将其转换为字节数组,然后使用它来生成X.509证书,以获取模数&


$ b 这是我要转换的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();

我一直在想办法把这个转换成.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;对我来说这看起来像应该工作,但它抛出一个 < /msdn.microsoft.com/en-us/library/system.security.cryptography.cryptographicexception.aspxrel =nofollow noreferrer> CryptographicException 当我使用base64编码字符串从Java代码生成X509证书。我收到的确切讯息是:


找不到要求的物件。


Java的X.509实现只是与.NET不兼容,或者我在从Java到.NET的转换中做错了吗?



或者在这种情况下,从Java到.NET没有转换?

解决方案

您的base64编码资料似乎不是X.509凭证:


[ X509EncodedKeySpec class ]表示ASN.1编码


导出Java中的整个X.509证书,或尝试在中找到等效的X509EncodedKeySpec类。 .NET框架。


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.

This is the Java code I'm trying to convert:

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:

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 CryptographicException when I use the base 64 encoded string from the Java code to generate the X509 certificate. The exact message I receive is:

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?

Or is there simply no conversion from Java to .NET in this case?

解决方案

It seems your base64-encoded data does not represent an X.509 certificate:

[The X509EncodedKeySpec class] represents the ASN.1 encoding of a public key

Export the whole X.509 certificate in Java, or try to find an equivalent of the X509EncodedKeySpec class in the .NET framework.

这篇关于将字节数组转换为X.509证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆