教学服务系统

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

信息计算2019级1班29号敖若瑶

[复制链接]

8

主题

18

帖子

124

积分

注册会员

Rank: 2

积分
124
发表于 2022-4-16 00:50:11 | 显示全部楼层 |阅读模式
本帖最后由 信息计算敖若瑶 于 2022-4-16 00:52 编辑

一、源代码

1.通过引用TripleDES-类中的加密方法Encrypt和解密方法Decrypt进行加密和解密
  1. /**//// <summary>
  2.         /// 加密方法File to File
  3.         /// </summary>
  4.         /// <param name="inFileName">待加密文件的路径</param>
  5.         /// <param name="outFileName">待加密后文件的输出路径</param>

  6.     public void Encrypt(string inFileName, string outFileName)
  7.     {
  8.         try
  9.         {

  10.             FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  11.             FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  12.             fout.SetLength(0);

  13.             mydes.Key = GetLegalKey();
  14.             mydes.IV = GetLegalIV();

  15.             byte[] bin = new byte[100];
  16.             long rdlen = 0;
  17.             long totlen = fin.Length;
  18.             int len;

  19.             ICryptoTransform encrypto = mydes.CreateEncryptor();
  20.             CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  21.             while (rdlen < totlen)
  22.             {
  23.                 len = fin.Read(bin, 0, 100);
  24.                 cs.Write(bin, 0, len);
  25.                 rdlen = rdlen + len;
  26.             }
  27.             cs.Close();
  28.             fout.Close();
  29.             fin.Close();

  30.         }
  31.         catch (Exception ex)
  32.         {
  33.             throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  34.         }
  35.     }
  36.     /**//// <summary>
  37.         /// 解密方法File to File
  38.         /// </summary>
  39.         /// <param name="inFileName">待解密文件的路径</param>
  40.         /// <param name="outFileName">待解密后文件的输出路径</param>
  41.     public void Decrypt(string inFileName, string outFileName)
  42.     {
  43.         try
  44.         {
  45.             FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  46.             FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  47.             fout.SetLength(0);

  48.             byte[] bin = new byte[100];
  49.             long rdlen = 0;
  50.             long totlen = fin.Length;
  51.             int len;
  52.             mydes.Key = GetLegalKey();
  53.             mydes.IV = GetLegalIV();
  54.             ICryptoTransform encrypto = mydes.CreateDecryptor();
  55.             CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  56.             while (rdlen < totlen)
  57.             {
  58.                 len = fin.Read(bin, 0, 100);
  59.                 cs.Write(bin, 0, len);
  60.                 rdlen = rdlen + len;
  61.             }
  62.             cs.Close();
  63.             fout.Close();
  64.             fin.Close();

  65.         }
  66.         catch (Exception ex)
  67.         {
  68.             throw new Exception("在文件解密的时候出现错误!错误提示:" + ex.Message);
  69.         }
  70.     }
复制代码



2.主程序:通过创建txt文件,来对其进行加密、解密
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Security.Cryptography;

  5. namespace DES
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //待加密文件的路径
  12.             string one = @"/Users/gesilasapple/Desktop/1.1.txt";
  13.             //加密后文件的输出路径
  14.             string two1 = @"/Users/gesilasapple/Desktop/1.2.txt";
  15.             //待解密文件的路径
  16.             string two2 = @"/Users/gesilasapple/Desktop/1.2.txt";
  17.             //解密后文件的输出路径
  18.             string three = @"/Users/gesilasapple/Desktop/1.3.txt";

  19.             //密钥key
  20.             string key = "220415@";

  21.             //加/解密方法File to File
  22.             TripleDES_ des = new TripleDES_(key);
  23.             des.Encrypt(one, two1);
  24.             des.Decrypt(two2, three);
  25.         }
  26.     }
复制代码

二、程序运行截图

1.实例一:



2.实例2:








本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-4-30 16:19 , Processed in 0.016036 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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