Crypto with Digital Certificate

  • Thread starter Thread starter Leandro H. Delamare
  • Start date Start date
L

Leandro H. Delamare

Hello Group..
Someone have a sample to how crypt a string using Crypto API with digital
certificate ?

If someone have a sample and can send to me :)

Best Regards

Leandro Delamare
 
Here's an example where you read a certificate from a local cert store and
use it to sign some data. It's easy to modify it so you can encrypt/decrypt.

I hope this helps,
-JG


/********************
* Cryptographer.cs *
********************/

using System;
using System.Security.Cryptography;
using Microsoft.Web.Services.Security.X509;

namespace Cryptomaniac {
public class Crytographer {
static void Main(string[] args) {
RSACryptoServiceProvider rsa;

// open the store
X509CertificateStore store =
X509CertificateStore.LocalMachineStore("MyStore");

store.OpenRead();

// get the cert
X509CertificateCollection ccol =
store.FindCertificateBySubjectString("Test cert");

X509Certificate cert = ccol[0];

// construct the RSA object with which to sign the data
rsa = (RSACryptoServiceProvider)cert.Key;

// get the bytes from the message (args[0] is the message)
byte[] msg_bytes = System.Text.Encoding.ASCII.GetBytes(args[0]);

// sign the data
byte[] signature = rsa.SignData(msg_bytes, "MD5");

// show the data as a Base64 string
Console.WriteLine("Signature: {0}",
System.Convert.ToBase64String(signature));
}
}
}
 
Back
Top