Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Certificate Encryption/Decryption Example

Posted on 2006-11-14
6
Medium Priority
?
1,567 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 2000 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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

916 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