濮阳杆衣贸易有限公司

主頁 > 知識庫 > Asp.net,C# 加密解密字符串的使用詳解

Asp.net,C# 加密解密字符串的使用詳解

熱門標(biāo)簽:凱立德導(dǎo)航官網(wǎng)地圖標(biāo)注 萊蕪?fù)夂綦婁N機器人價格 五常地圖標(biāo)注 智能電話營銷外呼系統(tǒng) 鄭州400電話辦理 聯(lián)通 戶外地圖標(biāo)注軟件手機哪個好用 地圖標(biāo)注和認(rèn)領(lǐng) 長春呼叫中心外呼系統(tǒng)哪家好 電銷語音自動機器人

首先在web.config | app.config 文件下增加如下代碼:

復(fù)制代碼 代碼如下:

?xml version="1.0"?>
  configuration>
    appSettings>
      add key="IV" value="SuFjcEmp/TE="/>
      add key="Key" value="KIPSToILGp6fl+3gXJvMsN4IajizYBBT"/>
    /appSettings>
  /configuration>

IV:加密算法的初始向量。

Key:加密算法的密鑰。

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

首先要從配置文件中得到IV 和Key。所以基本代碼如下

復(fù)制代碼 代碼如下:

public class CryptoHelper
        {
            //private readonly string IV = "SuFjcEmp/TE=";
            private readonly string IV = string.Empty;
            //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
            private readonly string Key = string.Empty;

            /// summary>
            ///構(gòu)造函數(shù)
            /// /summary>
            public CryptoHelper()
            {
                IV = ConfigurationManager.AppSettings["IV"];
                Key = ConfigurationManager.AppSettings["Key"];
            }
        }


注意添加System.Configuration.dll程序集引用。
在獲得了IV 和Key 之后,需要獲取提供加密服務(wù)的Service 類。

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

獲取TripleDESCryptoServiceProvider 的方法如下:

復(fù)制代碼 代碼如下:

/// summary>
        /// 獲取加密服務(wù)類
        /// /summary>
        /// returns>/returns>
        private TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();

            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(Key);

            return provider;
        }


TripleDESCryptoServiceProvider 兩個有用的方法

CreateEncryptor:創(chuàng)建對稱加密器對象ICryptoTransform.

CreateDecryptor:創(chuàng)建對稱解密器對象ICryptoTransform

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

cryptoStream 的構(gòu)造函數(shù)如下:

public CryptoStream(Stream stream, ICryptoTransform transform, CryptoStreamMode mode);

使用transform 對象對stream 進行轉(zhuǎn)換。

完整的加密字符串代碼如下:

復(fù)制代碼 代碼如下:

/// summary>
        /// 獲取加密后的字符串
        /// /summary>
        /// param name="inputValue">輸入值./param>
        /// returns>/returns>
        public string GetEncryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

            // 創(chuàng)建內(nèi)存流來保存加密后的流
            MemoryStream mStream = new MemoryStream();

            // 創(chuàng)建加密轉(zhuǎn)換流
            CryptoStream cStream = new CryptoStream(mStream,
            provider.CreateEncryptor(), CryptoStreamMode.Write);

            // 使用UTF8編碼獲取輸入字符串的字節(jié)。
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);

            // 將字節(jié)寫到轉(zhuǎn)換流里面去。
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();

            // 在調(diào)用轉(zhuǎn)換流的FlushFinalBlock方法后,內(nèi)部就會進行轉(zhuǎn)換了,此時mStream就是加密后的流了。
            byte[] ret = mStream.ToArray();

            // Close the streams.
            cStream.Close();
            mStream.Close();

            //將加密后的字節(jié)進行64編碼。
            return Convert.ToBase64String(ret);
        }


解密方法也類似:
復(fù)制代碼 代碼如下:

/// summary>
        /// 獲取解密后的值
        /// /summary>
        /// param name="inputValue">經(jīng)過加密后的字符串./param>
        /// returns>/returns>
        public string GetDecryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

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

            // 創(chuàng)建內(nèi)存流保存解密后的數(shù)據(jù)
            MemoryStream msDecrypt = new MemoryStream();

            // 創(chuàng)建轉(zhuǎn)換流。
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                                                        provider.CreateDecryptor(),
                                                        CryptoStreamMode.Write);

            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);

            csDecrypt.FlushFinalBlock();
            csDecrypt.Close();

            //獲取字符串。
            return new UTF8Encoding().GetString(msDecrypt.ToArray());
        }


