教学服务系统

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

信息计算2019级2班16号程志成

[复制链接]

7

主题

20

帖子

99

积分

注册会员

Rank: 2

积分
99
发表于 2022-4-30 00:12:32 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.IO;

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

  64.     /**//// <summary>
  65.         /// 加密方法File to File
  66.         /// </summary>
  67.         /// <param name="inFileName">待加密文件的路径</param>
  68.         /// <param name="outFileName">待加密后文件的输出路径</param>

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

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

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

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

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

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

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

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

  134.         static void Main(string[] args)
  135.         {
  136.             string inFileName1 = @"D:\密码学第一次作业\1.txt";
  137.             //待加密文件的路径
  138.             string outFileName1 = @"D:\密码学第一次作业\2.txt";
  139.             //加密后文件的输出路径
  140.             string inFileName2 = @"D:\密码学第一次作业\2.txt";
  141.             //待解密文件的路径
  142.             string outFileName2 = @"D:\密码学第一次作业\3.txt";
  143.             //解密后文件的输出路径
  144.             string key = "564765756466";
  145.             //密钥key

  146.             //加解密方法File to File
  147.             TripleDES_ my3des = new TripleDES_(key);
  148.             my3des.Encrypt(inFileName1, outFileName1);
  149.             my3des.Decrypt(inFileName2, outFileName2);
  150.         }
  151.     }
  152. }
复制代码

1.建立三个文件,分别为1,2,3.

2.调用方法Encrypt,对待加密的文件进行加密.
3.调用方法Decrypt,对待解密的文件进行解密。



第一次



第二次




第三次



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:41 , Processed in 0.016565 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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