admin管理员组

文章数量:1639677

最近工作中遇到的问题,需要对epub进行加密,浏览了几种加密后最后敲定的使用该方法进行加密解密,自己就稍微写了些,可以实现加密解密,但是里面还有很多不完善的地方。谢谢日志,做做mark,后期继续改进。

不在废话,上源码。


        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="toEncrypt">要加密的内容</param>
        /// <param name="strKey">密钥(16或者32位)</param>
        /// <returns>Base64转码后的密文</returns>
        public static bool Encrypt(string readPath, string writePath, string strKey)
        {
            bool IsSuccess = false;
            if (string.IsNullOrEmpty(readPath))
            {
                IsSuccess = false;
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(writePath))
            {
                IsSuccess = false;
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(strKey))
            {
                IsSuccess = false;
                return IsSuccess;
            }
            try
            {
                //读取要加密的文件
                byte[] toEncryptArray = null;
                using (FileStream readFile = new FileStream(readPath, FileMode.Open, FileAccess.Read))
                {
                    toEncryptArray = new byte[readFile.Length];
                    readFile.Read(toEncryptArray, 0, toEncryptArray.Length);
                    readFile.Seek(0, SeekOrigin.Begin);
                    readFile.Flush();
                    readFile.Close();
                    readFile.Dispose();
                }

                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = UTF8Encoding.UTF8.GetBytes(strKey);            //指定加解密使用的密钥
                rDel.Mode = CipherMode.ECB;                               //设置对称算法的运算模式
                rDel.Padding = PaddingMode.ISO10126;                      //设置填充模式
                ICryptoTransform cTransform = rDel.CreateEncryptor();     //创建解密对象
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);    //加密
                //将加密后的数据写入本地
                using (FileStream fstream = new FileStream(writePath, FileMode.Append, FileAccess.Write))
                {
                    fstream.Write(resultArray, 0, resultArray.Length);
                    fstream.Flush();
                    fstream.Close();
                    fstream.Dispose();
                }
                rDel.Clear();//释放所有使用的资源
                IsSuccess = true;
                return IsSuccess;
            }
            catch (Exception)
            {
                return false;
            }
          
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="readPath">加密文件路径</param>
        /// <param name="writePath">解密后文件存放路径</param>
        /// <param name="strKey">密钥</param>
        public static bool Decrypt(string readPath, string writePath, string strKey)
        {
            bool IsSuccess = false;
            if (string.IsNullOrEmpty(readPath))
            {
                IsSuccess = false;
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(writePath))
            {
                IsSuccess = false;
                return IsSuccess;
            }
            if (string.IsNullOrEmpty(strKey))
            {
                IsSuccess = false;
                return IsSuccess;
            }
            try
            {
                //读取要解密的文件
                byte[] toEncryptArray = null;
                using (FileStream readFile = new FileStream(readPath, FileMode.Open, FileAccess.Read))
                {
                    toEncryptArray = new byte[readFile.Length];
                    readFile.Read(toEncryptArray, 0, toEncryptArray.Length);
                    readFile.Seek(0, SeekOrigin.Begin);
                    readFile.Flush();
                    readFile.Close();
                    readFile.Dispose();
                }
                //声明RijndaelManaged对象 进行加解密操作
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = UTF8Encoding.UTF8.GetBytes(strKey); //指定加解密使用的密钥
                rDel.Mode = CipherMode.ECB;                    //设置对称算法的运算模式
                rDel.Padding = PaddingMode.ISO10126;           //设置填充模式
                ICryptoTransform cTransform = rDel.CreateDecryptor();   //创建解密对象
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);   //解密
                //将解密后的数据写入本地
                using (FileStream fstream = new FileStream(writePath, FileMode.Append, FileAccess.Write))
                {
                    fstream.Write(resultArray, 0, resultArray.Length);
                    fstream.Flush();
                    fstream.Close();
                    fstream.Dispose();
                }
                rDel.Clear();//释放所有使用的资源
                IsSuccess = true;
                return IsSuccess;
            }
            catch (Exception)
            {
                return false;
            }
        
        }

        前台

 <span style="white-space:pre">	</span>//加密
        [WebMethod]
        public bool FileEn(string filename)
        {
            string key = "597510E19D2D4D7E81604A58AF136B43";
            string path = Server.MapPath("temp\\" + filename + ".epub");
            if (!File.Exists(path))
            {
                return false;
            }
            string TempPath = Server.MapPath("temp\\" + filename + "TEMP.epub");
            return AESEnDe.Encrypt(path, TempPath, key);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        [WebMethod]
        public bool FileDe(string filename)
        {  
            string key = "597510E19D2D4D7E81604A58AF136B43";
            string path = Server.MapPath("temp\\" + filename + ".epub");
            string TempPath = Server.MapPath("temp\\" + filename + "TEMP.epub");
            if (!File.Exists(TempPath))
            {
                return false;
            }
            return AESEnDe.Decrypt(TempPath, path, key);
        }

我想说我自己也是一知半解。

本文标签: 文件RijndaelManagedePub