C#實(shí)現(xiàn)簡單的3DES加密解密功能示例
本文實(shí)例講述了C#實(shí)現(xiàn)簡單的3DES加密解密功能。分享給大家供大家參考,具體如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace _3DES
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CrypeBtn_Click(object sender, EventArgs e)
{
//實(shí)例化三倍DES服務(wù)提供者類
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
//隨機(jī)生成密鑰和IV向量
TDES.GenerateIV();
TDES.GenerateKey();
//執(zhí)行加密
byte[] EncrypeByte = EncrypText(StrBox.Text, TDES.Key, TDES.IV);
//顯示密文和密鑰
KeyBox.Text = Encoding.ASCII.GetString(TDES.Key);
IVBox.Text = Encoding.ASCII.GetString(TDES.IV);
CrypeBox.Text = Encoding.UTF8.GetString(EncrypeByte);
//執(zhí)行解密
Str2Box.Text = DecrypText(EncrypeByte, TDES.Key, TDES.IV);
}
#region 加密
/// <summary>
/// 執(zhí)行三倍DES加密的方法
/// </summary>
/// <param name="Str">明文字符串</param>
/// <param name="Key">密鑰</param>
/// <param name="IV">IV向量</param>
/// <returns>密文的字節(jié)序列</returns>
private byte[] EncrypText(string Str, byte[] Key, byte[] IV)
{
//創(chuàng)建一個內(nèi)存流,用于存放密文
MemoryStream MS = new MemoryStream();
//創(chuàng)建一個三倍DES服務(wù)提供者對象
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
//創(chuàng)建一個加密流,用于加密操作
CryptoStream CS = new CryptoStream(MS,
TDES.CreateEncryptor(Key, IV),
CryptoStreamMode.Write);
//定義輸入,執(zhí)行加密操作
CS.Write(Encoding.UTF8.GetBytes(Str), 0, Str.Length);
CS.FlushFinalBlock();
//返回將密文的內(nèi)存流轉(zhuǎn)換為字節(jié)序列并返回
return MS.ToArray();
}
#endregion
#region 解密
/// <summary>
/// 執(zhí)行三倍DES解密的方法
/// </summary>
/// <param name="CrypeText">密文的字節(jié)序列</param>
/// <param name="Key">密鑰</param>
/// <param name="IV">IV向量</param>
/// <returns>明文的字符串</returns>
private string DecrypText(byte[] CrypeText, byte[] Key, byte[] IV)
{
//創(chuàng)建一個內(nèi)存流,用于存放密文
MemoryStream MS = new MemoryStream(CrypeText);
//創(chuàng)建一個三倍DES服務(wù)提供者對象
TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
//創(chuàng)建解密流
CryptoStream CS = new CryptoStream(MS,
TDES.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
//創(chuàng)建一個用于存放明文的容器(字節(jié)序列)
byte[] DecrypeBytes = new byte[CrypeText.Length];
//執(zhí)行解密
CS.Read(DecrypeBytes, 0, CrypeText.Length);
//返回解密后的明文字符串
return Encoding.UTF8.GetString(DecrypeBytes);
}
#endregion
}
}
運(yùn)行效果:
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#加密與解密算法與技巧總結(jié)》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計入門教程》
希望本文所述對大家C#程序設(shè)計有所幫助。
上一篇:C#實(shí)現(xiàn)動態(tài)數(shù)據(jù)繪圖graphic的方法示例
欄 目:C#教程
下一篇:C#使用ILGenerator動態(tài)生成函數(shù)的簡單代碼
本文標(biāo)題:C#實(shí)現(xiàn)簡單的3DES加密解密功能示例
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5504.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什


