C#文件加密写入文档

时间:2021-10-26 17:29:24
    /// <summary>
    /// 文件加密
    /// </summary>
    public class FileEncryptDecrypt
    {

        /// <summary>
        /// 加密字符
        /// </summary>
         private static string key = "KANGJINW";


        /// <summary>
        /// 将密钥加密写入到文件
        /// </summary>
        /// <param name="sInputFilename">密密钥</param>
        /// <param name="sOutputFilename">密钥文件路径</param>
        /// <param name="sKey"></param>
        private  static void EncryptFile(string sInputFilename,string sOutputFilename,string sKey)
        {

            FileStream 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 cryptostream = new CryptoStream(fsEncrypted,  desencrypt, CryptoStreamMode.Write);
            byte[] fsInput = System.Text.Encoding.Default.GetBytes(sInputFilename);
            cryptostream.Write(fsInput, 0, fsInput.Length);
            cryptostream.Close();
            fsEncrypted.Close();
        }
        /// <summary>
        /// 打开密钥文件
        /// </summary>
        /// <param name="sInputFilename">密钥路径</param>
        /// <param name="sKey"></param>
        static string DecryptFile(string sInputFilename, string sKey)
        {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();

            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);


            FileStream fsread = new FileStream(sInputFilename,  FileMode.Open,  FileAccess.Read);

            ICryptoTransform desdecrypt = DES.CreateDecryptor();

            CryptoStream cryptostreamDecr = new CryptoStream(fsread,  desdecrypt,  CryptoStreamMode.Read);
            StreamReader read = new StreamReader(cryptostreamDecr, System.Text.Encoding.Default);
            string reft = read.ReadToEnd();
            fsread.Flush();
            fsread.Close();
            return reft;
        }
        /// <summary>
        /// 加密文件
        /// </summary>
        /// <param name="filename">文件存放路径</param>
        /// <param name="soce">加密内容</param>
        public static void Encrypt(string filename, string soce)
        {
            try
            {
                EncryptFile(soce, filename, key);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        /// <summary>
        /// 解密文件
        /// </summary>
        /// <param name="filename">打开文件路径</param>
        /// <returns>返回加密文件的内容</returns>
        public static string Decrypt(string filename)
        {
            try
            {

                return DecryptFile(filename, key);

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }