欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

C#實(shí)現(xiàn)簡單的3DES加密解密功能示例

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

本文實(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è)計(jì)入門教程》

希望本文所述對大家C#程序設(shè)計(jì)有所幫助。

上一篇:C#實(shí)現(xiàn)動態(tài)數(shù)據(jù)繪圖graphic的方法示例

欄    目:C#教程

下一篇:C#使用ILGenerator動態(tài)生成函數(shù)的簡單代碼

本文標(biāo)題:C#實(shí)現(xiàn)簡單的3DES加密解密功能示例

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5504.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有