在C#中加载ASN.1/DER编码的RSA密钥对 [英] Load ASN.1/DER encoded RSA keypair in C#

查看:216
本文介绍了在C#中加载ASN.1/DER编码的RSA密钥对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Crypto ++中创建的DER编码RSA密钥对以及密码.它们是Base64Encoded字符串.我首先将数据从Base64解码为字节数组,但不确定如何将它们加载到RSACryptoServiceProvider.

I have DER encoded RSA keypair created in Crypto++, as well as cipher. They are Base64Encoded string. I first decode the data from Base64 to byte array, but I am not sure how to load them into RSACryptoServiceProvider.

static void Main()
{
    string pbkeystr = "mypublickey";
    string pvkeystr = "myprivatekey";
    string cipherstr = "mycipher";

    byte[] pbkey = Convert.FromBase64String(pbkeystr);
    byte[] pvkey = Convert.FromBase64String(pvkeystr);
    byte[] cipher = Convert.FromBase64String(cipherstr);

    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

    //Set keys here..

    //Decrypt the cipher using private key
    rsa.Decrypt(pvkey, false);
}

没有设置键的功能.我发现的唯一内容是ImportParameters方法,该方法采用RSAParameters类,该类由pqn,模数,指数等组成.我无法访问这些.

There are no functions to set keys. The only thing I found was ImportParameters method, which takes RSAParameters class which consists of p, q, n, modulus, exponent etc. I don't have access to these.

有什么办法可以将密钥作为字符串加载?如何将密钥加载到RSACryptoServiceProvider?

Is there any way I can load the keys as string? How can I load the key into RSACryptoServiceProvider?

推荐答案

有什么办法可以将密钥作为字符串加载?如何将密钥加载到RSACryptoServiceProvider中?

Is there any way I can load the keys as string? How can I load the key into RSACryptoServiceProvider?

从另一个Crypto ++问题如何在Crypto ++中加载Base64 RSA密钥,看来您已经公钥和私钥,因为您使用了DEREncodeBERDecode.也就是说,您具有RSA参数,但没有主题公用密钥信息和专用密钥信息.您的密钥缺少OID标识符和版本号.这样很好.

From your other Crypto++ question, How to load Base64 RSA keys in Crypto++, it looks like you have only the public and private keys because you used DEREncode and BERDecode. That is, you have the RSA parameters, and not the subject public key info and the private key info. Your keys lack the OID identifiers and version numbers. Things are fine that way.

在代码项目的密码互操作性:密钥中,您在您对Base64进行解码后,将需要一个C#类来解析ASN.1/DER. CodeProject文章提供了一个名为AsnKeyParser的C#类来读取ASN.1/DER,并返回一个RSAParameters以便加载到CSP中.

From Cryptographic Interoperability: Keys on the Code Project, you will need a C# class that parses the ASN.1/DER after you Base64 decode it. The CodeProject article provides a C# class called AsnKeyParser to read the ASN.1/DER and returns a RSAParameters to load into a CSP.

AsnKeyParser类的代码大约有800行,还有五个其他支持文件可以使它们全部实现,因此将其放置在这里并不是很合适.您应该自己下载它.感兴趣的文件称为CSInteropKeys.zip.

The code for the AsnKeyParser class is about 800 lines, and there are five other supporting files to make it all happen, so its not really appropriate to place it here. You should download it yourself. The file of interest is called CSInteropKeys.zip.

一旦插入AsnKeyParser类,对于RSA公钥,它将与以下内容一样简单.私钥将相似,并且代码在CodeProject网站上给出.

Once you wire-in the AsnKeyParser class, it will be as simple as the following for a RSA Public key. The private key will be similar, and the code is given on the CodeProject site.

// Your ASN.1/DER parser class
AsnKeyParser keyParser = new AsnKeyParser("rsa-public.der");
RSAParameters publicKey = keyParser.ParseRSAPublicKey();

// .Net class
CspParameters csp = new CspParameters;
csp.KeyContainerName = "RSA Test (OK to Delete)";    
csp.ProviderType = PROV_RSA_FULL;    // 1
csp.KeyNumber = AT_KEYEXCHANGE;      // 1

// .Net class
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(publicKey);

链接到另一个站点上的文件的方法不受欢迎,但是我不知道如何提供其他信息.答案中涉及的源代码太多.

Linking to files on another site is frowned upon, but I don't know how to provide the information otherwise. There's too much source code involved to place in an answer.

出于完整性考虑,.Net不会使互操作变得容易.他们不接受ASN.1/DER或PEM.而是,.Net接受键的某些XML表示形式.我相信您可以在 RFC 3275,XML签名语法和处理中找到它. 微软没有为您声明 .我在编写代码项目"文章时有点拼凑而成.

For completeness, .Net does not make interop easy. They do not accept ASN.1/DER or PEM. Rather, .Net accepts some XML representation of the keys. I believe you can find it in RFC 3275, XML-Signature Syntax and Processing. Microsoft does not state that for you. I kind of pieced it together when I wrote the Code Project article.

也许除了ASN.1/DER和PEM之外,我们还应该在Crypto ++中添加一个类以对XML进行重新注册.

Maybe we should add a class to Crypto++ to regurgitate XML in addition to ASN.1/DER and PEM.

这篇关于在C#中加载ASN.1/DER编码的RSA密钥对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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