签署MD5WithRSA从质子交换膜/ .Pkcs8密钥文件在C#中的数据 [英] Sign data with MD5WithRSA from .Pem/.Pkcs8 keyfile in C#

查看:568
本文介绍了签署MD5WithRSA从质子交换膜/ .Pkcs8密钥文件在C#中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在Java中下面的代码示例,我需要在C#中重新制定它:

I've got the following code sample in Java, and I need to re-enact it in C#:

PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(pkcs8PrivateKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);
Signature sign = Signature.getInstance("MD5withRSA");

sign.initSign(privKey);
sign.update(data);
byte[] signature = sign.sign();

是否有可能与标准的.NET加密API,或者我应该使用BouncyCastle的?
谢谢,

Is it possible with the standard .Net Crypto API, or should I use BouncyCastle? Thanks,

推荐答案

我运行到一个非常类似的问题试图创建包装Chrome扩展本机C#工具(使用SHA1,MD5没有,但是这不是一个很大的区别)。我相信我已经试过字面上用于.NET每一个可能的解决方案:System.Security.Cryptography,BouncyCastle的,OpenSSL.Net和奇尔卡特RSA。

I am running into a very similar problem trying to create a native C# tool for packing Chrome extensions (using SHA1, not MD5, but that's not a big difference). I believe I have tried literally every possible solution for .Net: System.Security.Cryptography, BouncyCastle, OpenSSL.Net and Chilkat RSA.

最好的解决办法可能是奇尔卡特;它们的接口是最干净,最简单的,它很好的支持和良好的记录,并有100万例。举例来说,这里是用他们的库,做一些事情非常接近你想要的一些代码: HTTP: //www.example-code.com/csharp/rsa_signPkcs8.asp 。但是,它不是免费的(虽然$ 150是没有道理的,看到我烧了2天试图弄清楚这一点,我做了一天!有点超过$ 75)。

The best solution is probably Chilkat; their interface is the cleanest and most straightforward, it's well-supported and well-documented, and there are a million examples. For instance, here's some code using their library that does something very close to what you want: http://www.example-code.com/csharp/rsa_signPkcs8.asp. However, it's not free (though $150 is not unreasonable, seeing as I have burned 2 days trying to figure this out, and I make a bit more than $75 a day!).

作为一个自由选择,JavaScience提供了多个加密实用程序源代码的形式支持多国语言(包括C#/。NET)的的 http://www.jensign.com/JavaScience/cryptoutils/index.html 。这是最突出的,以你正在尝试做的一个是opensslkey(http://www.jensign.com/opensslkey/index.html),这将让你产生从.pem文件一的RSACryptoServiceProvider。然后,您可以使用该供应商签署您的代码:

As a free alternative, JavaScience offers up a number of crypto utilities in source form for multiple languages (including C#/.Net) at http://www.jensign.com/JavaScience/cryptoutils/index.html. The one that's most salient to what you are trying to do is opensslkey (http://www.jensign.com/opensslkey/index.html), which will let you generate a RSACryptoServiceProvider from a .pem file. You can then use that provider to sign your code:

        string pemContents = new StreamReader("pkcs8privatekey.pem").ReadToEnd();
        var der = opensslkey.DecodePkcs8PrivateKey(pemContents);
        RSACryptoServiceProvider rsa = opensslkey.DecodePrivateKeyInfo(der);

        signature = rsa.SignData(data, new MD5CryptoServiceProvider());

这篇关于签署MD5WithRSA从质子交换膜/ .Pkcs8密钥文件在C#中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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