教学服务系统

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

信息计算2019级2班5号张欢

[复制链接]

8

主题

23

帖子

108

积分

注册会员

Rank: 2

积分
108
发表于 2022-4-15 22:13:10 | 显示全部楼层 |阅读模式
本帖最后由 张欢 于 2022-4-15 22:51 编辑

3DES是DES加密算法的一种模式,它使用2条不同的56位的密钥对数据进行三次加密,而且每个密钥之间是独立的。

以下为主函数:
  1. static void Main(String[] args){
  2.             TripleDES_ TripleDES_=new TripleDES_("fhaskfsdfakfsdot");
  3.             TripleDES_.Encrypt(@"D:\桌面\密码学1.txt", @"D:\桌面\密码学2.txt");
  4.             TripleDES_.Decrypt(@"D:\桌面\密码学2.txt", @"D:\桌面\密码学3.txt");
  5.         }
复制代码
读取密码学1.txt中的内容,进行3DES加密,再将密文存入密码学2.txt
  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);//使用指定的路径、创建模式、读/写和共享权限、缓冲区大小和同步或异步状态初始化 FileStream 类的新实例。
  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.         }
复制代码
其中,

  1. ICryptoTransform encrypto = mydes.CreateEncryptor();
复制代码
这是定义加密转换的基本操作,用的是mydes,前面用了private TripleDES mydes,就是将mydes定义成3des。


读取密码学2.txt中的内容,然后进行3DES解密。
  1.   /**//// <summary>
  2.         /// 解密方法File to File
  3.         /// </summary>
  4.         /// <param name="inFileName">待解密文件的路径</param>
  5.         /// <param name="outFileName">待解密后文件的输出路径</param>
  6.         public void Decrypt(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.                 byte[] bin = new byte[100];
  14.                 long rdlen = 0;
  15.                 long totlen = fin.Length;
  16.                 int len;
  17.                 mydes.Key = GetLegalKey();
  18.                 mydes.IV = GetLegalIV();
  19.                 ICryptoTransform encrypto = mydes.CreateDecryptor();
  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.         }
复制代码
其中,
  1. ICryptoTransform encrypto = mydes.CreateDecryptor();
复制代码
这是定义解密转换的基本操作,用的是3DES解密。


3DES加解密过程:
  1. private TripleDES mydes;
  2.         public string Key;
  3.         public string IV;

  4.         public TripleDES_(string key)
  5.         {
  6.             mydes = new TripleDESCryptoServiceProvider();
  7.             Key = key;
  8.             IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
  9.         }

  10.         public TripleDES_(string key, string iv)
  11.         {
  12.             mydes = new TripleDESCryptoServiceProvider();
  13.             Key = key;
  14.             IV = iv;
  15.         }


  16.         private byte[] GetLegalKey()
  17.         {
  18.             string sTemp = Key;
  19.             mydes.GenerateKey();
  20.             byte[] bytTemp = mydes.Key;
  21.             int KeyLength = bytTemp.Length;
  22.             if (sTemp.Length > KeyLength)
  23.                 sTemp = sTemp.Substring(0, KeyLength);
  24.             else if (sTemp.Length < KeyLength)
  25.                 sTemp = sTemp.PadRight(KeyLength, ' ');
  26.             return ASCIIEncoding.ASCII.GetBytes(sTemp);
  27.         }

  28.         private byte[] GetLegalIV()
  29.         {
  30.             string sTemp = IV;
  31.             mydes.GenerateIV();
  32.             byte[] bytTemp = mydes.IV;
  33.             int IVLength = bytTemp.Length;
  34.             if (sTemp.Length > IVLength)
  35.                 sTemp = sTemp.Substring(0, IVLength);
  36.             else if (sTemp.Length < IVLength)
  37.                 sTemp = sTemp.PadRight(IVLength, ' ');
  38.             return ASCIIEncoding.ASCII.GetBytes(sTemp);
  39.         }

  40.         public string Encrypt(string Source)
  41.         {
  42.             try
  43.             {
  44.                 byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
  45.                 MemoryStream ms = new MemoryStream();
  46.                 mydes.Key = GetLegalKey();
  47.                 mydes.IV = GetLegalIV();
  48.                 ICryptoTransform encrypto = mydes.CreateEncryptor();// 创建对称加密器对象
  49.                 CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);//将数据流链接到加密转换的流
  50.                 cs.Write(bytIn, 0, bytIn.Length);//将一字节序列写入当前的 CryptoStream,并将通过写入的字节数提前该流的当前位置
  51.                 cs.FlushFinalBlock();//用缓冲区的当前状态更新基础数据源或存储库,随后清除缓冲区。
  52.                 ms.Close();
  53.                 byte[] bytOut = ms.ToArray();
  54.                 return Convert.ToBase64String(bytOut);
  55.             }
  56.             catch (Exception ex)
  57.             {
  58.                 throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
  59.             }
  60.         }

复制代码
运行结果如下:



本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

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

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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