源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#实现简单的3DES加密解密功能示例

  • 时间:2022-11-03 08:51 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#实现简单的3DES加密解密功能示例
本文实例讲述了C#实现简单的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)
    {
      //实例化三倍DES服务提供者类
      TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
      //随机生成密钥和IV向量
      TDES.GenerateIV();
      TDES.GenerateKey();
      //执行加密
      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);
      //执行解密
      Str2Box.Text = DecrypText(EncrypeByte, TDES.Key, TDES.IV);
    }
    #region 加密
    /// <summary>
    /// 执行三倍DES加密的方法
    /// </summary>
    /// <param name="Str">明文字符串</param>
    /// <param name="Key">密钥</param>
    /// <param name="IV">IV向量</param>
    /// <returns>密文的字节序列</returns>
    private byte[] EncrypText(string Str, byte[] Key, byte[] IV)
    {
      //创建一个内存流,用于存放密文
      MemoryStream MS = new MemoryStream();
      //创建一个三倍DES服务提供者对象
      TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
      //创建一个加密流,用于加密操作
      CryptoStream CS = new CryptoStream(MS,
        TDES.CreateEncryptor(Key, IV),
        CryptoStreamMode.Write);
      //定义输入,执行加密操作
      CS.Write(Encoding.UTF8.GetBytes(Str), 0, Str.Length);
      CS.FlushFinalBlock();
      //返回将密文的内存流转换为字节序列并返回
      return MS.ToArray();
    }
    #endregion
    #region 解密
    /// <summary>
    /// 执行三倍DES解密的方法
    /// </summary>
    /// <param name="CrypeText">密文的字节序列</param>
    /// <param name="Key">密钥</param>
    /// <param name="IV">IV向量</param>
    /// <returns>明文的字符串</returns>
    private string DecrypText(byte[] CrypeText, byte[] Key, byte[] IV)
    {
      //创建一个内存流,用于存放密文
      MemoryStream MS = new MemoryStream(CrypeText);
      //创建一个三倍DES服务提供者对象
      TripleDESCryptoServiceProvider TDES = new TripleDESCryptoServiceProvider();
      //创建解密流
      CryptoStream CS = new CryptoStream(MS,
        TDES.CreateDecryptor(Key, IV),
        CryptoStreamMode.Read);
      //创建一个用于存放明文的容器(字节序列)
      byte[] DecrypeBytes = new byte[CrypeText.Length];
      //执行解密
      CS.Read(DecrypeBytes, 0, CrypeText.Length);
      //返回解密后的明文字符串
      return Encoding.UTF8.GetString(DecrypeBytes);
    }
    #endregion
  }
}

运行效果: [img]http://files.jb51.net/file_images/article/201708/2017828121318944.jpg?2017728122057[/img] [b]PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:[/b] [b]文字在线加密解密工具(包含AES、DES、RC4等): [/b][url=http://tools.jb51.net/password/txt_encode]http://tools.jb51.net/password/txt_encode[/url] [b]MD5在线加密工具: [/b][url=http://tools.jb51.net/password/CreateMD5Password]http://tools.jb51.net/password/CreateMD5Password[/url] [b]在线散列/哈希算法加密工具: [/b][url=http://tools.jb51.net/password/hash_encrypt]http://tools.jb51.net/password/hash_encrypt[/url] [b]在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具: [/b][url=http://tools.jb51.net/password/hash_md5_sha]http://tools.jb51.net/password/hash_md5_sha[/url] [b]在线sha1/sha224/sha256/sha384/sha512加密工具: [/b][url=http://tools.jb51.net/password/sha_encode]http://tools.jb51.net/password/sha_encode[/url] 更多关于C#相关内容还可查看本站专题:《[url=http://www.1sucai.cn/Special/173.htm]C#加密与解密算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/611.htm]C#窗体操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/165.htm]C#常见控件用法教程[/url]》、《[url=http://www.1sucai.cn/Special/125.htm]WinForm控件用法总结[/url]》、《[url=http://www.1sucai.cn/Special/116.htm]C#数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/265.htm]C#数组操作技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/478.htm]C#面向对象程序设计入门教程[/url]》 希望本文所述对大家C#程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部