教学服务系统

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

信息计算2019级1班25号王庆海

[复制链接]

11

主题

18

帖子

95

积分

注册会员

Rank: 2

积分
95
发表于 2022-4-15 20:21:08 | 显示全部楼层 |阅读模式
本帖最后由 王庆海 于 2022-4-15 20:22 编辑

原理:
3des算法主要采用了下列两个类的算法
  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.     }
复制代码
步骤:
<1> 创建文件夹存放需要加密的原文

<2> 创建主函数实现对加密算法的调用
  1. using System;


  2. namespace ConsoleApp2
  3. {
  4.     class des1
  5.     {
  6.         static void Main(string[] args)
  7.         {
  8.             string cokey = "tkGGRmBErvc=";
  9.             TripleDES_ tri = new TripleDES_(cokey);
  10.             Console.Write("请输入此桌面上需要加密的文件名:\n");
  11.             string inFileName = @"C:\Users\86176\Desktop" + Console.ReadLine() + ".txt";
  12.             Console.Write("请输入加密后存放的文件名:\n");
  13.             string outFileName = @"C:\Users\86176\Desktop" + Console.ReadLine() + ".txt";
  14.             Console.Write("请输入解密后存放的文件名:\n");
  15.             string finalFileName = @"C:\Users\86176\Desktop" + Console.ReadLine() + ".txt";
  16.             tri.Encrypt(inFileName, outFileName);
  17.             tri.Decrypt(outFileName, finalFileName);

  18.         }
  19.     }
  20. }
复制代码
运行界面:

原文:

加密文件夹:

解密文件夹:

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 07:37 , Processed in 0.014781 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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