教学服务系统

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

信息计算2019级2班13号孙鹏

[复制链接]

11

主题

13

帖子

65

积分

注册会员

Rank: 2

积分
65
发表于 2022-5-30 22:20:15 | 显示全部楼层 |阅读模式
思路:使用所给的TripleDES_类的Encrypt方法和Decrypt方法分别进行明文文件的加密和密文文件的解密,将所转换的内容传入到对应的文件中。
具体操作如下:
1、创建三个txt文本,分别为ji1,ji2,ji3。
2、引用类TripleDES_;
3、传入待加密文件的路径inFileName1,加密后文件的输出路径outFileName1,待解密文件的路径outFileName1,解密后文件的输出路径outFileName2;

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;

  5. namespace TriDES
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string key = "jijijijijiji";
  12.             TripleDES_ tripleDES_ = new TripleDES_(key);
  13.             Console.WriteLine("需要加密的文件:");
  14.             string inFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
  15.             Console.WriteLine("加密后的文件:");
  16.             string miFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
  17.             Console.WriteLine("解密后的文件:");
  18.             string outFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
  19.             tripleDES_.Encrypt(inFile,miFile);
  20.             tripleDES_.Decrypt(miFile, outFile);
  21.         }
  22.     }
  23. }
  24.       public void Encrypt(string inFileName, string outFileName)
  25.         {
  26.             try
  27.             {

  28.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  29.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  30.                 fout.SetLength(0);

  31.                 mydes.Key = GetLegalKey();
  32.                 mydes.IV = GetLegalIV();

  33.                 byte[] bin = new byte[100];
  34.                 long rdlen = 0;
  35.                 long totlen = fin.Length;
  36.                 int len;

  37.                 ICryptoTransform encrypto = mydes.CreateEncryptor();
  38.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  39.                 while (rdlen < totlen)
  40.                 {
  41.                     len = fin.Read(bin, 0, 100);
  42.                     cs.Write(bin, 0, len);
  43.                     rdlen = rdlen + len;
  44.                 }
  45.                 cs.Close();
  46.                 fout.Close();
  47.                 fin.Close();

  48.             }
  49.             catch (Exception ex)
  50.             {
  51.                 throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  52.             }
  53.         }

  54. /// 解密方法File to File
  55.         /// </summary>
  56.         /// <param name="inFileName">待解密文件的路径</param>
  57.         /// <param name="outFileName">待解密后文件的输出路径</param>
  58.         public void Decrypt(string inFileName, string outFileName)
  59.         {
  60.             try
  61.             {
  62.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  63.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  64.                 fout.SetLength(0);

  65.                 byte[] bin = new byte[100];
  66.                 long rdlen = 0;
  67.                 long totlen = fin.Length;
  68.                 int len;
  69.                 mydes.Key = GetLegalKey();
  70.                 mydes.IV = GetLegalIV();
  71.                 ICryptoTransform encrypto = mydes.CreateDecryptor();
  72.                 CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  73.                 while (rdlen < totlen)
  74.                 {
  75.                     len = fin.Read(bin, 0, 100);
  76.                     cs.Write(bin, 0, len);
  77.                     rdlen = rdlen + len;
  78.                 }
  79.                 cs.Close();
  80.                 fout.Close();
  81.                 fin.Close();

  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  86.             }
  87.         }

  88.     }
  89. }
复制代码
运行截图


本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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