C#的3DES加密解密算法實(shí)例代碼
C#類如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
/// <summary>
/// 加解密類
/// </summary>
public class EncryptHelper
{
//構(gòu)造一個(gè)對(duì)稱算法
private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
#region 加密解密函數(shù)
/// <summary>
/// 字符串的加密
/// </summary>
/// <param name="Value">要加密的字符串</param>
/// <param name="sKey">密鑰,必須32位</param>
/// <param name="sIV">向量,必須是12個(gè)字符</param>
/// <returns>加密后的字符串</returns>
public string EncryptString(string Value, string sKey,string sIV)
{
try
{
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);//創(chuàng)建加密對(duì)象
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());
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, "出現(xiàn)異常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ("Error in Encrypting " + ex.Message);
}
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="Value">加密后的字符串</param>
/// <param name="sKey">密鑰,必須32位</param>
/// <param name="sIV">向量,必須是12個(gè)字符</param>
/// <returns>解密后的字符串</returns>
public string DecryptString(string Value, string sKey, string sIV)
{
try
{
ICryptoTransform ct;//加密轉(zhuǎn)換運(yùn)算
MemoryStream ms;//內(nèi)存流
CryptoStream cs;//數(shù)據(jù)流連接到數(shù)據(jù)加密轉(zhuǎn)換的流
byte[] byt;
//將3DES的密鑰轉(zhuǎn)換成byte
mCSP.Key = Convert.FromBase64String(sKey);
//將3DES的向量轉(zhuǎn)換成byte
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);//創(chuàng)建對(duì)稱解密對(duì)象
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());
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message, "出現(xiàn)異常", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return ("Error in Decrypting " + ex.Message);
}
}
#endregion
}
}
調(diào)用方法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
EncryptHelper helper = new EncryptHelper();
//加密
string oldValue = "13800138000";
//加密后結(jié)果
//密鑰,必須32位
string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM5";
//向量,必須是12個(gè)字符
string sIV = "andyliu1234=";
//print
string newValue = helper.EncryptString(oldValue,sKey,sIV);
Console.WriteLine("加密后:"+ newValue);
//解密
string desValue = helper.DecryptString(newValue,sKey,sIV);
//
Console.WriteLine("解密后:"+ desValue);
Console.ReadLine();
}
}
}
相關(guān)文章
C#中調(diào)用SAPI實(shí)現(xiàn)語(yǔ)音合成的2種方法
這篇文章主要介紹了C#中調(diào)用SAPI實(shí)現(xiàn)語(yǔ)音合成的2種方法,本文直接給出示例代碼,需要的朋友可以參考下2015-06-06
利用C#實(shí)現(xiàn)獲取與監(jiān)控電腦系統(tǒng)信息
在C#中,獲取與監(jiān)控電腦系統(tǒng)信息通??梢酝ㄟ^多種方式實(shí)現(xiàn),這篇文章主要為大家整理了幾種常見的方法及其示例代碼,希望對(duì)大家有所幫助2024-11-11
c#實(shí)現(xiàn)獲取字符串陣列中元素最長(zhǎng)或最短的長(zhǎng)度
下面小編就為大家分享一篇c#實(shí)現(xiàn)獲取字符串陣列中元素最長(zhǎng)或最短的長(zhǎng)度方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
C#如何使用PaddleOCR進(jìn)行圖片文字識(shí)別功能
PaddlePaddle(飛槳)是百度開發(fā)的深度學(xué)習(xí)平臺(tái),旨在為開發(fā)者提供全面、靈活的工具集,用于構(gòu)建、訓(xùn)練和部署各種深度學(xué)習(xí)模型,它具有開放源代碼、高度靈活性、可擴(kuò)展性和分布式訓(xùn)練等特點(diǎn),這篇文章主要介紹了C#使用PaddleOCR進(jìn)行圖片文字識(shí)別,需要的朋友可以參考下2024-04-04
C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法
這篇文章主要介紹了C#使用RenderControl將GridView控件導(dǎo)出到EXCEL的方法,是C#應(yīng)用程序設(shè)計(jì)中非常實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-08-08

