asp.net

位置:IT落伍者 >> asp.net >> 浏览文章

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


发布日期:2021年05月03日
 
Asp.net,C# 加密解密字符串

首先在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())

}

}

}

使用例子

               

上一篇:ASP.NET递归将分类绑定到TreeView

下一篇:浅谈ASP.NET的内部机制(二)