国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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# 加密解密字符串的使用詳解

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精精国产xxxx在线视频www | 九九热九九| 操批网站 | 性色好看的网站 | 成年人免费看 | 97碰碰碰免费公开在线视频 | 日本三级韩国三级三级a级中文 | 久久精品无码一区二区三区 | 四季久久免费一区二区三区四区 | 久久久91视频 | 日韩精品1区 | 中文字幕av亚洲精品一部二部 | 转生成为史莱姆这档事第四季在线观看 | 亚洲天堂av网 | 欧美一区二区三 | 一区免费视频 | 香蕉国产| 日韩亚洲一区二区 | 欧美精品久久久 | а天堂中文最新一区二区三区 | 精品在线一区二区 | 好看的国产精彩视频 | 午夜视频| 一级看片| 播放毛片| 久久天天躁狠狠躁夜夜躁2014 | 色播av | 亚洲高清视频在线 | av高清在线看 | 欧美视频区 | 亚洲成人中文字幕 | 先锋av资源| 国产精品一区二区三区在线播放 | 欧美一区二区三区在线视频 | 激情综合在线 | 欧美三区 | 国产精品日韩在线观看 | 国产精品入口久久 | 久久久国产精品入口麻豆 | 欧美日韩激情一区 | 99亚洲伊人久久精品影院 |