C# Encryption / Decryption Helper Class
03
July
I have collected a reasonably good size library of C# helper files over the years. The EncryptionHelper below is one of many which I plan to share.
using System; using System.Security.Cryptography; using System.Text; namespace Common { public static class EncryptionHelper { private const string cryptoKey = "cryptoKey"; // The Initialization Vector for the DES encryption routine private static readonly byte[] IV = new byte[8] { 240, 3, 45, 29, 0, 76, 173, 59 }; /// <summary> /// Encrypts provided string parameter /// </summary> public static string Encrypt(string s) { if (s == null || s.Length == 0) return string.Empty; string result = string.Empty; try { byte[] buffer = Encoding.ASCII.GetBytes(s); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey)); des.IV = IV; result = Convert.ToBase64String( des.CreateEncryptor().TransformFinalBlock( buffer, 0, buffer.Length)); } catch { throw; } return result; } /// <summary> /// Decrypts provided string parameter /// </summary> public static string Decrypt(string s) { if (s == null || s.Length == 0) return string.Empty; string result = string.Empty; try { byte[] buffer = Convert.FromBase64String(s); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider(); des.Key = MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey)); des.IV = IV; result = Encoding.ASCII.GetString( des.CreateDecryptor().TransformFinalBlock( buffer, 0, buffer.Length)); } catch { throw; } return result; } } }
You may have noticed that my recent posts have been short and provide little more than featured code. This is a new approach for me. What do you think? Should I continue to pepper in these quick code snippet posts with little commentary?



well, I would say, that format is okay for me. The helper is great. Thanks
Looks interesting, but I have one question:
Is there a reason that your catch block in both of the methods simply re-throws? Wouldn’t simply not having the try/catch block there and allowing the exception to bubble up the stack to the caller achive the same result or am I missing something?
Hi Jesse,
You are absolutely correct and you aren’t missing a thing. The try/catch/throw is providing zero programmatic value since this ‘pattern’ accomplishes the same thing as having no try/catch/throw at all.
With that said, there is some method to my madness and the seemingly nugatory code was intentional. I don’t know about you, but I tend to wait until my code is working until I do my refactoring and clean up. For example, I may add XML Commments, error handling, logging or I may also optimize for performance. In some cases, there isn’t a lot to do, but I personally want a way to ‘mark’ the code as ‘closed.’ In other words, I’ve completed my review and I don’t need to give it further attention. In the two cases above, I can quickly see the routines are ‘closed’ since comments and exception handling are in place. If I opted to exclude the explicit exception handling, I might waste time reviewing the code again and I have fallen into this trap before.
I don’t know. The try/catch/throw acts as a good indicator for me, but it isn’t for everyone. I definitely should consider stripping it out in future samples.
Thanks for the comment,
Ben
[...] - bookmarked by 1 members originally found by nexusicon on 2008-08-15 C# Encryption / Decryption Helper Class http://johnnycoder.com/blog/2008/07/03/c-encryption-decryption-helper-class/ - bookmarked by 2 [...]
thank you for sharing this. There are many technical pages on the web about encryption but few just have a simple, and more important, USEFUL class like this. Works like a charm, thanks!