教学服务系统

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

信息计算2019级2班8号张黎曦

[复制链接]

9

主题

19

帖子

89

积分

注册会员

Rank: 2

积分
89
发表于 2022-4-16 09:55:14 | 显示全部楼层 |阅读模式
本帖最后由 张黎曦 于 2022-4-16 10:01 编辑

先建立三个文本文档打开vs,创建一个c#工程
引进类TripleDES_
传入待加密文件的路径,加密后文件的输出路径
调用类TripleDES_里的Encrypt,对待加密的文件进行加密
调用类TripleDES_里的Decrypt,对待解密的文件进行解密
  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. namespace zlx

  2. {
  3.     class sdd1
  4.     {
  5.         static void Main(string[] args)
  6.         {
  7.             string cokey = "gvssjabdhvyfb";
  8.             TripleDES_ tri = new TripleDES_(cokey);
  9.             Console.WriteLine("请输入此桌面上需要加密的文件名:\n");
  10.             string inFileName = @"D:\DELL"+ Console.ReadLine() + ".txt";
  11.             Console.WriteLine("请输入机密后存放文件的文件名:\n");
  12.             string outFileName = @"D:\DELL" + Console.ReadLine() + ".txt";
  13.             Console.WriteLine("请输入解密后存放文件的文件名:\n");
  14.             string finalFileName = @"D:\DELL" + Console.ReadLine();
  15.             tri.Encrypt(inFileName, outFileName);
  16.             tri.Decrypt(outFileName, finalFileName);
  17.         }
  18.     }
  19. }
复制代码



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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