教学服务系统

 找回密码
 立即注册
搜索
查看: 554|回复: 1

信息计算2019级1班15号刘阔

[复制链接]

10

主题

22

帖子

96

积分

注册会员

Rank: 2

积分
96
发表于 2022-4-15 22:54:29 | 显示全部楼层 |阅读模式
步骤1:创建一个txt.,放上需要加密的内容。步骤2:写主函数进行调用

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


  6. namespace liukuo
  7. {
  8.     class test
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string cokey = "kahjwdoahl=";
  13.             TripleDES_ tri = new TripleDES_(cokey);
  14.             Console.Write("请输入需要加密的文件名:\n");
  15.             string inFileName = @"C:\Users\刘阔\Desktop" + Console.ReadLine() + ".txt";
  16.             Console.Write("请输入加密后文件名:\n");
  17.             string outFileName = @"C:\Users\刘阔\Desktop" + Console.ReadLine() + ".txt";
  18.             Console.Write("请输入解密后文件名:\n");
  19.             string finalFileName = @"C:\Users\刘阔\Desktop" + Console.ReadLine() + ".txt";
  20.             tri.Encrypt(inFileName, outFileName);
  21.             tri.Decrypt(outFileName, finalFileName);

  22.         }
  23.     }
  24. }
复制代码
主要调用的代码:
  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.     }
复制代码
步骤三:运行截图

步骤四:运行结果


本帖子中包含更多资源

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

x
回复

使用道具 举报

10

主题

22

帖子

96

积分

注册会员

Rank: 2

积分
96
 楼主| 发表于 2022-4-15 23:12:31 | 显示全部楼层
另一份文件运行结果:





本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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