教学服务系统

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

信息计算2019级1班3号刘丹丹

[复制链接]

9

主题

19

帖子

99

积分

注册会员

Rank: 2

积分
99
发表于 2022-4-15 21:20:38 | 显示全部楼层 |阅读模式
一.大体思路:
使用老师给的TripleDES_类的Encrypt方法和Decrypt方法进行文件的加密解密,最后将所转换的内容传入到对应的文件中。
  Encrypt方法:此方法主要通过传入一个原文的文件地址,方法的作用则是用来对原文加密,并且将加密后的数据放入传入的第二个参数指向的文件,这里我们指的是加密文件(Encrypt.txt)
  Decrypt方法:此方法通过将传入的密文文件中的内容进行解密,并且将解密后的内容传入第二个参数所指向的文件,这里指的是解密文件(Decrypt.txt)。


  1. /**//// <summary>
  2.         /// 加密方法File to File
  3.         /// </summary>
  4.         /// <param name="inFileName">待加密文件的路径</param>
  5.         /// <param name="outFileName">待加密后文件的输出路径</param>

  6.         public void Encrypt(string inFileName, string outFileName)
  7.         {
  8.             try
  9.             {

  10.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  11.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  12.                 fout.SetLength(0);

  13.                 mydes.Key = GetLegalKey();
  14.                 mydes.IV = GetLegalIV();

  15.                 byte[] bin = new byte[100];
  16.                 long rdlen = 0;
  17.                 long totlen = fin.Length;
  18.                 int len;

  19.                 ICryptoTransform encrypto = mydes.CreateEncryptor();
  20.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  21.                 while (rdlen < totlen)
  22.                 {
  23.                     len = fin.Read(bin, 0, 100);
  24.                     cs.Write(bin, 0, len);
  25.                     rdlen = rdlen + len;
  26.                 }
  27.                 cs.Close();
  28.                 fout.Close();
  29.                 fin.Close();

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

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

  65.             }
  66.             catch (Exception ex)
  67.             {
  68.                 throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  69.             }
  70.         }
复制代码


二.运行截图
1.创建的三个txt文件

从左到右分别是原文文件、加密文件、解密文件

2.不同的例子的运行截图
(1)例一:



(2)例二:




(3)例三:



三.源代码:
(1)主程序代码:
  1. using System;

  2. namespace ldd_3DES
  3. {
  4.     class Program
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             TripleDES_ trides = new TripleDES_("jdsafueaofjwoifoiweoif");
  9.             string primetxt = "C:\\Users\\HP\\Desktop\\Prime.txt";
  10.             string Encrypttxt = "C:\\Users\\HP\\Desktop\\Encrypt.txt";
  11.             string Decrypttxt = "C:\\Users\\HP\\Desktop\\Decrypt.txt";
  12.             trides.Encrypt(primetxt, Encrypttxt);
  13.             trides.Decrypt(Encrypttxt, Decrypttxt);
  14.         }
  15.     }
  16. }
复制代码




(2)启动程序截图:

在更改原文文件中的内容后,启动程序,则对应加密解密文件中的内容相应变化

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 11:50 , Processed in 0.015608 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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