教学服务系统

 找回密码
 立即注册
搜索
查看: 557|回复: 0

信息计算2019级2班15号陈周易

[复制链接]

9

主题

21

帖子

99

积分

注册会员

Rank: 2

积分
99
发表于 2022-4-16 10:10:49 | 显示全部楼层 |阅读模式
本帖最后由 陈周易 于 2022-4-16 10:10 编辑
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;

  5. namespace ConsoleApp1
  6. {
  7.     /**//// <summary>
  8.     /// 三重DES
  9.     /// </summary>
  10.     public class TripleDES_
  11.     {
  12.         private TripleDES mydes;
  13.         public string Key;
  14.         public string IV;
  15.         /**//// <summary>
  16.         /// 对称加密类的构造函数
  17.         /// </summary>
  18.         public TripleDES_(string key)
  19.         {
  20.             mydes = new TripleDESCryptoServiceProvider();
  21.             Key = key;
  22.             IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
  23.         }
  24.         /**//// <summary>
  25.         /// 对称加密类的构造函数
  26.         /// </summary>
  27.         public TripleDES_(string key, string iv)
  28.         {
  29.             mydes = new TripleDESCryptoServiceProvider();
  30.             Key = key;
  31.             IV = iv;
  32.         }
  33.         /**//// <summary>
  34.         /// 获得密钥
  35.         /// </summary>
  36.         /// <returns>密钥</returns>
  37.         private byte[] GetLegalKey()
  38.         {
  39.             string sTemp = Key;
  40.             mydes.GenerateKey();
  41.             byte[] bytTemp = mydes.Key;
  42.             int KeyLength = bytTemp.Length;
  43.             if (sTemp.Length > KeyLength)
  44.                 sTemp = sTemp.Substring(0, KeyLength);
  45.             else if (sTemp.Length < KeyLength)
  46.                 sTemp = sTemp.PadRight(KeyLength, ' ');
  47.             return ASCIIEncoding.ASCII.GetBytes(sTemp);
  48.         }
  49.         /**//// <summary>
  50.         /// 获得初始向量IV
  51.         /// </summary>
  52.         /// <returns>初试向量IV</returns>
  53.         private byte[] GetLegalIV()
  54.         {
  55.             string sTemp = IV;
  56.             mydes.GenerateIV();
  57.             byte[] bytTemp = mydes.IV;
  58.             int IVLength = bytTemp.Length;
  59.             if (sTemp.Length > IVLength)
  60.                 sTemp = sTemp.Substring(0, IVLength);
  61.             else if (sTemp.Length < IVLength)
  62.                 sTemp = sTemp.PadRight(IVLength, ' ');
  63.             return ASCIIEncoding.ASCII.GetBytes(sTemp);
  64.         }
  65.         /**//// <summary>
  66.             /// 加密方法File to File
  67.             /// </summary>
  68.             /// <param name="inFileName">待加密文件的路径</param>
  69.             /// <param name="outFileName">待加密后文件的输出路径</param>

  70.         public void Encrypt(string inFileName, string outFileName)
  71.         {
  72.             try
  73.             {

  74.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  75.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  76.                 fout.SetLength(0);

  77.                 mydes.Key = GetLegalKey();
  78.                 mydes.IV = GetLegalIV();

  79.                 byte[] bin = new byte[100];
  80.                 long rdlen = 0;
  81.                 long totlen = fin.Length;
  82.                 int len;

  83.                 ICryptoTransform encrypto = mydes.CreateEncryptor();
  84.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  85.                 while (rdlen < totlen)
  86.                 {
  87.                     len = fin.Read(bin, 0, 100);
  88.                     cs.Write(bin, 0, len);
  89.                     rdlen = rdlen + len;
  90.                 }
  91.                 cs.Close();
  92.                 fout.Close();
  93.                 fin.Close();

  94.             }
  95.             catch (Exception ex)
  96.             {
  97.                 throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  98.             }
  99.         }
  100.         /**//// <summary>
  101.         /// 解密方法File to File
  102.         /// </summary>
  103.         /// <param name="inFileName">待解密文件的路径</param>
  104.         /// <param name="outFileName">待解密后文件的输出路径</param>
  105.         public void Decrypt(string inFileName, string outFileName)
  106.         {
  107.             try
  108.             {
  109.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  110.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  111.                 fout.SetLength(0);

  112.                 byte[] bin = new byte[100];
  113.                 long rdlen = 0;
  114.                 long totlen = fin.Length;
  115.                 int len;
  116.                 mydes.Key = GetLegalKey();
  117.                 mydes.IV = GetLegalIV();
  118.                 ICryptoTransform encrypto = mydes.CreateDecryptor();
  119.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  120.                 while (rdlen < totlen)
  121.                 {
  122.                     len = fin.Read(bin, 0, 100);
  123.                     cs.Write(bin, 0, len);
  124.                     rdlen = rdlen + len;
  125.                 }
  126.                 cs.Close();
  127.                 fout.Close();
  128.                 fin.Close();

  129.             }
  130.             catch (Exception ex)
  131.             {
  132.                 throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  133.             }
  134.         }

  135.     static void Main(string[] args)
  136.         {
  137.             string inFileName1 = @"C:\Users\user\Desktop\待加密文件.txt";
  138.             string outFileName1=@"C:\Users\user\Desktop\第一次加密.txt";
  139.             string key = "diyicijiami";
  140.             TripleDES_ czy = new TripleDES_(key);
  141.             czy.Encrypt(inFileName1, outFileName1);
  142.             string inFileName2 = @"C:\Users\user\Desktop\第一次加密.txt";
  143.             string outFileName2 = @"C:\Users\user\Desktop\第二次解密.txt";
  144.             czy.Decrypt(inFileName2, outFileName2);
  145.         }
  146.     }
  147. }
复制代码

1.建立三个文件
2.调用Encrypt与Decrypt进行加密解密




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

教学服务系统

GMT+8, 2025-4-30 14:28 , Processed in 0.016228 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表