完整的CryptoHelper代碼如下:
復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public class CryptoHelper
    {
        //private readonly string IV = "SuFjcEmp/TE=";
        private readonly string IV = string.Empty;
        //private readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";
        private readonly string Key = string.Empty;

        public CryptoHelper()
        {
            IV = ConfigurationManager.AppSettings["IV"];
            Key = ConfigurationManager.AppSettings["Key"];
        }

        /// summary>
        /// 獲取加密后的字符串
        /// /summary>
        /// param name="inputValue">輸入值./param>
        /// returns>/returns>
        public string GetEncryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();

            // 創(chuàng)建內(nèi)存流來保存加密后的流
            MemoryStream mStream = new MemoryStream();

            // 創(chuàng)建加密轉(zhuǎn)換流
            CryptoStream cStream = new CryptoStream(mStream,

            provider.CreateEncryptor(), CryptoStreamMode.Write);
            // 使用UTF8編碼獲取輸入字符串的字節(jié)。
            byte[] toEncrypt = new UTF8Encoding().GetBytes(inputValue);

            // 將字節(jié)寫到轉(zhuǎn)換流里面去。
            cStream.Write(toEncrypt, 0, toEncrypt.Length);
            cStream.FlushFinalBlock();

            // 在調(diào)用轉(zhuǎn)換流的FlushFinalBlock方法后,內(nèi)部就會進行轉(zhuǎn)換了,此時mStream就是加密后的流了。
            byte[] ret = mStream.ToArray();

            // Close the streams.
            cStream.Close();
            mStream.Close();

            //將加密后的字節(jié)進行64編碼。
            return Convert.ToBase64String(ret);
        }

        /// summary>
        /// 獲取加密服務(wù)類
        /// /summary>
        /// returns>/returns>
        private TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();

            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(Key);

            return provider;

        }

        /// summary>
        /// 獲取解密后的值
        /// /summary>
        /// param name="inputValue">經(jīng)過加密后的字符串./param>
        /// returns>/returns>
        public string GetDecryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
            byte[] inputEquivalent = Convert.FromBase64String(inputValue);

            // 創(chuàng)建內(nèi)存流保存解密后的數(shù)據(jù)
            MemoryStream msDecrypt = new MemoryStream();

            // 創(chuàng)建轉(zhuǎn)換流。
            CryptoStream csDecrypt = new CryptoStream(msDecrypt,
            provider.CreateDecryptor(),
            CryptoStreamMode.Write);

            csDecrypt.Write(inputEquivalent, 0, inputEquivalent.Length);
            csDecrypt.FlushFinalBlock();

            csDecrypt.Close();

            //獲取字符串。
            return new UTF8Encoding().GetString(msDecrypt.ToArray());
        }
    }
}


使用例子:

您可能感興趣的文章:
  • asp.net web.config加密解密方法
  • asp.net 字符串加密解密技術(shù)
  • asp.net TripleDES加密、解密算法
  • asp.net的加密解密技巧
  • 基于.net4.0實現(xiàn)IdentityServer4客戶端JWT解密

標(biāo)簽:西寧 湖州 宣城 衢州 岳陽 西藏 紅河 福州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Asp.net,C# 加密解密字符串的使用詳解》,本文關(guān)鍵詞  Asp.net,加密解密,字符串,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Asp.net,C# 加密解密字符串的使用詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于Asp.net,C# 加密解密字符串的使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    太仆寺旗| 南雄市| 巴南区| 七台河市| 宁国市| 济阳县| 三明市| 高密市| 通榆县| 安塞县| 纳雍县| 澄迈县| 潢川县| 合肥市| 葵青区| 安化县| 仪征市| 建始县| 昌宁县| 鞍山市| 蕲春县| 万年县| 平江县| 比如县| 桃园市| 沙湾县| 前郭尔| 娱乐| 唐山市| 顺平县| 彩票| 连江县| 印江| 恩施市| 静海县| 信丰县| 渝中区| 安仁县| 龙门县| 漳平市| 余江县|