c#加密類使用方法示例
更新時(shí)間:2013年11月25日 15:41:46 作者:
這篇文章主要介紹了c#加密類使用方法,大家可以參考使用
復(fù)制代碼 代碼如下:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
namespace Encryption.App_Code
{
/// <summary>
/// 加密碼類
/// </summary>
public class Encryption
{
/// <summary>
/// 加密
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public static string DesEncrypt(string inputString)
{
return DesEncrypt(inputString, Key);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="inputString"></param>
/// <returns></returns>
public static string DesDecrypt(string inputString)
{
return DesDecrypt(inputString, Key);
}
/// <summary>
/// 密匙
/// </summary>
private static string Key
{
get
{
return "hongye10";
}
}
/// <summary>
/// 加密字符串
/// 注意:密鑰必須為8位
/// </summary>
/// <param name="strText">字符串</param>
/// <param name="encryptKey">密鑰</param>
/// <param name="encryptKey">返回加密后的字符串</param>
public static string DesEncrypt(string inputString, string encryptKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception error)
{
//return error.Message;
return null;
}
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="this.inputString">加了密的字符串</param>
/// <param name="decryptKey">密鑰</param>
/// <param name="decryptKey">返回解密后的字符串</param>
public static string DesDecrypt(string inputString, string decryptKey)
{
byte[] byKey = null;
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new Byte[inputString.Length];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(inputString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
return encoding.GetString(ms.ToArray());
}
catch (System.Exception error)
{
//return error.Message;
return null;
}
}
}
}
相關(guān)文章
Repeater的FooterTemplate顯示某列總計(jì)思路與代碼
在Repeater的FooterTemplate顯示某列總計(jì),接下來(lái)與大家分享詳細(xì)的實(shí)現(xiàn)方案,感興趣的各位可以參考下哈2013-03-03ASP.NET MVC阿里大于短信接口開(kāi)發(fā)短信群發(fā)能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC阿里大于短信接口來(lái)開(kāi)發(fā)例會(huì)短信群發(fā)能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10批量賬號(hào)的login測(cè)試功能實(shí)現(xiàn)
用WaitiN寫了個(gè)簡(jiǎn)單的login自動(dòng)化測(cè)試,能夠使用少量的代碼實(shí)現(xiàn)批量賬號(hào)的login測(cè)試,需要的朋友可以參考下2012-11-11淺析GridView中顯示時(shí)間日期格式的問(wèn)題
下面小編就為大家?guī)?lái)一篇淺析GridView中顯示時(shí)間日期格式的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05三種方法讓Response.Redirect在新窗口打開(kāi)
通過(guò)設(shè)置form的target屬性同樣可以讓Response.Rederect所指向的url在新的窗口打開(kāi),下面為大家介紹三種具體的實(shí)現(xiàn)方法2013-10-10ASP.NET CORE學(xué)習(xí)教程之自定義異常處理詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET CORE學(xué)習(xí)教程之自定義異常處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01asp.net 光棒效應(yīng)實(shí)現(xiàn)代碼
asp.net 光棒效應(yīng)(今天剛剛學(xué)到的)2009-12-12