开发者

How can I import private key and use it to sign document in C#?

I've generate a self-signed certificates(X509 certificate) and want to use the private key to sign some document to make digital signature and I'm doing it in C#.

How can I import .pvk file? Do I need to import from key store?

And can I use this code to sign and create a digital signature?

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA开发者_开发百科);
RSAFormatter.SetHashAlgorithm("SHA1");
RSAFormatter.SetKey(cert.PrivateKey);
byte[] SignedHash = RSAFormatter.CreateSignature(data);


Try tho Initialize your RSA like this:

RSACryptoServiceProvider RSA = (RSACryptoServiceProvider)cert.PrivateKey;

To generate a signature I use rsa.SignData(data, "sha1");


First of all you need to determine, in what format the signature should be made. If you deal with certificates, most likely you will create PKCS#7 or CMS signature. There's a class in .NET for CMS signatures.

The next steps depend on whether you need certificate-based PKCS#7 signature or RSA (PKCS#1) signature.

Side note - putting several questions into one makes it hard to answer properly. Also most of your questions have lots of answers if you do the search.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