Skip to main content

Posts

Showing posts from August, 2013

Use X509 certificate to encrypt and decrypt

1. To make a test certificate, use the makecert.exe tool makecert -n "CN=My Company" -ss "MyCompany.com" -pe -sr LocalMachine -sky Exchange test.cer Here the -sky Exchange parameter is very important, without this, the generated certificate can only be used for signing, but not for encrypting/decrypting. 2. Write C# code as following:     public class CertificateSSO     {         private X509Certificate2 GetCertificate()         {             X509Store store = new X509Store(" MyCompany .com", StoreLocation.LocalMachine);             store.Open(OpenFlags.OpenExistingOnly);             X509Certificate2 cert = store.Certificates.Find(X509FindType.FindBySubjectName, "My Company", false)[0];             store.Close();             return cert;         }         public string Encrypt(string strPlainText)         {             X509Certificate2 cert = GetCertificate();             using (RSACryptoServiceProvider provider = (RSACrypto