The idea is to produce utility class , so that whenever the guys hack the best currently known algorithms and new one comes to the market the only think that the Developer would have to do is to add the NewHighTechEncryptingAlgorithm_Encryptor class and change a global application setting for NewHighTechEncryptingAlgorithm_As_String
so that the call would be string myNewEncryptedString = Encryptor.Encrypt(StringToEncrypt , strAlgorithmName)
Edit: I removed the old code and pasted the answer, provided by rwwilden with the calling code
I quess the proper wording would be "Enhashing" as oppose of "Encryption" since no salt is envolved here ... this seems to be the best solution according to the proposed "specs"
using System;
using System.Text;
using System.Security.Cryptography;
namespace GenApp.Utils.Security
{
/// <summary>
/// Summary description for Encrypter
/// </summary>
public class Encrypter
{
/// <summary>
/// Encrypts according to the passed plain name for hasing algorithm
/// see CryptoConfig class from MSDN for more details
/// </summary>
/// <param name="strPlainTxt">the plain text to encrypt</param>
/// <param name="strAlgorithmName">The plain name of the hashing algorithm </param>
/// <returns> the encrypted string </returns>
public static string Encrypt ( string strPlainTxt, string strAlgorithmName )
{
string strHashedTxt = String.Empty;
byte[] bytPlain = System.Text.Encoding.UTF8.GetBytes ( strPlainTxt );
using (HashAlgorithm objAlgorithm = HashAlgorithm.Create ( strAlgorithmName ))
{
byte[] bytHash = objAlgorithm.ComputeHash ( bytPlain );
strHashedTxt = Convert.ToBase64String ( bytHash );
return strHashedTxt;
}
} //eof method
///// OLD CODE - REQUIRES RECODING
///// <summary>
///// Encrypts according to the passed plain name for hasing algorithm
///// see CryptoConfig class from MSDN for more details
///// </summary>
///// <param name="strPlainTxt">the plain text to encrypt</param>
///// <param name="strAlgorithmName">The plain name of the hashing algorithm </param>
///// <returns> the encrypted string </returns>
//public static string Encrypt ( string strPlainTxt, string strAlgorithmName )
//{
// string strHashedTxt = String.Empty;
// byte[] bytPlains = System.Text.Encoding.UTF8.GetBytes ( strPlainTxt );
// byte[] bytHash;
// //CryptoConfig objCryptoConfig = new CryptoConfig ();
// switch (strAlgorithmName)
// {
// case "SHA1":
// SHA1CryptoServiceProvider objProvForSHA1alg =
// (SHA1CryptoServiceProvider)CryptoConfig.CreateFromName ( strAlgorithmName );
// bytHash = objProvForSHA1alg.ComputeHash ( bytPlains );
// objProvForSHA1alg.Clear ();
// strHashedTxt = Convert.ToBase64String ( bytHash );
// break;
// case "MD5" :
// MD5CryptoServiceProvider objProvForMD5alg =
// (MD5CryptoServiceProvider)CryptoConfig.CreateFromName ( strAlgorithmName );
// bytHash = objProvForMD5alg.ComputeHash ( bytPlains );
// strHashedTxt = Convert.ToBase64String ( bytHash );
// objProvForMD5alg.Clear ();
// break;
// } //eof switch
// if (String.IsNullOrEmpty ( strHashedTxt ))
// throw new Exception ( "Encryption provider called by invalide simple name " );
// return strHashedTxt;
//} //eof method
} //eof class
class Program
{
static void Main ( string[] args )
{
string strPlainTxt = "UnEncryptedText";
string strAlgorithmName = "SHA1"; //the type of al
string strHashedTxt = String.Empty;
//START WITH ONE ALGORITHM
Console.WriteLine ( "Using the " + strAlgorithmName + " START " );
Console.WriteLine ( "The plain text is " + strPlainTxt );
Console.WriteLine ( "The encrypting algorithm is " + strAlgorithmName );
strHashedTxt = Encrypter.Encrypt ( strPlainTxt, strAlgorithmName );
Console.WriteLine ( "The hashed text is " + strHashedTxt );
Console.WriteLine ( "Using the " + strAlgorithmName + " END " );
//NOW CHANGE THE ALGORITHM
strAlgorithmName = "MD5";
Console.WriteLine ( "Using the " + strAlgorithmName + " START " );
Console.WriteLine ( "The plain text is " + strPlainTxt );
Console.WriteLine ( "The encrypting algorithm is " + strAlgorithmName );
strHashedTxt = Encrypter.Encrypt ( strPlainTxt, strAlgorithmName );
Console.WriteLine ( "The hashed text is " + strHashedTxt );
Console.WriteLine ( "Using the " + strAlgorithmName + " END " );
strAlgorithmName = "SHA256";
Console.WriteLine ( "Using the " + strAlgorithmName + " START " );
Console.WriteLine ( "The plain text is " + strPlainTxt );
Console.WriteLine ( "The encrypting algorithm is " + strAlgorithmName );
strHashedTxt = Encrypter.Encrypt ( strPlainTxt, strAlgorithmName );
Console.WriteLine ( "The hashed text is " + strHashedTxt );
Console.WriteLine ( "Using the " + strAlgorithmName + " END " );
Console.WriteLine ( "Hit enter to exit" );
Console.ReadLine ();
}
}
} //eof namespace