Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#
Article

Crypto & Hashing Wrapper Classes

Rate me:
Please Sign up or sign in to vote.
3.96/5 (16 votes)
22 Mar 20042 min read 50.2K   1.1K   25   3
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

Introduction

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:

C#
public enum CryptoTypes
{
encTypeDES = 0,
encTypeRC2,
encTypeRijndael,
encTypeTripleDES
}

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

C#
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 http://www.nunit.org/.

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:

C#
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); 

Conclusion

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.

License

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


Written By
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

Comments and Discussions

 
NewsTwo other related encryption articles in CodeProject ... Pin
Tony Selke27-Sep-07 7:15
Tony Selke27-Sep-07 7:15 
GeneralOutput to Strings Pin
Heath Stewart23-Mar-04 18:29
protectorHeath Stewart23-Mar-04 18:29 
GeneralRe: Output to Strings Pin
Brad Vincent23-Mar-04 19:31
Brad 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.