Solved

Certificate Encryption/Decryption Example

Posted on 2006-11-14
6
1,541 Views
Last Modified: 2008-11-20
I need a good, and preferably simple, tutorial on how to create a X.509 certificate and then sample code using that certificate to encrypt and decrypt.  The application I need to integrate the encryption into is on .NET 2.0, and I will take C# or VB code.
0
Comment
Question by:SkipFire
  • 3
  • 3
6 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 500 total points
ID: 17940706
Way 1)
http://www.openssl.org/docs/HOWTO/certificates.txt
Way 2)
http://technet2.microsoft.com/WindowsServer/en/library/448dc9ee-eec5-4229-a6e5-64653bc4e1531033.mspx?mfr=true

Please visit http://www.openssl.org to get more information about PKI.


The following are example functions using a PKCS12 cert. (It has both public and private keys) and the result is encoded/decoded by base64.

static string Decrypt(string data)
        {
            X509Certificate2 myCertificate = new X509Certificate2("c:\\temp\\mycerts.p12","123456");
            RSACryptoServiceProvider provider1 = (RSACryptoServiceProvider)myCertificate.PrivateKey;
            byte[] buffer1 = Convert.FromBase64String(data);
            byte[] result = provider1.Decrypt(buffer1,false);
            return (new UnicodeEncoding()).GetString(result);
        }
        static string Encrypt(string data)
        {
            X509Certificate2 myCertificate = new X509Certificate2("c:\\temp\\mycerts.p12", "123456");
            RSACryptoServiceProvider provider1 = (RSACryptoServiceProvider)myCertificate.PublicKey.Key;
            byte[] buffer1 = (new UnicodeEncoding()).GetBytes(data);
            byte[] result = provider1.Encrypt(buffer1, false);
            string b64s = Convert.ToBase64String(result);
            return b64s;
        }

        static bool VerifyData(string data, string signature)
        {
            X509Certificate2 myCertificate = new X509Certificate2("c:\\temp\\mycerts.p12", "123456");
            RSACryptoServiceProvider provider1 = (RSACryptoServiceProvider)myCertificate.PublicKey.Key;
            byte[] sb = Convert.FromBase64String(signature);
            byte[] db = (new ASCIIEncoding()).GetBytes(data);
           return provider1.VerifyData(db, new SHA1CryptoServiceProvider(), sb);
         }

        static string SignData(string data)
        {
            X509Certificate2 myCertificate = new X509Certificate2("c:\\temp\\mycerts.p12", "123456");
            RSACryptoServiceProvider provider1 = (RSACryptoServiceProvider)myCertificate.PrivateKey;
            byte[] db = (new ASCIIEncoding()).GetBytes(data);
             byte[] sb = provider1.SignData(db, new SHA1CryptoServiceProvider());
             return Convert.ToBase64String(sb);
        }
0
 
LVL 4

Author Comment

by:SkipFire
ID: 17940730
Thanks joechina, I was hoping you would find and respond to this.  I'll give it a try and get back to you asap.  This is back on that java junk I was dealing with a month ago.  I can't get .NET to recognize their cert as valid, so I figured I would build a .NET sample end to end and make them encrypt with what I know I can decrypt.
0
 
LVL 4

Author Comment

by:SkipFire
ID: 17940862
I looked through both of the links, but I don't see how I get a PKCS12 certificate from Windows 2003.  I went into the certsrv site and I see a chain download which is a PKCS #7 and if I request a new certificate I see an option to generate a PKCS10.

What is it I am not understanding?
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 13

Expert Comment

by:joechina
ID: 17947072
Hi, SkipFire,

Microsoft CA server is to sign the certificate. I think the browser generates the keys and send it to CA server for signing.
For some reason, IE won't let you export the private key. (PKCS12 file). But FireFox does. Please use FireFox and connect to MS CA server and request a personal cert and install it. After that you should be able to export a PKCS12 file from the option menu.

BTW, here is detail description of various PKCS standards.

http://en.wikipedia.org/wiki/PKCS

Good Luck
0
 
LVL 4

Author Comment

by:SkipFire
ID: 17949324
Ok, I've got a certificate that it seems to recognize.  What are the VerifyData and SignData function used for?  Nothing calls either function which is why I'm not seeing their purpose.
0
 
LVL 13

Expert Comment

by:joechina
ID: 18085628
VerifyData and SignData are used for digital signature

For example, if I send you a message and don't care other person can see it or not, I will call SignData(with my private key) and send you both the data and the result string returned by SignData. When you receive them, you can call VerifyData(using my public key) to make sure the message is sent by me not by any other person.

Hope this explains,
(Sorry for the later reply)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question