TripleDES类 3des加密算法实现
生活随笔
收集整理的這篇文章主要介紹了
TripleDES类 3des加密算法实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
可以加密字符串,也可以加密字節(jié)數(shù)組。
采用3-des加密算法,加密鍵只能是16byte(128位)或者是24byte(192位)的,指定的鍵不僅有長(zhǎng)度上的要求,還不能是個(gè)弱鍵
?( 注:DES 算法使用 56 位(7 字節(jié))的密鑰)
//調(diào)用方法
// 指定加密鍵
string key = "yygmldcsjmdsthcg"; //16byte
CryptionData cd = new CryptionData(key);
string testStr = "SpNumber + “$”+ UserNumber + “$”+ ServiceTag + “$”+ AccessTime";
string result = cd.EncryptionStringData(testStr); // result 已經(jīng)經(jīng)過(guò)base64編碼
string encodingStr = HttpUtility.UrlEncode(result, System.Text.Encoding.GetEncoding("GB2312"));
//TripleDES類代碼
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Windows.Forms;
public class CryptionData
{
??// The length of Encryptionstring should be 24 byte and not be a weak key
??private string EncryptionString;
??// The length of initialization vector should be 8 byte
??private static Byte[] EncryptionIV = Encoding.Default.GetBytes("abcdefgh");
??/// <summary>
??/// Constructor
??/// </summary>
??public CryptionData()
??{
??}
??/// <summary>
??/// Constructor
??/// </summary>
??/// <param name="EncryptionString">SecureKey</param>
??public CryptionData(string EncryptionString)
??{
????this.EncryptionString = EncryptionString;
??}
??/// <summary>
??/// Encryption method for byte array
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>byte array</returns>
??public byte[] EncryptionByteData(byte[] SourceData)
??{
????byte[] returnData = null;
????try
????{
??????// Create TripleDESCryptoServiceProvider object
??????TripleDESCryptoServiceProvider desProvider = new TripleDESCryptoServiceProvider();
??????// Set SecureKey and IV of desProvider
??????byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
??????desProvider.Key = byteKey;
??????desProvider.IV = EncryptionIV;
??????// A MemoryStream object
??????MemoryStream ms = new MemoryStream();
??????// Create Encryptor
??????ICryptoTransform encrypto = desProvider.CreateEncryptor();
??????// Create CryptoStream object
??????CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
??????// Encrypt SourceData
??????cs.Write(SourceData,0,SourceData.Length);
??????cs.FlushFinalBlock();
??????// Get Encryption result
??????returnData = ms.ToArray();
????}
????catch(Exception ex)
????{
??????throw ex;
????}
????return returnData;
??}
??/// <summary>
??/// Decryption method for byte array
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>byte array</returns>
??public byte[] DecryptionByteData(byte[] SourceData)
??{
????byte[] returnData = null;
????try
????{
??????// Create TripleDESCryptoServiceProvider object
??????TripleDESCryptoServiceProvider desProvider = new TripleDESCryptoServiceProvider();
??????// Set SecureKey and IV of desProvider
??????byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
??????desProvider.Key = byteKey;
??????desProvider.IV = EncryptionIV;
??????// A MemoryStream object
??????MemoryStream ms = new MemoryStream();
??????// Create Decryptor
??????ICryptoTransform encrypto = desProvider.CreateDecryptor();
??????// Create CryptoStream object
??????CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
??????// Decrypt SourceData
??????cs.Write(SourceData, 0, SourceData.Length);
??????cs.FlushFinalBlock();
??????// Get Decryption result
??????returnData = ms.ToArray();
????}
????catch(Exception ex)
????{
??????throw ex;
????}
????return returnData;
??}
??/// <summary>
??/// Encryption method for string
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>string</returns>
??public string EncryptionStringData(string SourceData)
??{
????try
????{
??????// Convert source data from string to byte array
??????byte[] SourData = Encoding.Default.GetBytes(SourceData);
??????// Encrypt byte array
??????byte[] retData = EncryptionByteData(SourData);
??????// Convert encryption result from byte array to Base64String
??????return Convert.ToBase64String(retData, 0, retData.Length);
????}
????catch(Exception ex)
????{
??????throw ex;
????}
??}
??/// <summary>
??/// Decryption method for string
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>string</returns>
??public string DecryptionStringdata(string SourceData)
??{
????try
????{
??????// Convert source data from Base64String to byte array
??????byte[] SourData = Convert.FromBase64String(SourceData);
??????// Decrypt byte array
??????byte[] retData = DecryptionByteData(SourData);
??????// Convert Decryption result from byte array to string
??????return Encoding.Default.GetString(retData, 0, retData.Length);
????}
????catch(Exception ex)
????{
??????throw ex;
????}
??}
}
采用3-des加密算法,加密鍵只能是16byte(128位)或者是24byte(192位)的,指定的鍵不僅有長(zhǎng)度上的要求,還不能是個(gè)弱鍵
?( 注:DES 算法使用 56 位(7 字節(jié))的密鑰)
//調(diào)用方法
// 指定加密鍵
string key = "yygmldcsjmdsthcg"; //16byte
CryptionData cd = new CryptionData(key);
string testStr = "SpNumber + “$”+ UserNumber + “$”+ ServiceTag + “$”+ AccessTime";
string result = cd.EncryptionStringData(testStr); // result 已經(jīng)經(jīng)過(guò)base64編碼
string encodingStr = HttpUtility.UrlEncode(result, System.Text.Encoding.GetEncoding("GB2312"));
//TripleDES類代碼
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Windows.Forms;
public class CryptionData
{
??// The length of Encryptionstring should be 24 byte and not be a weak key
??private string EncryptionString;
??// The length of initialization vector should be 8 byte
??private static Byte[] EncryptionIV = Encoding.Default.GetBytes("abcdefgh");
??/// <summary>
??/// Constructor
??/// </summary>
??public CryptionData()
??{
??}
??/// <summary>
??/// Constructor
??/// </summary>
??/// <param name="EncryptionString">SecureKey</param>
??public CryptionData(string EncryptionString)
??{
????this.EncryptionString = EncryptionString;
??}
??/// <summary>
??/// Encryption method for byte array
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>byte array</returns>
??public byte[] EncryptionByteData(byte[] SourceData)
??{
????byte[] returnData = null;
????try
????{
??????// Create TripleDESCryptoServiceProvider object
??????TripleDESCryptoServiceProvider desProvider = new TripleDESCryptoServiceProvider();
??????// Set SecureKey and IV of desProvider
??????byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
??????desProvider.Key = byteKey;
??????desProvider.IV = EncryptionIV;
??????// A MemoryStream object
??????MemoryStream ms = new MemoryStream();
??????// Create Encryptor
??????ICryptoTransform encrypto = desProvider.CreateEncryptor();
??????// Create CryptoStream object
??????CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
??????// Encrypt SourceData
??????cs.Write(SourceData,0,SourceData.Length);
??????cs.FlushFinalBlock();
??????// Get Encryption result
??????returnData = ms.ToArray();
????}
????catch(Exception ex)
????{
??????throw ex;
????}
????return returnData;
??}
??/// <summary>
??/// Decryption method for byte array
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>byte array</returns>
??public byte[] DecryptionByteData(byte[] SourceData)
??{
????byte[] returnData = null;
????try
????{
??????// Create TripleDESCryptoServiceProvider object
??????TripleDESCryptoServiceProvider desProvider = new TripleDESCryptoServiceProvider();
??????// Set SecureKey and IV of desProvider
??????byte[] byteKey = Encoding.Default.GetBytes(EncryptionString);
??????desProvider.Key = byteKey;
??????desProvider.IV = EncryptionIV;
??????// A MemoryStream object
??????MemoryStream ms = new MemoryStream();
??????// Create Decryptor
??????ICryptoTransform encrypto = desProvider.CreateDecryptor();
??????// Create CryptoStream object
??????CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
??????// Decrypt SourceData
??????cs.Write(SourceData, 0, SourceData.Length);
??????cs.FlushFinalBlock();
??????// Get Decryption result
??????returnData = ms.ToArray();
????}
????catch(Exception ex)
????{
??????throw ex;
????}
????return returnData;
??}
??/// <summary>
??/// Encryption method for string
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>string</returns>
??public string EncryptionStringData(string SourceData)
??{
????try
????{
??????// Convert source data from string to byte array
??????byte[] SourData = Encoding.Default.GetBytes(SourceData);
??????// Encrypt byte array
??????byte[] retData = EncryptionByteData(SourData);
??????// Convert encryption result from byte array to Base64String
??????return Convert.ToBase64String(retData, 0, retData.Length);
????}
????catch(Exception ex)
????{
??????throw ex;
????}
??}
??/// <summary>
??/// Decryption method for string
??/// </summary>
??/// <param name="SourceData">source data</param>
??/// <returns>string</returns>
??public string DecryptionStringdata(string SourceData)
??{
????try
????{
??????// Convert source data from Base64String to byte array
??????byte[] SourData = Convert.FromBase64String(SourceData);
??????// Decrypt byte array
??????byte[] retData = DecryptionByteData(SourData);
??????// Convert Decryption result from byte array to string
??????return Encoding.Default.GetString(retData, 0, retData.Length);
????}
????catch(Exception ex)
????{
??????throw ex;
????}
??}
}
轉(zhuǎn)載于:https://www.cnblogs.com/yoyozhou/archive/2006/10/12/527080.html
總結(jié)
以上是生活随笔為你收集整理的TripleDES类 3des加密算法实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 用python做mud
- 下一篇: 《面向对象软件工程》笔记(一)