当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net,C# 加密解密字符串的使用详解

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

陈翔被粉丝强吻,鱼的月牙肉在哪,54岁女厅官落马

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


使用例子:

image

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网