namespace Sheng.Common
{
/// <summary>
/// 加密幫助類
/// </summary>
public class EncryptHelper
{
/// <summary>
/// MD5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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);
}
/// <summary>
/// DES加密字符串
/// </summary>
/// <param name="sInputString">輸入字符</param>
/// <param name="sKey">Key</param>
/// <returns>加密結(jié)果</returns>
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(); //加密器對(duì)象
result = desencrypt.TransformFinalBlock(data, 0, data.Length); //轉(zhuǎn)換指定字節(jié)數(shù)組的指定區(qū)域
return BitConverter.ToString(result);
}
catch (Exception ex)
{
//ex.Message = "DES加密異常";
throw ex;
}
}
/// <summary>
/// DES解密字符串
/// </summary>
/// <param name="sInputString">輸入字符</param>
/// <param name="sKey">Key</param>
/// <returns>解密結(jié)果</returns>
public string DESDecryptString(string sInputString, string sKey)
{
try
{
//將字符串轉(zhuǎn)換為字節(jié)數(shù)組
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;
}
}
}
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com