EncryptFunction.cs
上传用户:lyg_rssy
上传日期:2022-04-14
资源大小:104k
文件大小:5k
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Security.Cryptography;
- namespace HKBU_DataExport
- {
- static class EncryptFunction
- {
- /// <summary>
- /// 加密文件
- /// </summary>
- /// <param name="sInputFile">源文件</param>
- /// <param name="sOutputFile">加密过后的文件</param>
- /// <param name="sKey">密钥</param>
- /// <param name="sMsg">返回信息</param>
- /// <returns>是否成功</returns>
- public static bool EncryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
- {
- bool IsSuccess = true;
- FileStream fsInput = null;
- FileStream fsEncrypted = null;
- CryptoStream cryptostream = null;
- try
- {
- fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
- fsEncrypted = new FileStream(sOutputFilename,
- FileMode.Create,
- FileAccess.Write);
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- ICryptoTransform desencrypt = DES.CreateEncryptor();
- cryptostream = new CryptoStream(fsEncrypted,
- desencrypt,
- CryptoStreamMode.Write);
- byte[] bytearrayinput = new byte[fsInput.Length];
- fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
- cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
- cryptostream.Close();
- fsInput.Close();
- fsEncrypted.Close();
- sMsg = "Encrypted Success";
- return true;
- }
- catch (Exception ex)
- {
- sMsg = "Encrypted Failed: " + ex.Message;
- IsSuccess = false;
- }
- finally
- {
- try
- {
- if (cryptostream != null) cryptostream.Close();
- if (fsInput != null) fsInput.Close();
- if (fsEncrypted != null) fsEncrypted.Close();
- }
- catch
- {
- }
- }
- return IsSuccess;
- }
- public static bool DecryptFile(string sInputFilename, string sOutputFilename, string sKey, out string sMsg)
- {
- FileStream fsread = null;
- StreamWriter fsDecrypted = null;
- bool isSuccess = true;
- try
- {
- DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
- //A 64 bit key and IV is required for this provider.
- //Set secret key For DES algorithm.
- DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
- //Set initialization vector.
- DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
- //Create a file stream to read the encrypted file back.
- fsread = new FileStream(sInputFilename,
- FileMode.Open,
- FileAccess.Read);
- //Create a DES decryptor from the DES instance.
- ICryptoTransform desdecrypt = DES.CreateDecryptor();
- //Create crypto stream set to read and do a
- //DES decryption transform on incoming bytes.
- CryptoStream cryptostreamDecr = new CryptoStream(fsread,
- desdecrypt,
- CryptoStreamMode.Read);
- //Print out the contents of the decrypted file.
- fsDecrypted = new StreamWriter(sOutputFilename);
- fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
- fsDecrypted.Flush();
- fsDecrypted.Close();
- sMsg = "Decrypted Successed";
- }
- catch (Exception ex)
- {
- sMsg = "Decrypted Failed: " + ex.Message;
- isSuccess = false;
- }
- finally
- {
- try
- {
- if (fsread != null) fsread.Close();
- if (fsDecrypted != null) fsDecrypted.Close();
- }
- catch
- {
- }
- }
- return isSuccess;
- }
- }
- }