c#等价于"java.security.spec.RSAPublicKeySpec";和"java.security.PublicKey" [英] c# equivalent of "java.security.spec.RSAPublicKeySpec" and "java.security.PublicKey"

查看:84
本文介绍了c#等价于"java.security.spec.RSAPublicKeySpec";和"java.security.PublicKey"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用现有Java应用程序的C#开发一个新版本.

I'm developing a new version in c# of an existing java application.

现有应用程序使用具有java.security.spec.*和boncycastle api的RSA加密.

The existing application uses RSA encryption with java.security.spec.* and boncycastle api.

我正在用下面的Java代码在c#中寻找等效的代码:

I'm looking for equivalent code in c# for the java below code:

public static java.security.PublicKey getKey
(
org.bouncycastle.asn1.x509.RSAPublicKeyStructure  rsaPublicKey
)
{

java.security.KeyFactory keyFactory = KeyFactory.getInstance("RSA");

 java.security.spec.RSAPublicKeySpec keySpec = new RSAPublicKeySpec(
rsaPublicKey.getModulus(), 
rsaPublicKey.getPublicExponent());

java.security.PublicKey pkey = keyFactory.generatePublic(keySpec);

return pkey;
}

我经常搜索",但未找到解决方案.

I "googled" a lot but don't found solution.

预先感谢您的帮助.

推荐答案

尽管您可能已经知道了这一点,但它是.a版本的

Although you may be already aware of this, there is a .NET version of Bouncy Castle, so you can use it in your C# project.

关于您的问题,此处是以下示例在纯的Bouncy Castle中实现签名,它使用MakeKey方法处理密钥生成,因此您可能需要看一下它.

Regarding your question, here is an example of implementing signing in pure Bouncy Castle, an it deals with key generation in the MakeKey method, so you may want to take a look at it.

顺便说一句,如果此密钥在证书中,则可能需要查看.NET X509Certificate2类.

By the way, if this key is in a certificate, you may want to look at the .NET X509Certificate2 class.

修改

我试图将您的方法转换为c#等效项,并且离我越近:

I tried to convert your method into a c# equivalent, and this it the closer I got:

public static byte[] getKey(Org.BouncyCastle.Asn1.x509.RSAPublicKeyStructure  rsaPublicKey)
{
    Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters bcKeySpec = new RsaKeyParameters();
    bcKeySpec.RsaKeyParameters(false, rsaPublicKey.getModulus(), rsaPublicKey.getPublicExponent());
    RSAParameters keySpec = Org.BouncyCastle.Security.DotNetUtilities.ToRSAParameters(bcKeySpec);
    RSACryptoServiceProvider keyFactory = new RSACryptoServiceProvider();
    keyFactory.ImportParameters(keySpec);
    byte[] pKey = keyFactory.ExportCspBlob(false);
    return pKey;
}

请注意,密钥已导出到字节数组中,这取决于您以后要对密钥进行的操作,对您有无帮助,此外,RSACryptoServiceProvider对象还允许您加密,解密,签名和验证,因此,如果出于上述目的之一要获取密钥,则可能需要返回keyFactory对象而不是导出的公共密钥.

Note that the key is exported into a byte array, which depending of what you want to do with your key later, may or may not be helpful to you, also, the RSACryptoServiceProvider object let you encrypt, decrypt, sign and verify, so if you are going to get the key for any of these purposes, then you may want to return the keyFactory object instead of the exported public key.

如果您想了解有关RSACryptoServiceProvider的更多信息,可以在这里阅读: http://msdn.microsoft.com/en-us/library/s575f7e2.aspx

If you want more information about RSACryptoServiceProvider you can read here: http://msdn.microsoft.com/en-us/library/s575f7e2.aspx

这篇关于c#等价于"java.security.spec.RSAPublicKeySpec";和"java.security.PublicKey"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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