Monday, April 19, 2010

How to Generate RSA Encryption keys (C#) ?


In this post, Simple Demonstration of RSA Encryption Algorithm will be explained.

Here, we are going to make use of the System.Security.Cryptography namespace.


Now, Lets create two XML files, PrivateKey.xml and PublicKey.xml. In these files we will be storing the Private Key and Public Key respectively.

Usually, the Public Key is available publically but the Private key will be kept in a Secret Location. Data Encrypted using the Public Key can only be decrypted using Private Key. Basically, Private key is the full Key and Public Key is part of the Private Key. So even with just Public key, you can't decrypt the Data. So it provides Full Protection and Data Security.

Create a Simple Web Application and create a New Class with the name "Crypt".

Assume we have both the PrivateKey.xml and PublicKey.xml in our same Application Folder. Lets create few methods to update two string variables that represent the path of PrivateKey.xml and PublicKey.xml from Aspx page. Include the following in "Crypt" class

private static string privatePath = "";
public static string publicPath = "";

        
public void updatePrivatePath(string xmlPath) {
      privatePath = xmlPath;
}

public void updatePublicPath(string xmlPath) {
     publicPath = xmlPath;
}



Include the following to update from Page Load method of Aspx Page.


Crypt myCrypt = new Crypt();
myCrypt.updatePrivatePath(Server.MapPath("PrivateKey.xml"));
myCrypt.updatePublicPath(Server.MapPath("PublicKey.xml"));


Add the following public variable in the "Crypt" class
public static RSACryptoServiceProvider rsa;

Now lets implement Private and Public Key Generation. Include the following in the "Crypt" class.


private void ParameterSetup()
{
    CspParameters cspParams;
    cspParams = new CspParameters(1, "Microsoft Strong Cryptographic Provider", "MyContainer");
    cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
    rsa = new RSACryptoServiceProvider(cspParams);
}

public void GenerateKey() 
{
     ParameterSetup();

    //Write Public and Private Key
    StreamWriter writer = new StreamWriter(privatePath);
    string publicPrivateKeyXML = rsa.ToXmlString(true);
    writer.Write(publicPrivateKeyXML);
    writer.Close();

    //Write Public Key only
    writer = new StreamWriter(publicPath);
    string publicOnlyKeyXML = rsa.ToXmlString(false);
    writer.Write(publicOnlyKeyXML);
    writer.Close();
}

Now from Aspx page, you can update the path and Call the GenerateKey method as below,
            
Crypt myCrypt = new Crypt();
myCrypt.updatePrivatePath(Server.MapPath("PrivateKey.xml"));
myCrypt.updatePublicPath(Server.MapPath("PublicKey.xml"));
myCrypt.GenerateKey();

Now you can check your Xml files to have a look at your Public and Private keys. 
Next Part, we will look at how to Encrypt and Decrypt using RSA asymmetric encryption algorithm.









0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...