熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

  首先在nfig | nfig 文件下增加如下代碼
    <?xml version=?>
    <configuration>
    <appSettings>
    <add key=IV value=SuFjcEmp/TE=/>
    <add key=Key value=KIPSToILGpfl+gXJvMsNIajizYBBT/>
    </appSettings>
    </configuration>
    IV:加密算法的初始向量
    Key:加密算法的密鑰
    接著新建類CryptoHelper作為加密幫助類
    首先要從配置文件中得到IV 和Key所以基本代碼如下
    public class CryptoHelper
    {
    //private readonly string IV = SuFjcEmp/TE=;
    private readonly string IV = stringEmpty;
    //private readonly string Key = KIPSToILGpfl+gXJvMsNIajizYBBT;
    private readonly string Key = stringEmpty;
    /// <summary>
    ///構造函數
    /// </summary>
    public CryptoHelper()
    {
    IV = ConfigurationManagerAppSettings[IV];
    Key = ConfigurationManagerAppSettings[Key];
    }
    }
    注意添加SystemConfigurationdll程序集引用
    在獲得了IV 和Key 之後需要獲取提供加密服務的Service 類
    在這裡使用的是SystemSecurityCryptography; 命名空間下的TripleDESCryptoServiceProvider類
    獲取TripleDESCryptoServiceProvider 的方法如下
    /// <summary>
    /// 獲取加密服務類
    /// </summary>
    /// <returns></returns>
    private TripleDESCryptoServiceProvider GetCryptoProvider()
    {
    TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider()
    providerIV = ConvertFromBaseString(IV)
    providerKey = ConvertFromBaseString(Key)
    return provider;
    }
    TripleDESCryptoServiceProvider 兩個有用的方法
    CreateEncryptor:創建對稱加密器對象ICryptoTransform
    CreateDecryptor:創建對稱解密器對象ICryptoTransform
    加密器對象和解密器對象可以被CryptoStream對象使用來對流進行加密和解密
    cryptoStream 的構造函數如下
    public CryptoStream(Stream stream ICryptoTransform transform CryptoStreamMode mode)使用transform 對象對stream 進行轉換
    完整的加密字符串代碼如下
    /// <summary>
    /// 獲取加密後的字符串
    /// </summary>
    /// <param name=inputValue>輸入值</param>
    /// <returns></returns>
    public string GetEncryptedValue(string inputValue)
    {
    TripleDESCryptoServiceProvider provider = thisGetCryptoProvider()
    // 創建內存流來保存加密後的流
    MemoryStream mStream = new MemoryStream()
    // 創建加密轉換流
    CryptoStream cStream = new CryptoStream(mStream
    providerCreateEncryptor() CryptoStreamModeWrite)
    // 使用UTF編碼獲取輸入字符串的字節
    byte[] toEncrypt = new UTFEncoding()GetBytes(inputValue)
    // 將字節寫到轉換流裡面去
    cStreamWrite(toEncrypt toEncryptLength)
    cStreamFlushFinalBlock()
    // 在調用轉換流的FlushFinalBlock方法後內部就會進行轉換了此時mStream就是加密後的流了
    byte[] ret = mStreamToArray()
    // Close the streams
    cStreamClose()
    mStreamClose()
    //將加密後的字節進行編碼
    return ConvertToBaseString(ret)
    }
    解密方法也類似
    /// <summary>
    /// 獲取解密後的值
    /// </summary>
    /// <param name=inputValue>經過加密後的字符串</param>
    /// <returns></returns>
    public string GetDecryptedValue(string inputValue)
    {
    TripleDESCryptoServiceProvider provider = thisGetCryptoProvider()
    byte[] inputEquivalent = ConvertFromBaseString(inputValue)
    // 創建內存流保存解密後的數據
    MemoryStream msDecrypt = new MemoryStream()
    // 創建轉換流
    CryptoStream csDecrypt = new CryptoStream(msDecrypt
    providerCreateDecryptor()
    CryptoStreamModeWrite)
    csDecryptWrite(inputEquivalent inputEquivalentLength)
    csDecryptFlushFinalBlock()
    csDecryptClose()
    //獲取字符串
    return new UTFEncoding()GetString(msDecryptToArray())
    }


    完整的CryptoHelper代碼如下
    using System;
    using SystemCollectionsGeneric;
    using SystemLinq;
    using SystemText;
    using SystemSecurityCryptography;
    using SystemIO;
    using SystemConfiguration;
    namespace WindowsFormsApplication
    {
    public class CryptoHelper
    {
    //private readonly string IV = SuFjcEmp/TE=;
    private readonly string IV = stringEmpty;
    //private readonly string Key = KIPSToILGpfl+gXJvMsNIajizYBBT;
    private readonly string Key = stringEmpty;
    public CryptoHelper()
    {
    IV = ConfigurationManagerAppSettings[IV];
    Key = ConfigurationManagerAppSettings[Key];
    }
    /// <summary>
    /// 獲取加密後的字符串
    /// </summary>
    /// <param name=inputValue>輸入值</param>
    /// <returns></returns>
    public string GetEncryptedValue(string inputValue)
    {
    TripleDESCryptoServiceProvider provider = thisGetCryptoProvider()
    // 創建內存流來保存加密後的流
    MemoryStream mStream = new MemoryStream()
    // 創建加密轉換流
    CryptoStream cStream = new CryptoStream(mStream
    providerCreateEncryptor() CryptoStreamModeWrite)
    // 使用UTF編碼獲取輸入字符串的字節
    byte[] toEncrypt = new UTFEncoding()GetBytes(inputValue)
    // 將字節寫到轉換流裡面去
    cStreamWrite(toEncrypt toEncryptLength)
    cStreamFlushFinalBlock()
    // 在調用轉換流的FlushFinalBlock方法後內部就會進行轉換了此時mStream就是加密後的流了
    byte[] ret = mStreamToArray()
    // Close the streams
    cStreamClose()
    mStreamClose()
    //將加密後的字節進行編碼
    return ConvertToBaseString(ret)
    }
    /// <summary>
    /// 獲取加密服務類
    /// </summary>
    /// <returns></returns>
    private TripleDESCryptoServiceProvider GetCryptoProvider()
    {
    TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider()
    providerIV = ConvertFromBaseString(IV)
    providerKey = ConvertFromBaseString(Key)
    return provider;
    }
    /// <summary>
    /// 獲取解密後的值
    /// </summary>
    /// <param name=inputValue>經過加密後的字符串</param>
    /// <returns></returns>
    public string GetDecryptedValue(string inputValue)
    {
    TripleDESCryptoServiceProvider provider = thisGetCryptoProvider()
    byte[] inputEquivalent = ConvertFromBaseString(inputValue)
    // 創建內存流保存解密後的數據
    MemoryStream msDecrypt = new MemoryStream()
    // 創建轉換流
    CryptoStream csDecrypt = new CryptoStream(msDecrypt
    providerCreateDecryptor()
    CryptoStreamModeWrite)
    csDecryptWrite(inputEquivalent inputEquivalentLength)
    csDecryptFlushFinalBlock()
    csDecryptClose()
    //獲取字符串
    return new UTFEncoding()GetString(msDecryptToArray())
    }
    }
    }
    使用例子

  


From:http://tw.wingwit.com/Article/program/net/201311/12266.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.