Sponsored Ad

Thursday, October 22, 2009

C# Extension

It's elementary to start. First, generate a standard library class file. Change the signature of the class to be static, add static methods, and add a reference to your project for the new DLL. For the most part, this is it. There aqre some minor changes to the method signatures which they will cover in the sample code below, but this is easy stuff. For the code posted here, I am going to leave out the validation checks for brevity, but the downloadable code does contain it. I am also only going to show the code that uses a Registry key for encryption/decryption.

//Nothing special about this - just a standard static class
public static class StringExtensionMethods
{

    //Almost a standard static method just the first parameter is different
    //the keyword "this" tells what type of type you are extending
    //so the "this string" means we want
    //this method to be used by System.String types
    public static string EncryptStringUsingRegistryKey(this string encryptValue,
                                                       string publicKey)
    {
       // This is the variable that will be returned to the user
       string encryptedValue = string.Empty;

       // Create the CspParameters object which is used to create the RSA provider
       // without it generating a new private/public key.
       // Parameter value of 1 indicates RSA provider
       // type - 13 would indicate DSA provider
       CspParameters csp = new CspParameters(1);

       // Registry key name containing the RSA private/public key
       csp.KeyContainerName = publicKey;

       // Supply the provider name
       csp.ProviderName = "Microsoft Strong Cryptographic Provider";

       try
       {
          //Create new RSA object passing our key info
          RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);
          // Before encrypting the value we must convert it over to byte array
          byte[] bytesToEncrypt = System.Text.Encoding.UTF8.GetBytes(encryptValue);

          // Encrypt our byte array. The false parameter has to do with
          // padding (not to clear on this point but you can
          // look it up and decide which is better for your use)
          byte[] bytesEncrypted = rsa.Encrypt(bytesToEncrypt, false);
          // Extract our encrypted byte array into a string value to return to our user
          encryptedValue = Convert.ToBase64String(bytesEncrypted);
       }
       catch (CryptographicException cex)
       {
          Console.WriteLine(cex.Message);
       }
       catch (Exception ex)
       {
          Console.WriteLine(ex.Message);
       }

        return encryptedValue;
    }

    public static string DecryptStringUsingRegistryKey(this string
                         decryptValue, string privateKey)
    {

       // This is the variable that will be returned to the user
       string decryptedValue = string.Empty;

       // Create the CspParameters object which is used to create the RSA provider
       // without it generating a new private/public key.
       // Parameter value of 1 indicates RSA provider
       // type - 13 would indicate DSA provider
       CspParameters csp = new CspParameters(1);

       // Registry key name containing the RSA private/public key
       csp.KeyContainerName = privateKey;

       // Supply the provider name
       csp.ProviderName = "Microsoft Strong Cryptographic Provider";

       try
       {
          //Create new RSA object passing our key info
          RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(csp);

          // Before decryption we must convert this ugly string into a byte array
          byte[] valueToDecrypt = Convert.FromBase64String(decryptValue);

          // Decrypt the passed in string value -
          // Again the false value has to do with padding
          byte[] plainTextValue = rsa.Decrypt(valueToDecrypt, false);

          // Extract our decrypted byte array into
          // a string value to return to our user
          decryptedValue = System.Text.Encoding.UTF8.GetString(plainTextValue);
       }
       catch (CryptographicException cex)
       {
          Console.WriteLine(cex.Message);
       }
       catch (Exception ex)
       {
          Console.WriteLine(ex.Message);
       }

       return decryptedValue;
    }
}

0 comments:

Post a Comment

Sponsored Ad

Website Update

Followers