Asp.net,C# 加密解密字符串的使用詳解
首先在web.config | app.config 文件下增加如下代碼:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="IV" value="SuFjcEmp/TE="/>
<add key="Key" value="KIPSToILGp6fl+3gXJvMsN4IajizYBBT"/>
</appSettings>
</configuration>
IV:加密算法的初始向量。
Key:加密算法的密鑰。
接著新建類CryptoHelper,作為加密幫助類。
首先要從配置文件中得到IV 和Key。所以基本代碼如下
public class CryptoHelper
{
//private readonly string IV = "SuFjcEmp/TE=";
private readonly string IV = string.Empty;
//private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
private readonly string Key = string.Empty;
/// <summary>
///構造函數(shù)
/// </summary>
public CryptoHelper()
{
IV = ConfigurationManager.AppSettings["IV"];
Key = ConfigurationManager.AppSettings["Key"];
}
}
注意添加System.Configuration.dll程序集引用。
在獲得了IV 和Key 之后,需要獲取提供加密服務的Service 類。
在這里,使用的是System.Security.Cryptography; 命名空間下的TripleDESCryptoServiceProvider類。
獲取TripleDESCryptoServiceProvider 的方法如下:
/// <summary>
/// 獲取加密服務類
/// </summary>
/// <returns></returns>
private TripleDESCryptoServiceProvider GetCryptoProvider()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.IV = Convert.FromBase64String(IV);
provider.Key = Convert.FromBase64String(Key);
return provider;
}
TripleDESCryptoServiceProvider 兩個有用的方法
CreateEncryptor:創(chuàng)建對稱加密器對象ICryptoTransform.
CreateDecryptor:創(chuàng)建對稱解密器對象ICryptoTransform
加密器對象和解密器對象可以被CryptoStream對象使用。來對流進行加密和解密。
cryptoStream 的構造函數(shù)如下:
public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode);
使用transform 對象對stream 進行轉換。
完整的加密字符串代碼如下:
/// <summary>
/// 獲取加密后的字符串
/// </summary>
/// <param name="inputValue">輸入值.</param>
/// <returns></returns>
public string GetEncryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
// 創(chuàng)建內存流來保存加密后的流
MemoryStream mStream = new MemoryStream();
// 創(chuàng)建加密轉換流
CryptoStream cStream = new CryptoStream(mStream,
provider.CreateEncryptor(), CryptoStreamMode.Write);
// 使用UTF8編碼獲取輸入字符串的字節(jié)。
byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);
// 將字節(jié)寫到轉換流里面去。
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
// 在調用轉換流的FlushFinalBlock方法后,內部就會進行轉換了,此時mStream就是加密后的流了。
byte[] ret = mStream.ToArray();
// Close the streams.
cStream.Close();
mStream.Close();
//將加密后的字節(jié)進行64編碼。
return Convert.ToBase64String(ret);
}
解密方法也類似:
/// <summary>
/// 獲取解密后的值
/// </summary>
/// <param name="inputValue">經過加密后的字符串.</param>
/// <returns></returns>
public string GetDecryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
byte[] inputEquivalent = Convert.FromBase64String(inputValue);
// 創(chuàng)建內存流保存解密后的數(shù)據
MemoryStream msDecrypt = new MemoryStream();
// 創(chuàng)建轉換流。
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
provider.CreateDecryptor(),
CryptoStreamMode.Write);
csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
csDecrypt.FlushFinalBlock();
csDecrypt.Close();
//獲取字符串。
return new UTF8Encoding().GetString(msDecrypt.ToArray());
}
完整的CryptoHelper代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;
namespace WindowsFormsApplication1
{
public class CryptoHelper
{
//private readonly string IV = "SuFjcEmp/TE=";
private readonly string IV = string.Empty;
//private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
private readonly string Key = string.Empty;
public CryptoHelper()
{
IV = ConfigurationManager.AppSettings["IV"];
Key = ConfigurationManager.AppSettings["Key"];
}
/// <summary>
/// 獲取加密后的字符串
/// </summary>
/// <param name="inputValue">輸入值.</param>
/// <returns></returns>
public string GetEncryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
// 創(chuàng)建內存流來保存加密后的流
MemoryStream mStream = new MemoryStream();
// 創(chuàng)建加密轉換流
CryptoStream cStream = new CryptoStream(mStream,
provider.CreateEncryptor(), CryptoStreamMode.Write);
// 使用UTF8編碼獲取輸入字符串的字節(jié)。
byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);
// 將字節(jié)寫到轉換流里面去。
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();
// 在調用轉換流的FlushFinalBlock方法后,內部就會進行轉換了,此時mStream就是加密后的流了。
byte[] ret = mStream.ToArray();
// Close the streams.
cStream.Close();
mStream.Close();
//將加密后的字節(jié)進行64編碼。
return Convert.ToBase64String(ret);
}
/// <summary>
/// 獲取加密服務類
/// </summary>
/// <returns></returns>
private TripleDESCryptoServiceProvider GetCryptoProvider()
{
TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
provider.IV = Convert.FromBase64String(IV);
provider.Key = Convert.FromBase64String(Key);
return provider;
}
/// <summary>
/// 獲取解密后的值
/// </summary>
/// <param name="inputValue">經過加密后的字符串.</param>
/// <returns></returns>
public string GetDecryptedValue(string inputValue)
{
TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
byte[] inputEquivalent = Convert.FromBase64String(inputValue);
// 創(chuàng)建內存流保存解密后的數(shù)據
MemoryStream msDecrypt = new MemoryStream();
// 創(chuàng)建轉換流。
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
provider.CreateDecryptor(),
CryptoStreamMode.Write);
csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
csDecrypt.FlushFinalBlock();
csDecrypt.Close();
//獲取字符串。
return new UTF8Encoding().GetString(msDecrypt.ToArray());
}
}
}
使用例子:

相關文章
asp.net core配合vue實現(xiàn)后端驗證碼邏輯
網上的前端驗證碼邏輯總感覺不安全,驗證碼建議還是使用后端配合驗證。本文主要介紹了asp.net core配合vue實現(xiàn)后端驗證碼邏輯,感興趣的可以了解一下2021-06-06
MVC使用T4模板生成其他類的具體實現(xiàn)學習筆記2
這篇文章主要為大家詳細介紹了MVC使用T4模板生成其他類的具體實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
asp.net中獲取遠程網頁的內容之一(downmoon原創(chuàng))
asp.net中獲取遠程網頁的內容之一(downmoon原創(chuàng))...2007-04-04
Asp.Net?Core配置多環(huán)境log4net配置文件的全過程
在.NET世界中有非常多的日志框架,然而log4net是目前為止最流行的一款日志框架,下面這篇文章主要給大家介紹了關于Asp.Net?Core配置多環(huán)境log4net配置文件的相關資料,需要的朋友可以參考下2022-04-04
ASP.NET Core中預壓縮靜態(tài)文件的方法步驟
這篇文章主要給大家介紹了關于ASP.NET Core中如何預壓縮靜態(tài)文件的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-03-03
VS2010/VS2013項目創(chuàng)建 ADO.NET連接mysql/sql server詳細步驟
這篇文章主要介紹了VS2010/VS2013項目創(chuàng)建,及ADO.NET連接mysql/sql server詳細步驟,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10

