DES加密文件

来源:互联网 发布:apache nginx php并发 编辑:程序博客网 时间:2024/05/28 05:18
 /// <summary>        /// 随机产生密钥        /// </summary>        /// <returns></returns>        static string GenerateKey()        {            // Create an instance of Symetric Algorithm. Key and IV is generated automatically.            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();            // Use the Automatically generated key for Encryption.             return ASCIIEncoding.ASCII.GetString(desCrypto.Key);        }                /// <summary>        /// DES加密文件内容(不支持中文)        /// </summary>        /// <param name="sInputFilename">输入文件名</param>        /// <param name="sOutputFilename">输出文件名</param>        /// <param name="sKey">加密密钥8位</param>        public static void EncryptFileByASCII(string sInputFilename,string sOutputFilename,string sKey)        {               FileStream fsInput = new FileStream(sInputFilename,FileMode.Open,FileAccess.Read);            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[] bytearrayinput = new byte[fsInput.Length];            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);            cryptostream.Close();            fsInput.Close();            fsEncrypted.Close();        }        /// <summary>        /// DES解密文件(不支持中文)        /// </summary>        /// <param name="sInputFilename">输入文件名</param>        /// <param name="sOutputFilename">输出文件名</param>        /// <param name="sKey">解密密钥8位</param>        public static void DecryptFileByASCII(string sInputFilename, string sOutputFilename, string sKey)        {            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.            FileStream 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 the contents of the decrypted file.            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);            fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());            fsDecrypted.Flush();            fsDecrypted.Close();        }        /// <summary>        /// 对文件内容进行DES加密(支持中文)        /// </summary>        /// <param name="sourceFile">待加密的文件绝对路径</param>        /// <param name="destFile">加密后的文件保存的绝对路径</param>        /// <param name="sKey">加密密钥8位</param>        public static void EncryptFileByUnicode(string sourceFile, string destFile,string sKey)        {            if (!File.Exists(sourceFile))            {                throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);            }            byte[] btKey = Encoding.Default.GetBytes(sKey);            byte[] btIV = Encoding.Default.GetBytes(sKey);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] btFile = File.ReadAllBytes(sourceFile);            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))            {                try                {                    using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))                    {                        cs.Write(btFile, 0, btFile.Length);                        cs.FlushFinalBlock();                    }                }                catch                {                    throw;                }                finally                {                    fs.Close();                }            }        }        /// <summary>        /// 对文件内容进行DES加密,加密后覆盖掉原来的文件(支持中文)        /// </summary>        /// <param name="sourceFile">待加密的文件的绝对路径</param>        /// <param name="sKey">加密密钥8位</param>        public static void EncryptFileByUnicode(string sourceFile, string sKey)        {            EncryptFileByUnicode(sourceFile, sourceFile, sKey);        }        /// <summary>        /// 对文件内容进行DES解密(支持中文)        /// </summary>        /// <param name="sourceFile">待解密的文件绝对路径</param>        /// <param name="destFile">解密后的文件保存的绝对路径</param>        /// <param name="sKey">解密密钥8位</param>        public static void DecryptFileByUnicode(string sourceFile, string destFile, string sKey)        {            if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);            byte[] btKey = Encoding.Default.GetBytes(sKey);            byte[] btIV = Encoding.Default.GetBytes(sKey);            DESCryptoServiceProvider des = new DESCryptoServiceProvider();            byte[] btFile = File.ReadAllBytes(sourceFile);            using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))            {                try                {                    using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))                    {                        cs.Write(btFile, 0, btFile.Length);                        cs.FlushFinalBlock();                    }                }                catch                {                    throw;                }                finally                {                    fs.Close();                }            }        }        /// <summary>        /// 对文件内容进行DES解密,加密后覆盖掉原来的文件(支持中文)        /// </summary>        /// <param name="sourceFile">待解密的文件的绝对路径</param>        /// <param name="sKey">解密密钥8位</param>        public static void DecryptFileByUnicode(string sourceFile, string sKey)        {            DecryptFileByUnicode(sourceFile, sourceFile, sKey);        }


 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 成都驾照过期了怎么办 襄阳驾照换证怎么办 外地考驾照居住证怎么办 学驾照没有居住证怎么办 居住证到期驾照还没考完怎么办 买车需要居住证怎么办 住亲友家怎么办居住证 审驾照体检近视怎么办 开车忘带行车证怎么办 韩国转机过境签怎么办 身份证掉了坐车怎么办 科目一考试模拟怎么办 小车驾照过期了怎么办 本地驾驶证掉了怎么办 摩托车驾照脱审怎么办 驾驶证撕坏了怎么办 个体营业执照掉了怎么办 天津驾照丢了怎么办 东莞行驶证丢失怎么办 信用社存折丢了怎么办 没有存折和密码怎么办 行驶证没有照片怎么办 驾证吊销了怎么办 吊销驾照后开车怎么办 外地办行驶证怎么办 驾照考试没过怎么办 驾照考爆了怎么办 考驾照老是不过怎么办 考驾照没时间怎么办 驾照不退学费怎么办 驾照报名费不退怎么办 货车撞人保险金额不够怎么办 科目三不懂灯光怎么办 驾照忘记换证怎么办 小车驾驶证丢了怎么办 天津河西区驾驶证过期怎么办 b2证年审过期怎么办 武汉社保卡到期怎么办 杭州市民卡过期怎么办 外地驾驶证脱审怎么办 没有驾驶证脱审怎么办