RSA Public Key Encryption/ Private Key Decryption

Post date: Mar 7, 2011 10:55:16 AM

using System;

using System.Collections.Generic;

using System.Security;

using System.Text;

using System.Security.Cryptography;

using System.IO;

using Microsoft.CSharp;

using System.CodeDom.Compiler;

 

 

namespace RSACryptography

{

    public class Cryptography{

 

        public static  string SUCCESS = "Success";

       

        //Function to Encrypt the Data using Public Key Encrytion

        public static string EncryptData(string publicKeyPath,string data2Encrypt) 

        {  

            StreamReader reader=null;

            RSACryptoServiceProvider rsa;

          

           

            try

            {

                //Initialize the parameters

                const int PROVIDER_RSA_FULL = 1;

                const string CONTAINER_NAME = "License";

 

                //perform Cryptography computation

                CspParameters cspParams;

                //initialize the CSP parameters

                cspParams = new CspParameters(PROVIDER_RSA_FULL);

                //Key Name

                cspParams.KeyContainerName = CONTAINER_NAME;

                //use information from computer's store

                cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

                //Provider Name

                cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

                //Configure the RAS Provider

                rsa = new RSACryptoServiceProvider(cspParams);

 

 

 

                //Reader to read xml file

                reader = new StreamReader(publicKeyPath);

                string publicOnlyKeyXML = reader.ReadToEnd();

                //initialize the public key for encryption

                rsa.FromXmlString(publicOnlyKeyXML);

                reader.Close();

                //read plaintext, encrypt it to ciphertext 

                byte[] plainbytes = System.Text.Encoding.UTF8.GetBytes(data2Encrypt);

                //Encrypt the Given data

                byte[] cipherbytes = rsa.Encrypt(plainbytes, false);

                //Ciper text to caller

                return Convert.ToBase64String(cipherbytes);

            }

            catch

            {

                try

                {

                    reader.Close();

                }

                catch { }

 

                return null;

            }

      

        }

 

      

 

        //Create New Files

        public static bool AssignNewKey(string privateKeyPath, string publicKeyPath) 

        {

            //Ture when file is created successfully

 

            bool isFileCreated = false;

            StreamWriter writer=null;

            RSACryptoServiceProvider rsa;

 

 

            try

            {

                //InitializeParameter;

 

 

                //Initialize the parameters

                const int PROVIDER_RSA_FULL = 1;

                const string CONTAINER_NAME = "License";

 

                //perform Cryptography computation

                CspParameters cspParams;

                //initialize the CSP parameters

                cspParams = new CspParameters(PROVIDER_RSA_FULL);

                //Key Name

                cspParams.KeyContainerName = CONTAINER_NAME;

                //use information from computer's store

                cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

                //Provider Name

                cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

                //Configure the RAS Provider

                rsa = new RSACryptoServiceProvider(cspParams);

                

                //provide public and private RSA params    

                 writer= new StreamWriter(privateKeyPath);

                string publicPrivateKeyXML = rsa.ToXmlString(true);

                writer.Write(publicPrivateKeyXML);

                writer.Close();

 

                //provide public only RSA params     

                writer = new StreamWriter(publicKeyPath);

                string publicOnlyKeyXML = rsa.ToXmlString(false);

                writer.Write(publicOnlyKeyXML);

 

                writer.Close();

                             

               

                isFileCreated = true;

               

                return isFileCreated;

            }//Incase any error occured when read or writing the file

            catch

            {

                try

                {

                    //close the file

                    writer.Close();

                }

                catch

                {

                }

 

                return isFileCreated;

            }

        } 

        public static string DecryptData(string privateKey,string data2Decrypt) 

        { 

            StreamReader reader=null;

 

            try

            {

                //Initialize the parameters

                //InitializeParameter();

 

 

                RSACryptoServiceProvider rsa;

 

                //Initialize the parameters

                const int PROVIDER_RSA_FULL = 1;

                const string CONTAINER_NAME = "License";

 

                //perform Cryptography computation

                CspParameters cspParams;

                //initialize the CSP parameters

                cspParams = new CspParameters(PROVIDER_RSA_FULL);

                //Key Name

                cspParams.KeyContainerName = CONTAINER_NAME;

                //use information from computer's store

                cspParams.Flags = CspProviderFlags.UseMachineKeyStore;

                //Provider Name

                cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";

                //Configure the RAS Provider

                rsa = new RSACryptoServiceProvider(cspParams);

 

 

                //Convert into 8-bit Unsignned integer array

                byte[] getpassword = Convert.FromBase64String(data2Decrypt);

 

                //Reader to read XML file

                reader= new StreamReader(privateKey);

 

                string publicPrivateKeyXML = reader.ReadToEnd();

 

                //initialize the RSA by using the key string

                rsa.FromXmlString(publicPrivateKeyXML);

                reader.Close();

 

                //read ciphertext, decrypt it to plaintext 

                byte[] plain = rsa.Decrypt(getpassword, false);

 

                //convert byte into character string

                return Encoding.UTF8.GetString(plain);

            }

            catch

            {

                try

                {

                    reader.Close();

                }

                catch

                {

                }

                return null;

            }

        }

    }

 

}