溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Asp.net中對字符串進行加密解密操作

發布時間:2021-03-01 16:27:51 來源:億速云 閱讀:184 作者:戴恩恩 欄目:開發技術

本文章向大家介紹怎么在Asp.net中對字符串進行加密解密操作,主要包括怎么在Asp.net中對字符串進行加密解密操作的使用實例、應用技巧、基本知識點總結和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。

ASP.NET 是什么

ASP.NET 是開源,跨平臺,高性能,輕量級的 Web 應用構建框架,常用于通過 HTML、CSS、JavaScript 以及服務器腳本來構建網頁和網站。

首先在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中對字符串進行加密解密操作的文章就介紹到這了,更多相關的內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女