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)文章
.NET?如何使用?OpenTelemetry?metrics?監(jiān)控應(yīng)用程序指標(biāo)
這篇文章主要介紹了.NET?使用?OpenTelemetry?metrics?監(jiān)控應(yīng)用程序指標(biāo),通過代碼演示了如何通過 OpenTelemetry 把 Metrics 的數(shù)據(jù)發(fā)送到 Prometheus 里進(jìn)行查詢與展示,然后又演示了自定義相關(guān)指標(biāo)來滿足業(yè)務(wù)數(shù)據(jù)指標(biāo)的監(jiān)控,需要的朋友可以參考下2024-06-06
實(shí)現(xiàn)DataGridView控件中CheckBox列的使用實(shí)例
最近做WindowsForms程序,使用DataGridView控件時(shí),加了一列做選擇用,發(fā)現(xiàn)CheckBox不能選中。搜索后,要實(shí)現(xiàn)DataGridView的CellContentClick事件,將代碼貼一下2014-01-01
ASP.NET MVC實(shí)現(xiàn)批量文件上傳
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC實(shí)現(xiàn)批量文件上傳,簡(jiǎn)單介紹單文件上傳的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Repeater的FooterTemplate顯示某列總計(jì)思路與代碼
在Repeater的FooterTemplate顯示某列總計(jì),接下來與大家分享詳細(xì)的實(shí)現(xiàn)方案,感興趣的各位可以參考下哈2013-03-03
ASP.NET?MVC開發(fā)接入微信公共平臺(tái)
這篇文章主要為大家介紹了微信平臺(tái)開發(fā)ASP.NET?MVC接入微信公共平臺(tái)實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Windows Server 2012 R2 或 2016無法安裝.Net 3.5.1
這篇文章主要為大家詳細(xì)介紹了Windows Server 2012 R2 或 2016 無法安裝 .Net 3.5.1,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

