asp.net EncryptHelper 加密協助類

來源:互聯網
上載者:User

複製代碼 代碼如下:/**************************************************
* 著作權: 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
{
/// <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>加密結果</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(); //加密器對象
result = desencrypt.TransformFinalBlock(data, 0, data.Length); //轉換指定位元組數組的指定地區
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>解密結果</returns>
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;
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.