EncryptFunction.cs
上传用户:lyg_rssy
上传日期:2022-04-14
资源大小:104k
文件大小:5k
源码类别:

数据库系统

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Security.Cryptography;
  7. namespace HKBU_DataExport
  8. {
  9.     static class EncryptFunction
  10.     {
  11.         /// <summary>
  12.         /// 加密文件
  13.         /// </summary>
  14.         /// <param name="sInputFile">源文件</param>
  15.         /// <param name="sOutputFile">加密过后的文件</param>
  16.         /// <param name="sKey">密钥</param>
  17.         /// <param name="sMsg">返回信息</param>
  18.         /// <returns>是否成功</returns>
  19.         public static bool EncryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
  20.         {
  21.             bool IsSuccess = true;
  22.             FileStream fsInput = null;
  23.             FileStream fsEncrypted = null;
  24.             CryptoStream cryptostream = null;
  25.             try
  26.             {
  27.                 fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
  28.                 fsEncrypted = new FileStream(sOutputFilename,
  29.                                                         FileMode.Create,
  30.                                                         FileAccess.Write);
  31.                 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  32.                 DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  33.                 DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  34.                 ICryptoTransform desencrypt = DES.CreateEncryptor();
  35.                 cryptostream = new CryptoStream(fsEncrypted,
  36.                                                             desencrypt,
  37.                                                             CryptoStreamMode.Write);
  38.                 byte[] bytearrayinput = new byte[fsInput.Length];
  39.                 fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
  40.                 cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
  41.                 cryptostream.Close();
  42.                 fsInput.Close();
  43.                 fsEncrypted.Close();
  44.                 sMsg = "Encrypted Success";
  45.                 return true;
  46.             }
  47.             catch (Exception ex)
  48.             {
  49.                 sMsg = "Encrypted Failed: " + ex.Message;
  50.                 IsSuccess = false;
  51.             }
  52.             finally
  53.             {
  54.                 try
  55.                 {
  56.                     if (cryptostream != null) cryptostream.Close();
  57.                     if (fsInput != null) fsInput.Close();
  58.                     if (fsEncrypted != null) fsEncrypted.Close();
  59.                 }
  60.                 catch
  61.                 {
  62.                 }
  63.             }
  64.             return IsSuccess;
  65.         }
  66.         public static bool DecryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
  67.         {
  68.             FileStream fsread = null;
  69.             StreamWriter fsDecrypted = null;
  70.             bool isSuccess = true;
  71.             try
  72.             {
  73.                 DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
  74.                 //A 64 bit key and IV is required for this provider.
  75.                 //Set secret key For DES algorithm.
  76.                 DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
  77.                 //Set initialization vector.
  78.                 DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
  79.                 //Create a file stream to read the encrypted file back.
  80.                 fsread = new FileStream(sInputFilename,
  81.                                                FileMode.Open,
  82.                                                FileAccess.Read);
  83.                 //Create a DES decryptor from the DES instance.
  84.                 ICryptoTransform desdecrypt = DES.CreateDecryptor();
  85.                 //Create crypto stream set to read and do a
  86.                 //DES decryption transform on incoming bytes.
  87.                 CryptoStream cryptostreamDecr = new CryptoStream(fsread,
  88.                                                              desdecrypt,
  89.                                                              CryptoStreamMode.Read);
  90.                 //Print out the contents of the decrypted file.
  91.                 fsDecrypted = new StreamWriter(sOutputFilename);
  92.                 fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
  93.                 fsDecrypted.Flush();
  94.                 fsDecrypted.Close();
  95.                 sMsg = "Decrypted Successed";
  96.             }
  97.             catch (Exception ex)
  98.             {
  99.                 sMsg = "Decrypted Failed: " + ex.Message;
  100.                 isSuccess = false;
  101.             }
  102.             finally
  103.             {
  104.                 try
  105.                 {
  106.                     if (fsread != null) fsread.Close();
  107.                     if (fsDecrypted != null) fsDecrypted.Close();
  108.                 }
  109.                 catch
  110.                 {
  111.                 }
  112.             }
  113.             return isSuccess;
  114.         }
  115.     }
  116. }