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

巴扎黑
发布: 2016-12-20 16:01:41
原创
1633人浏览过

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

度加剪辑
度加剪辑

度加剪辑(原度咔剪辑),百度旗下AI创作工具

度加剪辑 63
查看详情 度加剪辑
相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号