使用RijndaelManaged對epub文件進行加密

最近工作中遇到的問題,需要對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);
        }

我想說我自己也是一知半解。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章