Cybercrux

Everything is achievable through technology

Encription

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;
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s