教学服务系统

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

信息计算2019级2班4号袁敏婷

[复制链接]

8

主题

19

帖子

122

积分

注册会员

Rank: 2

积分
122
发表于 2022-4-15 22:15:45 | 显示全部楼层 |阅读模式
本帖最后由 袁敏婷 于 2022-4-15 23:28 编辑

1、建立一个用于存放原文档的文件
2、编写主函数,通过源文件的路径读取文件内容
3、运行程序,自动生成用于存放加密后的文档内容和解密后的文档内容的文件
4、运行结果如下
测试一:
测试二:
测试三:
5、源代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Security.Cryptography;
  5. using System.IO;
  6.    
  7.         /**//// <summary>
  8.         /// 加密方法File to File
  9.         /// </summary>
  10.         /// <param name="inFileName">待加密文件的路径</param>
  11.         /// <param name="outFileName">待加密后文件的输出路径</param>

  12.         public void Encrypt(string inFileName, string outFileName)
  13.         {
  14.             try
  15.             {
  16.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  17.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  18.                 fout.SetLength(0);
  19.                 mydes.Key = GetLegalKey();
  20.                 mydes.IV = GetLegalIV();
  21.                 byte[] bin = new byte[100];
  22.                 long rdlen = 0;
  23.                 long totlen = fin.Length;
  24.                 int len;
  25.                 ICryptoTransform encrypto = mydes.CreateEncryptor();
  26.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  27.                 while (rdlen < totlen)
  28.                 {
  29.                     len = fin.Read(bin, 0, 100);
  30.                     cs.Write(bin, 0, len);
  31.                     rdlen = rdlen + len;
  32.                 }
  33.                 cs.Close();
  34.                 fout.Close();
  35.                 fin.Close();
  36.             }
  37.             catch (Exception ex)
  38.             {
  39.                 throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  40.             }
  41.         }
  42.         /**//// <summary>
  43.         /// 解密方法File to File
  44.         /// </summary>
  45.         /// <param name="inFileName">待解密文件的路径</param>
  46.         /// <param name="outFileName">待解密后文件的输出路径</param>
  47.         public void Decrypt(string inFileName, string outFileName)
  48.         {
  49.             try
  50.             {
  51.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  52.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  53.                 fout.SetLength(0);

  54.                 byte[] bin = new byte[100];
  55.                 long rdlen = 0;
  56.                 long totlen = fin.Length;
  57.                 int len;
  58.                 mydes.Key = GetLegalKey();
  59.                 mydes.IV = GetLegalIV();
  60.                 ICryptoTransform encrypto = mydes.CreateDecryptor();
  61.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  62.                 while (rdlen < totlen)
  63.                 {
  64.                     len = fin.Read(bin, 0, 100);
  65.                     cs.Write(bin, 0, len);
  66.                     rdlen = rdlen + len;
  67.                 }
  68.                 cs.Close();
  69.                 fout.Close();
  70.                 fin.Close();
  71.             }
  72.             catch (Exception ex)
  73.             {
  74.                 throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  75.             }
  76.         }
  77.     }
  78. namespace ymtDES
  79. {
  80.     class Program
  81.     {
  82.         static void Main(string[] args)
  83.         {
  84.             string key = "cuifhoeiwhgfui";
  85.             TripleDES_ ymt = new TripleDES_(key);

  86.             Console.Write("请输入需要加密的文件名:");
  87.             string inFileName = @"C:\Users\丫丫\Desktop" + Console.ReadLine() + ".docx";
  88.             Console.Write("请输入加密后的文件名:");
  89.             string outFileName = @"C:\Users\丫丫\Desktop" + Console.ReadLine() + ".docx";
  90.             Console.Write("请输入解密后的文件名:");
  91.             string finalFileName = @"C:\Users\丫丫\Desktop" + Console.ReadLine() + ".docx";

  92.             ymt.Encrypt(inFileName, outFileName);
  93.             ymt.Decrypt(outFileName, finalFileName);
  94.         }
  95.     }
  96. }
复制代码



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 12:42 , Processed in 0.015689 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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