教学服务系统

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

信息计算2019级1班8号林荟萃

[复制链接]

8

主题

20

帖子

92

积分

注册会员

Rank: 2

积分
92
发表于 2022-4-15 18:30:05 | 显示全部楼层 |阅读模式
本帖最后由 108林荟萃 于 2022-4-15 18:39 编辑

一、3DES加解密算法简介:
      3DES是对称加密的一种,是DES向AES过渡的加密算法。它使用三个秘钥的三重DES加密方法,该算法执行三次DES算法,其加密的过程是加密-解密-加密。而3DES解密过程,与加密过程相反,即逆序使用秘钥并执行解密-加密-解密的过程。

二、利用所提供的TripleDES_类写一个文件加解密程序:
1.具体思路:
(1) 本次作业要求对文件进行读取加解密,阅读TripleDES_类的代码后可知,主要用到的部分是:
  1. public void Encrypt(string inFileName, string outFileName)
  2.         {
  3.             try
  4.             {

  5.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  6.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  7.                 fout.SetLength(0);

  8.                 mydes.Key = GetLegalKey();
  9.                 mydes.IV = GetLegalIV();

  10.                 byte[] bin = new byte[100];
  11.                 long rdlen = 0;
  12.                 long totlen = fin.Length;
  13.                 int len;

  14.                 ICryptoTransform encrypto = mydes.CreateEncryptor();
  15.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  16.                 while (rdlen < totlen)
  17.                 {
  18.                     len = fin.Read(bin, 0, 100);
  19.                     cs.Write(bin, 0, len);
  20.                     rdlen = rdlen + len;
  21.                 }
  22.                 cs.Close();
  23.                 fout.Close();
  24.                 fin.Close();

  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  29.             }
  30.         }
  31.         /**//// <summary>
  32.         /// 解密方法File to File
  33.         /// </summary>
  34.         /// <param name="inFileName">待解密文件的路径</param>
  35.         /// <param name="outFileName">待解密后文件的输出路径</param>
  36.         public void Decrypt(string inFileName, string outFileName)
  37.         {
  38.             try
  39.             {
  40.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  41.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  42.                 fout.SetLength(0);

  43.                 byte[] bin = new byte[100];
  44.                 long rdlen = 0;
  45.                 long totlen = fin.Length;
  46.                 int len;
  47.                 mydes.Key = GetLegalKey();
  48.                 mydes.IV = GetLegalIV();
  49.                 ICryptoTransform encrypto = mydes.CreateDecryptor();
  50.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  51.                 while (rdlen < totlen)
  52.                 {
  53.                     len = fin.Read(bin, 0, 100);
  54.                     cs.Write(bin, 0, len);
  55.                     rdlen = rdlen + len;
  56.                 }
  57.                 cs.Close();
  58.                 fout.Close();
  59.                 fin.Close();

  60.             }
  61.             catch (Exception ex)
  62.             {
  63.                 throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  64.             }
  65.         }
复制代码

其中,第一个为加密方法,inFileName为待加密文件的路径,outFileName为待加密后文件的输出路径; 第二个为解密方法,inFileName为待解密文件的路径,outFileName为待解密后文件的输出路径;

3. 阅读分析代码后可知,只需在Main函数内提供各文件的路径并传入相关函数中运行即可。

4. 编写的Main函数以实现加解密方法:
  1. static void Main(string[] args)
  2.         {
  3.             string key = "123456789101223344556788";
  4.             string inFileName =@"C:\Users\13337525256\Desktop\3DES\testIn.docx";
  5.             string outFileName =@"C:\Users\13337525256\Desktop\3DES\testOut.docx";
  6.             string FileName = @"C:\Users\13337525256\Desktop\3DES\testFinal.docx";
  7.             TripleDES_ tripleDES_ = new TripleDES_(key);
  8.             tripleDES_.Encrypt(inFileName, outFileName);
  9.             tripleDES_.Decrypt(outFileName, FileName);
  10.         }
复制代码

三、运行过程与结果:
1.在文件夹内创建三个文件,命名分别为testIn.docx(需要加密的文档)、testOut.docx(用于加密后的内容存放)、testFinal.docx(用于解密后的内容存放);


C:\Users\13337525256\Desktop\1.png
2.运行代码程序:


C:\Users\13337525256\Desktop\2.png
3.查看创建的三个文件:


C:\Users\13337525256\Desktop\4.png





本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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