asp.NetEncryptHelper暗号化ヘルプクラス

3318 ワード

 
  
/**************************************************
* : Mr_Sheng
* : EncryptHelper.cs
* :
* : EncryptHelper
* :
* ;
* GPL v3 , / ;
* , ;
* 。
* GNU v3( license.txt )。
* :
* v2.0.0 Mr_Sheng 2009-09-09
*
***************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;

namespace Sheng.Common
{
///
///
///

public class EncryptHelper
{
///
/// MD5
///

///
///
public static string MD5DecryptString(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] md5Source = System.Text.Encoding.UTF8.GetBytes(str);
byte[] md5Out = md5.ComputeHash(md5Source);
return Convert.ToBase64String(md5Out);
}

///
/// DES
///

///
/// Key
///
public string DESEncryptString(string sInputString, string sKey)
{
try
{
byte[] data = Encoding.Default.GetBytes(sInputString);
byte[] result;
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //
ICryptoTransform desencrypt = DES.CreateEncryptor(); //
result = desencrypt.TransformFinalBlock(data, 0, data.Length); //
return BitConverter.ToString(result);
}
catch (Exception ex)
{
//ex.Message = "DES ";
throw ex;
}
}

///
/// DES
///

///
/// Key
///
public string DESDecryptString(string sInputString, string sKey)
{
try
{
//
string[] sInput = sInputString.Split("-".ToCharArray());
byte[] data = new byte[sInput.Length];
byte[] result;
for (int i = 0; i < sInput.Length; i++)
{
data[i] = byte.Parse(sInput[i], System.Globalization.NumberStyles.HexNumber);
}

DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateDecryptor();
result = desencrypt.TransformFinalBlock(data, 0, data.Length);
return Encoding.Default.GetString(result);
}
catch (Exception ex)
{
//ex.Message = "DES ";
throw ex;
}
}
}
}