教学服务系统

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

信息计算2019级1班23号姚俣譞

[复制链接]

12

主题

17

帖子

82

积分

注册会员

Rank: 2

积分
82
发表于 2022-4-15 20:14:07 | 显示全部楼层 |阅读模式
阅读所给文件后,了解主要用到两个方法:加密方法和解密方法

  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. /// 解密方法File to File
  32.         /// </summary>
  33.         /// <param name="inFileName">待解密文件的路径</param>
  34.         /// <param name="outFileName">待解密后文件的输出路径</param>
  35.         public void Decrypt(string inFileName, string outFileName)
  36.         {
  37.             try
  38.             {
  39.                 FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  40.                 FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  41.                 fout.SetLength(0);

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

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

  65.     }
  66. }
复制代码
思路:1.在桌面创建三个文件夹分别用于存放原文、存放密文、存放译文


2.编写main函数对方法进行引用
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace TriDES
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string key = "jijijijijiji";
  13.             TripleDES_ tripleDES_ = new TripleDES_(key);
  14.             Console.WriteLine("需要加密的文件:");
  15.             string inFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
  16.             Console.WriteLine("加密后的文件:");
  17.             string miFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
  18.             Console.WriteLine("解密后的文件:");
  19.             string outFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
  20.             tripleDES_.Encrypt(inFile,miFile);
  21.             tripleDES_.Decrypt(miFile, outFile);
  22.         }
  23.     }
  24. }
复制代码
运行程序如下:

上述三个文件如下:
原文:

密文:

译文:

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-9-20 00:06 , Processed in 0.016949 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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