Pete Slade
Tampa, Florida
Sidebar
blocks_image
C# Usage Example
using SladeHome.SimpleCrypto;
....
....
  string hashed = Crypto.GetMD5Hash("Hello World");
  Console.WriteLine("Hashed Value: " + hashed);

  string encrypted = Crypto.EncryptTripleDES("Hello World", "MyPassPhrase");
  Console.WriteLine("Encrypted Text: " + encrypted);
  string decrypted = Crypto.DecryptTripleDES(encrypted, "MyPassPhrase");
  Console.WriteLine("Decrypted Text: " + decrypted);
 

VB.NET Usage Example
Imports SladeHome.SimpleCrypto
....
....
  Dim hashed As String = Crypto.GetMD5Hash("Hello World")
  Console.WriteLine("Hashed Value: " + hashed)
  Dim encrypted As String = Crypto.EncryptTripleDES("Hello World", "MyPassPhrase")
  Console.WriteLine("Encrypted Text: " + encrypted)
  Dim decrypted As String = Crypto.DecryptTripleDES(encrypted, "MyPassPhrase")
  Console.WriteLine("Decrypted Text: " + decrypted)
 
SimpleCrypto Source
using System;
using System.Security.Cryptography;
using System.Text; 

/*
 * SimpleCryto
 * By Peter Slade
 * Email: pete@sladehome.com
 * Web: http://www.sladehome.com
 *
 * THIS CODE IS PROVIDED WITHOUT ANY WARRANTY, AND WITHOUT ANY IMPLIED WARRANTIES
 * WITH REGARD TO MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * USE AT YOUR OWN RISK. YOU ASSUME ALL LIABILITIES!!!
 *
 */

namespace SladeHome.SimpleCrypto{

      /// <summary>
      ///
Simple Cryptographic Services
     
/// </summary>

      public class Crypto{ 

            /// <summary>
            /// Default Constructor
            /// </summary>
            public Crypto(){

            }

            /// <summary>
            /// Generates a cryptographic Hash Key for the provided text data.
            /// Basically a digital fingerprint
            /// </summary>
            /// <param name="dataToHash">text to hash</param>
            /// <returns>Unique hash representing string</returns>
            public static String GetMD5Hash(String dataToHash) {
                  String hexResult = "";
                  string[] tabStringHex = new string[16];
                  MD5 md5 = new MD5CryptoServiceProvider();
                  byte[] data = Encoding.ASCII.GetBytes(dataToHash);
                  byte[] result = md5.ComputeHash(data);
                  for (int i = 0; i < result.Length; i++) {
                        tabStringHex[i] = (result[i]).ToString( "x" );
                        hexResult += tabStringHex[i];
                  }
                  return hexResult;
            } 

            /// <summary>
            /// Encrypts text with Triple DES encryption using the supplied key
            /// </summary>
            /// <param name="plaintext">The text to encrypt</param>
            /// <param name="key">Key to use for encryption</param>
            /// <returns>The encrypted string represented as base 64 text</returns>
            public static string EncryptTripleDES(string plaintext, string key){
                  TripleDESCryptoServiceProvider DES =
                               new TripleDESCryptoServiceProvider();
                  MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
                  DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                  DES.Mode = CipherMode.ECB;
                  ICryptoTransform DESEncrypt = DES.CreateEncryptor();
                  byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(plaintext);
                  return Convert.ToBase64String(
                           DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }

            /// <summary>
            /// Decrypts supplied Triple DES encrypted text using the supplied key
            /// </summary>
            /// <param name="base64Text">Triple DES encrypted base64 text</param>
            /// <param name="key">Decryption Key</param>
            /// <returns>The decrypted string</returns>
            public static string DecryptTripleDES(string base64Text, string key){
                  TripleDESCryptoServiceProvider DES =
                                          new
TripleDESCryptoServiceProvider();
                  MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
                  DES.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
                  DES.Mode = CipherMode.ECB;
                  ICryptoTransform DESDecrypt = DES.CreateDecryptor();
                  byte[] Buffer = Convert.FromBase64String(base64Text);
                  return ASCIIEncoding.ASCII.GetString(
                          DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }

      } // class
} // namespace


 

Simple Crypto

Provides simple to use cryptographic services for generating Base64 MD5 Hash and performing TripleDES encryption/decryption using a password key.