一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - Asp.net,C# 加密解密字符串的使用詳解

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

2019-11-07 11:51asp.net教程網 ASP.NET教程

本篇文章對Asp.net,C# 加密解密字符串的使用進行了詳細的分析介紹,需要的朋友參考下

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

復制代碼代碼如下:

<?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。所以基本代碼如下

復制代碼代碼如下:


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>
            ///構造函數
            /// </summary>
            public CryptoHelper()
            {
                IV = ConfigurationManager.AppSettings["IV"];
                Key = ConfigurationManager.AppSettings["Key"];
            }
        }


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

 

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

獲取TripleDESCryptoServiceProvider 的方法如下:

復制代碼代碼如下:


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

 

            provider.IV = Convert.FromBase64String(IV);
            provider.Key = Convert.FromBase64String(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 = this.GetCryptoProvider();

 

            // 創建內存流來保存加密后的流
            MemoryStream mStream = new MemoryStream();

            // 創建加密轉換流
            CryptoStream cStream = new CryptoStream(mStream,
            provider.CreateEncryptor(), CryptoStreamMode.Write);

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

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

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

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

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


解密方法也類似:

復制代碼代碼如下:


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

 

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

            // 創建內存流保存解密后的數據
            MemoryStream msDecrypt = new MemoryStream();

            // 創建轉換流。
            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代碼如下:

復制代碼代碼如下:


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();

            // 創建內存流來保存加密后的流
            MemoryStream mStream = new MemoryStream();

            // 創建加密轉換流
            CryptoStream cStream = new CryptoStream(mStream,

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

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

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

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

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

        /// <summary>
        /// 獲取加密服務類
        /// </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">經過加密后的字符串.</param>
        /// <returns></returns>
        public string GetDecryptedValue(string inputValue)
        {
            TripleDESCryptoServiceProvider provider = this.GetCryptoProvider();
            byte[] inputEquivalent = Convert.FromBase64String(inputValue);

            // 創建內存流保存解密后的數據
            MemoryStream msDecrypt = new MemoryStream();

            // 創建轉換流。
            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,C# 加密解密字符串的使用詳解

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女福利视频午夜在线 | 男人把大ji巴放进女人小说 | 国产高清在线不卡 | 92国产福利久久青青草原 | 我不卡影院手机在线观看 | 操熟美女又肥又嫩的骚屁股 | 欧美兽皇另类 | 男人插曲女人下面 | 高清毛片aaaaaaaaa片 | 按摩椅play啊太快了h | 天天舔天天操天天干 | 四虎免费看片 | 欧美精选视频 | 2019中文字幕在线视频 | 国产欧美在线播放 | 青青青国产视频 | 日韩欧美一区黑人vs日本人 | 欧美日韩一区视频 | xxxxx性中国hd | 日本偷偷操 | 免费观看美女被cao视频 | 精品国产乱码久久久久久软件 | 小黄鸭YELLOWDUCK7596 | 亚洲精品短视频 | 色多多视频在线 | 嗯啊视频在线观看 | 国产码一区二区三区 | 日本三级成人中文字幕乱码 | 成人aqq| 美女沟厕撒尿全过程高清图片 | xxxx18日本视频xxxxx| 色cccwww| 国产高清国内精品福利 | 俄罗斯女同和女同xx | 大团圆6全文在线阅读 | 欧美在线视频一区二区 | 亚洲福利一区 | 亚洲国产货青视觉盛宴 | 亚洲欧美一区二区三区在线观看 | 91人人在线| 精品国产欧美一区二区三区成人 |