Asp.net,C# 加密解密字串

巴扎黑
發布: 2016-12-20 16:01:41
原創
1472 人瀏覽過

先在web.config | app.config 檔案下增加以下程式碼: 
     
pCLcm/ccx "/> 
       
   
 
 
    ? 

Key:加密演算法的金鑰。 



接著新建類CryptoHelper,作為加密幫助類別。 

首先要從設定檔中得到IV 和Key。所以基本碼如下: 
public class CryptoHelper 
        { 
           vate readonly string IV = string.Empty; 
            //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
            private readonly string Key = string.Empty; 

            ///

 
            ///構造函數 
            ///
 
            public CryptoHelper() 
            { 
                IV = ConfigurationManager.AppSettings["IV"] ; 
                Key

注意加入System.Configuration.dll程式集參考。 



在獲得了IV 和Key 之後,需要取得提供加密服務的Service 類別。 

在這裡,使用的是System.Security.Cryptography; 命名空間下的TripleDESCryptoServiceProvider類別。

取得TripleDESCryptoServiceProvider 的方法如下: 
///  
        /// 取得加密服務類別 
  turns> 
        private TripleDESCryptoServiceProvider GetCryptoProvider() 
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider(); 

            provider.Key = Convert.FromBase64String(Key); 

            return provider; 方法 

CreateEncryptor:建立對稱加密器物件ICryptoTransform. 

CreateDecryptor:建立對稱解密者物件ICryptoTransform 

加密器物件和解密者物件可以被CryptoStream物件使用。來對流進行加密和解密。 

cryptoStream 的建構子如下: 
public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode); 

使用transform 物件轉換stream 。



完整的加密字串代碼如下: 
///  
        /// 加密後的字串 
  putValue">輸入值. 
        ///  
public string GetEncryptedValue(string inputValue) 
        { 
              // 建立記憶體流以保存加密後的串流 
            MemoryStream mStream = new MemoryStream(); 

   CryptoStream cStream = new CryptoStream(mStream, 
            provider.CreateEncryptor(), CryptoStreamMode.Write);  
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue); 

      
            cStream.Write(toEncrypt, 0, toEncrypt.Length); 
           // 在呼叫轉換流的FlushFinalBlock方法後,內部就會進行轉換了,此時mStream就是加密後的流了。 
            byte[] ret = mStream.ToArray(); 

         
            mStream.Close(); 

            //以64的位元組進行64編碼。
            return Convert.ToBase64String(ret); 
        } 

   /// 取得解密後的值 
        ///
 
        /// 加密後的字串. 
        ///  
                 TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

            byte[] inputEquivalent = Convert.FromBase64String(inputValue); 

        ); 

           MemoryStream msDecrypt = new MemoryStream(); 

            // 建立轉換流。
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
                                                           CryptoStreamMode.Write); ength); 

            csDecrypt.FlushFinalBlock(); 
          ); 

            //取得字串。
            return new UTF8Encoding().GetString(msDecrypt.ToArray()); 
      ; 
使用System.Collections.Generic; 
使用System.Linq; 
使用System.Text; 
使用System.Security.Cryptography; 
使用System.IO; 
使用系統設定; 

命名空間WindowsFormsApplication1 
{    //私有唯讀字串IV = "SuFjcEmp/TE ="; 
        私有唯讀字串IV = string.Empty; 
        //私有唯讀字串Key = "KIPSToILGp6fl+3gXJvMsNKeyIKJSBTIc​​id Empty; 

        public CryptoHelper() 

            IV = ConfigurationManager.AppSettings["IV"]; 
           
        /// ; 
        /// 取得加密後的字串 
        ///

        /// 輸入值。  
        ///  
        public string GetEncrypted   TripleDESCryptoServiceProvider provider = this.GetCryptoProvider(); 

            // 建立記憶體流來儲存加密的流  mStream = new MemoryStream(); 

            // 加密轉換流 
            provider.CreateEncryptor(), CryptoStreamMode.Write); 
            // 使用UTF8編碼取得輸入字串的位元組。 
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue); 

      
            cStream.Write(toEncrypt, 0, toEncrypt.Length); 
           // 在呼叫轉換流的FlushFinalBlock方法後,內部就會進行轉換了,此時mStream就是加密後的流了。 
            byte[] ret = mStream.ToArray(); 

            // 關閉流。 
            cStream.Close(); 
            mStream    
            return Convert.ToBase64String(ret); 
        } 
        /// ; 
        ///  
私人TripleDESCryptoServiceProvider GetCryptoProvider() 
        { 
              provider.IV = Convert.FromBase64String(IV); 
           provider.Key = Convert.FromBase64String(Key); 🠎 } 

        /// ; 
        /// 取得解密後的值 
     的字串。  
        ///  
        public string GetDecrypted   TripleDESCryptoServiceProvider provider = this.GetCryptoProvider(); 
            byte[] inputEquivalent = Convert.FromBase64String(inputValue); // 建立記憶體流保存解密後的資料 
            MemoryStream msDecrypt = new MemoryStream(); 

    
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, 
         toStreamMode.Write); 

            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length); 🠎)D  
            csDecrypt.Close( ); 

            //取得字串。 
            return new UTF8Encoding().GetString(msDecrypt.ToArray()); 
     

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!