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阿里大于短信接口開發(fā)短信群發(fā)能
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC阿里大于短信接口來(lá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-11ASP.NET CORE學(xué)習(xí)教程之自定義異常處理詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET CORE學(xué)習(xí)教程之自定義異常處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(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