C# Encryption / Decryption Helper Class

Spread the love

C# Encryption / Decryption Helper Class

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

kaynak : http://johnnycoder.com/blog/2008/07/03/c-encryption-decryption-helper-class/
}

Bir yanıt yazın

Bu site, istenmeyenleri azaltmak için Akismet kullanıyor. Yorum verilerinizin nasıl işlendiği hakkında daha fazla bilgi edinin.