码迷,mamicode.com
首页 > 其他好文 > 详细

文件加密写入文档

时间:2016-12-05 02:22:35      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:[]   .text   toe   sys   解密   写入   文件路径   cat   open   

    /// <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;
            }
        }
    }

 

文件加密写入文档

标签:[]   .text   toe   sys   解密   写入   文件路径   cat   open   

原文地址:http://www.cnblogs.com/kangjin/p/6132493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!