Click here to Skip to main content
13,145,464 members (44,464 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 22 Mar 2004

Crypto & Hashing Wrapper Classes

, 22 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Simple class wrappers that encapsulate all the built in .NET functionality of both the symmetric encryption classes and the hashing classes.
Download source code - 2.8 Kb


The question you are most probably asking: why another Crypto class? Well, I found many articles, examples and documentation on encryption and decryption, but they were not exactly what I was looking for.

For example, there was lots of the same code in different areas: most of them used only 1 type of encryption and most were encypting to and from byte arrays and I wanted to work in strings only. 

So, I put all the knowledge I had gained into creating very simple-to-use classes to encypt, decrypt and hash strings. I also tried to create classes that had no code duplication at all, so if things change, then only 1 correction/update is needed. I also found problems in converting byte arrays to strings and back again. I went through all the possibilites, and eventually came up with the best way to do it - that works every time with all characters etc.

Summary of code (what does the code do?)

The file Crypto.cs contains 2 classes. The first one is the class Crypto and is used for symmetric encryption/decryption. It contains an enum so that you can use the different symmetric algorithms available:

public enum CryptoTypes
encTypeDES = 0,

The other is called Hashing and is used for oneway hashing. It also contains an enum for the different types of hashing available :

public enum HashingTypes
SHA, SHA256, SHA384, SHA512, MD5

The code references the NUnit library, which is used to do perform simple unit testing. You can download the NUnit libray from

How to use the code

Its easy! Add the file crypto.cs to you project, add a reference: using Utils; and fire away! The code is self explanatory and is well documented. The NUnit test also contains good examples:

string input = "Thi$ is @ str!&n to tEst encrypti0n!";
Crypto c = new Crypto(Utils.Crypto.CryptoTypes.encTypeTripleDES);
string s1 = c.Encrypt(input);
string s2 = c.Decrypt(s1);
Assert.IsTrue(s2 == input);
s1 = Hashing.Hash(input);
s2 = Hashing.Hash(input,Utils.Hashing.HashingTypes.MD5);
Assert.IsTrue(s1 == s2);
Assert.IsTrue( Hashing.isHashEqual(input,s1));
s1 = Hashing.Hash(input,Utils.Hashing.HashingTypes.SHA512); 


As you can see from the above code, the Hashing class contains only static methods, so no need to instantiate. You can also create your own utils class that has static methods for symmetric encryption/decryption.

Hope this helps.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Brad Vincent
Web Developer
South Africa South Africa
I am a software developer at CoreBiz - a small but growing software developement house in the sleepy hollow of Pietermaritzburg in South Africa, specialising in custom software development for all businesses

You may also be interested in...

Comments and Discussions

NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:15
memberTony Selke27-Sep-07 7:15 
GeneralOutput to Strings Pin
Heath Stewart23-Mar-04 18:29
editorHeath Stewart23-Mar-04 18:29 
GeneralRe: Output to Strings Pin
Brad Vincent23-Mar-04 19:31
memberBrad Vincent23-Mar-04 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 23 Mar 2004
Article Copyright 2004 by Brad Vincent
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid