教学服务系统

 找回密码
 立即注册
搜索
查看: 558|回复: 1

信息计算2019级1班16号舒圣斌

[复制链接]

8

主题

17

帖子

118

积分

注册会员

Rank: 2

积分
118
发表于 2022-4-5 17:11:06 | 显示全部楼层 |阅读模式

本帖子中包含更多资源

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

x
回复

使用道具 举报

8

主题

17

帖子

118

积分

注册会员

Rank: 2

积分
118
 楼主| 发表于 2022-4-5 18:45:55 | 显示全部楼层
  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Security.Cryptography;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. namespace ConsoleApp3
  8. {
  9.     class Class1
  10.     {
  11.         [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]
  12.         public static extern bool ZeroMemory(IntPtr Destination, int Length);
  13.         static string GenerateKey()
  14.         {
  15.             DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
  16.             return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
  17.         }
  18.         static void EncryptFile(string sInputFilename,string sOutputFilename,string sKey)
  19.         {
  20.             FileStream fsInput = new FileStream(sInputFilename, FileMode.Open,FileAccess.Read);
  21.             FileStream fsEncrypted = new FileStream(sOutputFilename, FileMode.Create, FileAccess.Write);
  22.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  23.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  24.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  25.             ICryptoTransform desencrypt = DES.CreateEncryptor();
  26.             CryptoStream cryptostream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
  27.             byte[] bytearrayinput = new byte[fsInput.Length];
  28.             fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  29.             cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  30.             cryptostream.Close();
  31.             fsInput.Close();
  32.             fsEncrypted.Close();
  33.         }
  34.         static void DecryptFile(string sInputFilename,string sOutputFilename,string sKey)
  35.         {
  36.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  37.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  38.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  39.             FileStream fsread = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read);
  40.             ICryptoTransform desdecrypt = DES.CreateDecryptor();
  41.             CryptoStream cryptostreamDecr = new CryptoStream(fsread,desdecrypt,CryptoStreamMode.Read);
  42.             StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
  43.             fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  44.             fsDecrypted.Flush();
  45.             fsDecrypted.Close();
  46.         }
  47.         static void Main()
  48.         {
  49.             string sSecretKey;
  50.             sSecretKey = GenerateKey();
  51.             GCHandle gch = GCHandle.Alloc(sSecretKey, GCHandleType.Pinned);
  52.             EncryptFile("D:\\MyData.doc", "D:\\Encrypted.doc", sSecretKey);
  53.             DecryptFile("D:\\Encrypted.doc", "D:\\Decrypted.doc", sSecretKey);
  54.             ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);
  55.             gch.Free();
  56.         }
  57.     }
  58. }
复制代码

本帖子中包含更多资源

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

x
回复

使用道具 举报

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

本版积分规则

教学服务系统

GMT+8, 2025-5-6 09:28 , Processed in 0.015780 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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