教学服务系统

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

信息计算2019级1班19号谭斌斌

[复制链接]

9

主题

16

帖子

75

积分

注册会员

Rank: 2

积分
75
发表于 2022-4-15 19:46:42 | 显示全部楼层 |阅读模式
TripleDES主要用到了以下两个方法:
  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.txt 、2.txt、 3.txt分别对应Encrypt和Decrypt方法中的inFileName、outFileName。

主函数中引用TripleDES_类实行加密如下
  1. string key = "oojyjyjyjyjyoo";
  2.             TripleDES_ triDES = new TripleDES_(key);
  3.             Console.WriteLine("需要加密的文件名为:");
  4.             string inputOriginalFile = @"C:\Users\Lenovo\Desktop" + Console.ReadLine() + ".txt";
  5.             Console.WriteLine("加密后的文件名为:");
  6.             string encryptedFile = @"C:\Users\Lenovo\Desktop" + Console.ReadLine() + ".txt";
  7.             Console.WriteLine("解密后的文件名为:");
  8.             string outputOriginalFile = @"C:\Users\Lenovo\Desktop" + Console.ReadLine() + ".txt";
  9.             triDES.Encrypt(inputOriginalFile, encryptedFile);
  10.             triDES.Decrypt(encryptedFile, outputOriginalFile);
复制代码


运行界面:


原文:


密文:


解密后的原文:

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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