使用MD5WithRSA从.Pem / .Pkcs8密钥文件在C# [英] Sign data with MD5WithRSA from .Pem/.Pkcs8 keyfile in C#
问题描述
我有以下Java代码示例,我需要在C#中重新启用它:
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(pkcs8PrivateKey);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PrivateKey privKey = keyFactory.generatePrivate(privKeySpec);
签名sign = Signature.getInstance(MD5withRSA);
sign.initSign(privKey);
sign.update(data);
byte [] signature = sign.sign();
有可能使用标准的.Net Crypto API,还是应该使用BouncyCastle?
谢谢,
b。
最好的解决方案可能是Chilkat;他们的接口是最干净,最直接的,它得到良好的支持和良好的文档,并有一百万的例子。例如,下面是一些使用他们的库的代码,它们可以完成非常接近您想要的操作: http ://www.example-code.com/csharp/rsa_signPkcs8.asp 。但是,它不是免费的(虽然150美元不是不合理,看到我烧了2天试图找出这一点,我有点超过75美元一天!)
作为一种免费的替代方法,JavaScience为多种语言(包括C#/。Net)提供了一些源代码的加密实用程序,它们位于 http://www.jensign.com/JavaScience/cryptoutils/index.html 。最重要的是您想要做的是opensslkey( http://www.jensign .com / opensslkey / index.html ),它将允许您从.pem文件生成RSACryptoServiceProvider。然后可以使用该提供程序对代码进行签名:
string pemContents = new StreamReader(pkcs8privatekey.pem)。ReadToEnd );
var der = opensslkey.DecodePkcs8PrivateKey(pemContents);
RSACryptoServiceProvider rsa = opensslkey.DecodePrivateKeyInfo(der);
signature = rsa.SignData(data,new MD5CryptoServiceProvider());
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();
Is it possible with the standard .Net Crypto API, or should I use BouncyCastle? Thanks,
b.
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.
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!).
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从.Pem / .Pkcs8密钥文件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!