一個(gè)可逆加密的類(使用3DES加密)
更新時(shí)間:2011年07月19日 23:29:34 作者:
表示三重?cái)?shù)據(jù)加密標(biāo)準(zhǔn)算法的基類,TripleDES 的所有實(shí)現(xiàn)都必須從此基類派生。是從 SymmetricAlgorithm 類里繼承出來(lái)。
一、提要
命名空間:System.Security.Cryptography.TripleDES 類
簡(jiǎn)單說(shuō)明: 表示三重?cái)?shù)據(jù)加密標(biāo)準(zhǔn)算法的基類,TripleDES 的所有實(shí)現(xiàn)都必須從此基類派生。是從 SymmetricAlgorithm 類里繼承出來(lái)。TripleDES 使用 DES 算法的三次連續(xù)迭代。它可以使用兩個(gè)或三個(gè) 56 位密鑰。
使用目的:比較安全的加密一種方式,密鑰和矢量的不同,會(huì)生產(chǎn)不同的加密字串。因?yàn)槭荄ES算法的三次連續(xù)迭代,而且算法可逆,這樣對(duì)于數(shù)據(jù)保密性和可恢復(fù)性都不錯(cuò)。
二、代碼示例
本代碼參照了部分MSDN上的代碼示例,再根據(jù)自己的實(shí)際情況,補(bǔ)充了一部分MSDN上沒(méi)有提到的內(nèi)容
using System;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Threading;
namespace TRIP3DES
{
/// <summary>
/// Class1 的摘要說(shuō)明。
/// </summary>
public class dllEncrypt
{
//密鑰
private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";
//矢量,矢量可以為空
private const string sIV = "qcDY6X+aPLw=";
//構(gòu)造一個(gè)對(duì)稱算法
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
public dllEncrypt(){}
#region public string EncryptString(string Value)
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="Value">輸入的字符串</param>
/// <returns>加密后的字符串</returns>
public string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
//指定加密的運(yùn)算模式
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
//獲取或設(shè)置加密算法的填充模式
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
#endregion
#region public string DecryptString(string Value)
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="Value">加過(guò)密的字符串</param>
/// <returns>解密后的字符串</returns>
public string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
#endregion
}
}
三、總結(jié)
做成類庫(kù)對(duì)于密鑰和矢量的保管比較方便,輸入輸出全部是string型變量,這樣也比較方便,密鑰的生成可以用mSCP. GenerateKey()來(lái)生成,矢量的生成也可以用mCSP.GenerateIV()來(lái)生成。大家也可以自己靈活的編寫(xiě)符合自己的3DES算法。
命名空間:System.Security.Cryptography.TripleDES 類
簡(jiǎn)單說(shuō)明: 表示三重?cái)?shù)據(jù)加密標(biāo)準(zhǔn)算法的基類,TripleDES 的所有實(shí)現(xiàn)都必須從此基類派生。是從 SymmetricAlgorithm 類里繼承出來(lái)。TripleDES 使用 DES 算法的三次連續(xù)迭代。它可以使用兩個(gè)或三個(gè) 56 位密鑰。
使用目的:比較安全的加密一種方式,密鑰和矢量的不同,會(huì)生產(chǎn)不同的加密字串。因?yàn)槭荄ES算法的三次連續(xù)迭代,而且算法可逆,這樣對(duì)于數(shù)據(jù)保密性和可恢復(fù)性都不錯(cuò)。
二、代碼示例
本代碼參照了部分MSDN上的代碼示例,再根據(jù)自己的實(shí)際情況,補(bǔ)充了一部分MSDN上沒(méi)有提到的內(nèi)容
復(fù)制代碼 代碼如下:
using System;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Threading;
namespace TRIP3DES
{
/// <summary>
/// Class1 的摘要說(shuō)明。
/// </summary>
public class dllEncrypt
{
//密鑰
private const string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM3";
//矢量,矢量可以為空
private const string sIV = "qcDY6X+aPLw=";
//構(gòu)造一個(gè)對(duì)稱算法
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
public dllEncrypt(){}
#region public string EncryptString(string Value)
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="Value">輸入的字符串</param>
/// <returns>加密后的字符串</returns>
public string EncryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
//指定加密的運(yùn)算模式
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
//獲取或設(shè)置加密算法的填充模式
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
#endregion
#region public string DecryptString(string Value)
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="Value">加過(guò)密的字符串</param>
/// <returns>解密后的字符串</returns>
public string DecryptString(string Value)
{
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
byte[] byt;
mCSP.Key = Convert.FromBase64String(sKey);
mCSP.IV = Convert.FromBase64String(sIV);
mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
byt = Convert.FromBase64String(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Encoding.UTF8.GetString(ms.ToArray());
}
#endregion
}
}
三、總結(jié)
做成類庫(kù)對(duì)于密鑰和矢量的保管比較方便,輸入輸出全部是string型變量,這樣也比較方便,密鑰的生成可以用mSCP. GenerateKey()來(lái)生成,矢量的生成也可以用mCSP.GenerateIV()來(lái)生成。大家也可以自己靈活的編寫(xiě)符合自己的3DES算法。
您可能感興趣的文章:
- vue的diff算法知識(shí)點(diǎn)總結(jié)
- 解析Vue 2.5的Diff算法
- vue2.0中g(shù)oods選購(gòu)欄滾動(dòng)算法的實(shí)現(xiàn)代碼
- JS實(shí)現(xiàn)的3des+base64加密解密算法完整示例
- python實(shí)現(xiàn)的DES加密算法和3DES加密算法實(shí)例
- Node.js DES加密的簡(jiǎn)單實(shí)現(xiàn)
- javascript實(shí)現(xiàn)des解密加密全過(guò)程
- javascript實(shí)現(xiàn)的DES加密示例
- vue.js使用3DES加密的方法示例
相關(guān)文章
C#編寫(xiě)Windows服務(wù)實(shí)例代碼
本篇文章主要介紹使用Microsoft Visual Studio2012可以很方便的創(chuàng)建一個(gè)Windows服務(wù),本例實(shí)現(xiàn)一個(gè)向D盤(pán)的txt文件里,寫(xiě)入系統(tǒng)時(shí)間的Windows服務(wù)2013-10-10
利用Aspose.Cells和Excel模板導(dǎo)出統(tǒng)計(jì)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了利用Aspose.Cells和Excel模板導(dǎo)出復(fù)雜的統(tǒng)計(jì)數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
C#基于ScottPlot實(shí)現(xiàn)可視化的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#如何基于ScottPlot實(shí)現(xiàn)可視化效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
C#實(shí)現(xiàn)常見(jiàn)加密算法的示例代碼
這篇文章主要為大家詳細(xì)介紹一下C#中一些常見(jiàn)加密算法(Base64編碼、凱撒密碼、Vigenere密碼、DES、AES)以及它們的實(shí)現(xiàn)代碼,感興趣的可以了解一下2022-07-07

