教学服务系统

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

信息计算2019级1班9号陈佳怡

[复制链接]

8

主题

15

帖子

84

积分

注册会员

Rank: 2

积分
84
发表于 2022-4-15 21:12:21 | 显示全部楼层 |阅读模式
本帖最后由 19-1-09陈佳怡 于 2022-4-15 22:15 编辑

一 思路
阅读代码后可知,TripleDES_类中构造了对称加密类函数,可获取密钥以及初始向量,已给出了一个文件加密,解密再写入另一个文件具体实现,而要进行程序的实现只需我们提供文件的对应路径,以及相应的密钥利用加解密方法即可实现。
main函数,提供文件的路径以及密钥
  1. static void Main(string[] args)
  2.     {
  3.         string infile = @"C:/Users/86137/Desktop/mima.txt";//密文路径
  4.         string outfile = @"C:/Users/86137/Desktop/m.txt";//加密文件路径
  5.         string outfile2 = @"C:/Users/86137/Desktop/m1.txt";//解密文件路径
  6.         string key = "ascnfadna";//密钥
  7.         TripleDES_ des = new TripleDES_(key);
  8.         des.Encrypt(infile, outfile);
  9.         des.Decrypt(outfile, outfile2);

  10.     }
复制代码


利用文件加密部分(Encrypt
  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.     }
复制代码

利用文件解密部分(Decrypt)
  1. public void Decrypt(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.             byte[] bin = new byte[100];
  9.             long rdlen = 0;
  10.             long totlen = fin.Length;
  11.             int len;
  12.             mydes.Key = GetLegalKey();
  13.             mydes.IV = GetLegalIV();
  14.             ICryptoTransform encrypto = mydes.CreateDecryptor();
  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.     }
复制代码

所需指令集:
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
复制代码



二.运行结果

创建存放加解密信息的文档m.txt m1.txt


密文内容(mima.txt)


运行成功截图1



截图2



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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