Solved

Certificate Encryption/Decryption Example

Posted on 2006-11-14
6
1,528 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 13

Expert Comment

by:joechina
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now