教学服务系统

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

信息计算2019级1班27号许鹏程

[复制链接]

8

主题

23

帖子

110

积分

注册会员

Rank: 2

积分
110
发表于 2022-4-16 11:54:07 | 显示全部楼层 |阅读模式

  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;

  5. /**/

  6. /// <summary>
  7. /// 三重DES
  8. /// </summary>
  9. public class TripleDES_
  10. {
  11.     private TripleDES mydes;
  12.     public string Key;
  13.     public string IV;
  14.     /**//// <summary>
  15.         /// 对称加密类的构造函数
  16.         /// </summary>
  17.     public TripleDES_(string key)
  18.     {
  19.         mydes = new TripleDESCryptoServiceProvider();
  20.         Key = key;
  21.         IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
  22.     }
  23.     /**//// <summary>
  24.         /// 对称加密类的构造函数
  25.         /// </summary>
  26.     public TripleDES_(string key, string iv)
  27.     {
  28.         mydes = new TripleDESCryptoServiceProvider();
  29.         Key = key;
  30.         IV = iv;
  31.     }
  32.     /**//// <summary>
  33.         /// 获得密钥
  34.         /// </summary>
  35.         /// <returns>密钥</returns>
  36.     private byte[] GetLegalKey()
  37.     {
  38.         string sTemp = Key;
  39.         mydes.GenerateKey();
  40.         byte[] bytTemp = mydes.Key;
  41.         int KeyLength = bytTemp.Length;
  42.         if (sTemp.Length > KeyLength)
  43.             sTemp = sTemp.Substring(0, KeyLength);
  44.         else if (sTemp.Length < KeyLength)
  45.             sTemp = sTemp.PadRight(KeyLength, ' ');
  46.         return ASCIIEncoding.ASCII.GetBytes(sTemp);
  47.     }
  48.     /**//// <summary>
  49.         /// 获得初始向量IV
  50.         /// </summary>
  51.         /// <returns>初试向量IV</returns>
  52.     private byte[] GetLegalIV()
  53.     {
  54.         string sTemp = IV;
  55.         mydes.GenerateIV();
  56.         byte[] bytTemp = mydes.IV;
  57.         int IVLength = bytTemp.Length;
  58.         if (sTemp.Length > IVLength)
  59.             sTemp = sTemp.Substring(0, IVLength);
  60.         else if (sTemp.Length < IVLength)
  61.             sTemp = sTemp.PadRight(IVLength, ' ');
  62.         return ASCIIEncoding.ASCII.GetBytes(sTemp);
  63.     }
  64.     /**//// <summary>
  65.         /// 加密方法
  66.         /// </summary>
  67.         /// <param name="Source">待加密的串</param>
  68.         /// <returns>经过加密的串</returns>
  69.     public string Encrypt(string Source)
  70.     {
  71.         try
  72.         {
  73.             byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
  74.             MemoryStream ms = new MemoryStream();
  75.             mydes.Key = GetLegalKey();
  76.             mydes.IV = GetLegalIV();
  77.             ICryptoTransform encrypto = mydes.CreateEncryptor();
  78.             CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
  79.             cs.Write(bytIn, 0, bytIn.Length);
  80.             cs.FlushFinalBlock();
  81.             ms.Close();
  82.             byte[] bytOut = ms.ToArray();
  83.             return Convert.ToBase64String(bytOut);
  84.         }
  85.         catch (Exception ex)
  86.         {
  87.             throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  88.         }
  89.     }
  90.     /**//// <summary>
  91.         /// 解密方法
  92.         /// </summary>
  93.         /// <param name="Source">待解密的串</param>
  94.         /// <returns>经过解密的串</returns>
  95.     public string Decrypt(string Source)
  96.     {
  97.         try
  98.         {
  99.             byte[] bytIn = Convert.FromBase64String(Source);
  100.             MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
  101.             mydes.Key = GetLegalKey();
  102.             mydes.IV = GetLegalIV();
  103.             ICryptoTransform encrypto = mydes.CreateDecryptor();
  104.             CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
  105.             StreamReader sr = new StreamReader(cs);
  106.             return sr.ReadToEnd();
  107.         }
  108.         catch (Exception ex)
  109.         {
  110.             throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  111.         }
  112.     }
  113.     /**//// <summary>
  114.         /// 加密方法byte[] to byte[]
  115.         /// </summary>
  116.         /// <param name="Source">待加密的byte数组</param>
  117.         /// <returns>经过加密的byte数组</returns>
  118.     public byte[] Encrypt(byte[] Source)
  119.     {
  120.         try
  121.         {
  122.             byte[] bytIn = Source;
  123.             MemoryStream ms = new MemoryStream();
  124.             mydes.Key = GetLegalKey();
  125.             mydes.IV = GetLegalIV();
  126.             ICryptoTransform encrypto = mydes.CreateEncryptor();
  127.             CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
  128.             cs.Write(bytIn, 0, bytIn.Length);
  129.             cs.FlushFinalBlock();
  130.             ms.Close();
  131.             byte[] bytOut = ms.ToArray();
  132.             return bytOut;
  133.         }
  134.         catch (Exception ex)
  135.         {
  136.             throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  137.         }
  138.     }
  139.     /**//// <summary>
  140.         /// 解密方法byte[] to byte[]
  141.         /// </summary>
  142.         /// <param name="Source">待解密的byte数组</param>
  143.         /// <returns>经过解密的byte数组</returns>
  144.     public byte[] Decrypt(byte[] Source)
  145.     {
  146.         try
  147.         {
  148.             byte[] bytIn = Source;
  149.             MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
  150.             mydes.Key = GetLegalKey();
  151.             mydes.IV = GetLegalIV();
  152.             ICryptoTransform encrypto = mydes.CreateDecryptor();
  153.             CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
  154.             StreamReader sr = new StreamReader(cs);
  155.             return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd());
  156.         }
  157.         catch (Exception ex)
  158.         {
  159.             throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  160.         }
  161.     }

  162.     /**//// <summary>
  163.         /// 加密方法File to File
  164.         /// </summary>
  165.         /// <param name="inFileName">待加密文件的路径</param>
  166.         /// <param name="outFileName">待加密后文件的输出路径</param>

  167.     public void Encrypt(string inFileName, string outFileName)
  168.     {
  169.         try
  170.         {

  171.             FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  172.             FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  173.             fout.SetLength(0);

  174.             mydes.Key = GetLegalKey();
  175.             mydes.IV = GetLegalIV();

  176.             byte[] bin = new byte[100];
  177.             long rdlen = 0;
  178.             long totlen = fin.Length;
  179.             int len;

  180.             ICryptoTransform encrypto = mydes.CreateEncryptor();
  181.             CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  182.             while (rdlen < totlen)
  183.             {
  184.                 len = fin.Read(bin, 0, 100);
  185.                 cs.Write(bin, 0, len);
  186.                 rdlen = rdlen + len;
  187.             }
  188.             cs.Close();
  189.             fout.Close();
  190.             fin.Close();

  191.         }
  192.         catch (Exception ex)
  193.         {
  194.             throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  195.         }
  196.     }
  197.     /**//// <summary>
  198.         /// 解密方法File to File
  199.         /// </summary>
  200.         /// <param name="inFileName">待解密文件的路径</param>
  201.         /// <param name="outFileName">待解密后文件的输出路径</param>
  202.     public void Decrypt(string inFileName, string outFileName)
  203.     {
  204.         try
  205.         {
  206.             FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
  207.             FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
  208.             fout.SetLength(0);

  209.             byte[] bin = new byte[100];
  210.             long rdlen = 0;
  211.             long totlen = fin.Length;
  212.             int len;
  213.             mydes.Key = GetLegalKey();
  214.             mydes.IV = GetLegalIV();
  215.             ICryptoTransform encrypto = mydes.CreateDecryptor();
  216.             CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
  217.             while (rdlen < totlen)
  218.             {
  219.                 len = fin.Read(bin, 0, 100);
  220.                 cs.Write(bin, 0, len);
  221.                 rdlen = rdlen + len;
  222.             }
  223.             cs.Close();
  224.             fout.Close();
  225.             fin.Close();

  226.         }
  227.         catch (Exception ex)
  228.         {
  229.             throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
  230.         }
  231.     }


  232.     public static void Main(string[] args)
  233.     {
  234.         string key = "xpcxhhxyxpcxhh";
  235.         TripleDES_ triple = new TripleDES_(key);
  236.         string beencrypted = @"C:\Users\20997\Desktop\mimaxue.txt";
  237.         string bedecrypted = @"C:\Users\20997\Desktop\mamixue.txt";
  238.         string encrypted = @"C:\Users\20997\Desktop\xuemima.txt";
  239.         
  240.         triple.Encrypt(beencrypted, bedecrypted);
  241.         triple.Decrypt(bedecrypted, encrypted);
  242.     }
  243. }


复制代码


通过定义一个密钥,再实例化这个类,然后把需要加密的文件路径定好,以及加密后的文件路径和解密后的文件路径找到。再调用Encrypt方法将加密的文件加密并存入加密后的文件路径,然后调用Decrypt方法将加密后的文件解密并存入解密后的文件路径。

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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