后台

 
public class CryptoHelper
    {
        // 对称加密算法提供器
        private ICryptoTransform encryptor;//加密器对象
        private ICryptoTransform decryptor;//解密器对象
        private const int BufferSize = 1024;
        public CryptoHelper(String algorithmName, String key)
        {
            SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName);
            provider.Key = Encoding.UTF8.GetBytes(key);//加密密钥
            provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//偏移量
            encryptor = provider.CreateEncryptor();
            decryptor = provider.CreateDecryptor();
        }
        /// <summary>
        ///  TripleDES 算法 给密钥加密
        /// </summary>
        /// <param name="key"></param>
        public CryptoHelper(string key) : this("TripleDES", key) { }
        /// <summary>
        /// 加密算法
        /// </summary>
        /// <param name="clearText"></param>
        /// <returns></returns>
        public String Encrypt(String clearText)
        {
            //创建明文流
            byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
            MemoryStream clearStream = new MemoryStream(clearBuffer);
            MemoryStream encryptedStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);
            //将明文流写入到buffer中
            //将buffer中的数据写入到cryptoStream 中
            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];
            //不能用循环 会执行两次
            //do
            //{
                bytesRead = clearStream.Read(buffer, 0, BufferSize);
                cryptoStream.Write(buffer, 0, BufferSize);
            //} while (bytesRead > 0);
            cryptoStream.FlushFinalBlock();
            //获取加密后的文本
            buffer = encryptedStream.ToArray();
            string encryptedText = Convert.ToBase64String(buffer);
            return encryptedText;
        }
        /// <summary>
        /// 解密算法
        /// </summary>
        /// <param name="encryptedText"></param>
        /// <returns></returns>
        public String Decrypt(String encryptedText)
        {
            byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
            Stream encryptedStream = new MemoryStream(encryptedBuffer);
            MemoryStream clearStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);
            int bytesRead = 0;
            byte[] buffer = new byte[BufferSize];
            //不能用循环 会执行两次
            //do
            //{
                bytesRead = cryptoStream.Read(buffer, 0, BufferSize);
                clearStream.Write(buffer, 0, bytesRead);
            //} while (bytesRead > 0);
            buffer = clearStream.GetBuffer();
            String clearText = Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);
            return clearText;
        }
        public static String Encrypt(string clearText, string Key)
        {
            CryptoHelper helper = new CryptoHelper(Key);
            return helper.Encrypt(clearText);
        }
        public static String Decrypt(String encryptedText, String Key)
        {
            CryptoHelper helper = new CryptoHelper(Key);
            return helper.Decrypt(encryptedText);
        }

}

 
前台
 String key = "abcdefghijklmno2";//17位数
            String clearText = "欢迎光临!www.ritztours.com";
            CryptoHelper helper = new CryptoHelper(key);
            String encryptedText = helper.Encrypt(clearText);

String DecryptText = helper.Decrypt(encryptedText);

.net 对称加密的更多相关文章

  1. 个人理解c#对称加密 非对称加密 散列算法的应用场景

    c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...

  2. .NET中的DES对称加密

    DES是一种对称加密(Data Encryption Standard)算法,于1977年得到美国政府的正式许可,是一种用56位密钥来加密64位数据的方法.一般密码长度为8个字节,其中56位加密密钥, ...

  3. C#不对称加密

    对称加密的缺点是双方使用相同的密钥和IV进行加密.解密.由于接收方必须知道密钥和IV才能解密数据,因此发送方需要先将密钥和IV传递给接收方.这就 有一个问题,如果攻击者截获了密钥和IV,也就等于知道了 ...

  4. AES —— JAVA中对称加密和解密

    package demo.security; import java.io.IOException; import java.io.UnsupportedEncodingException; impo ...

  5. 介绍对称加密的另一个算法——PBE

    除了DES,我们还知道有DESede(TripleDES,就是3DES).AES.Blowfish.RC2.RC4(ARCFOUR)等多种对称加密方式,其实现方式大同小异,这里介绍对称加密的另一个算法 ...

  6. iOS CommonCrypto 对称加密 AES ecb,cbc

    CommonCrypto 为苹果提供的系统加密接口,支持iOS 和 mac 开发: 不仅限于AES加密,提供的接口还支持其他DES,3DES,RC4,BLOWFISH等算法, 本文章主要讨论AES在i ...

  7. openssl evp 对称加密(AES_ecb,ccb)

    openssl evp 对称加密(AES_ecb,ccb) evp.h 封装了openssl常用密码学工具,以下主要说对称加密的接口 1. 如下使用 aes_256_ecb 模式的加密解密测试代码 u ...

  8. AES,RSA对称加密和非对称加密

    1.关于RSA加密机制:是非对称加密方式,两个钥,公钥和私钥,公钥用于加密数据,可以分享给其他用户,私钥可以用于解密用公钥加密的数据,关于安全问题是公钥泄露不会影响安全问题,公钥与私钥是一一对应的关系 ...

  9. TEA,XXTEA介绍,对称加密

    总结:在使用加密的时候,我们可以加入随机数,这样相同的明文,每次加密后得到不同的密文,同时可以在密文中加入密文有效期,控制密文的有效时间长度. 针对有的功能扩展使用,很好的思想. TEA对 64 位数 ...

  10. Java和.NET使用DES对称加密的区别

    Java和.NET的系统类库里都有封装DES对称加密的实现方式,但是对外暴露的接口却各不相同,甚至有时会让自己难以解决其中的问题,比如Java加密后的结果在.NET中解密不出来等,由于最近项目有跨Ja ...

随机推荐

  1. (转)Bootstrap 之 Metronic 模板的学习之路 - (1)总览

    https://segmentfault.com/a/1190000006673582#articleHeader0 写在前面 bootstrap 的模板非常多,Envato 上有着各种各样的免费及付 ...

  2. google浏览器 打印A4 最大宽度和高度px

    width: 1563px;(max) + = 分页了 + = 分页了 + = 没有分页 / ViewBag.results[].Count)); <td width="15%&quo ...

  3. MAMP PRO php的session保存在哪里

    session的概念就不介绍了,最近接触php,很好奇session会保存在哪里. mac上用了MAMP PRO集成环境,作为服务器. 查了网上,说session的保存路径在php.ini中声明,于是 ...

  4. X509 颁发者和使用者 详解

    CN=公用名称C=国家ST=省份L =城市或者区域O=组织名称OU=组织单位名称

  5. 微信小程序 PDF下载打印

    在开发微信小程序时,需要打印生成的PDF,实现思路是:后端生成相应的PDF,微信小程序下载并打开. 但是微信小程序并不可以打印,所以需要借助其他APP比如:WPS,但是发现微信小程序down的PDF在 ...

  6. [系统资源攻略]IO第一篇-磁盘IO,内核IO概念

    几个基本的概念 在研究磁盘性能之前我们必须先了解磁盘的结构,以及工作原理.不过在这里就不再重复说明了,关系硬盘结构和工作原理的信息可以参考维基百科上面的相关词条--Hard disk drive(英文 ...

  7. php实现网站访客数量统计的方法(简单实现,不能防刷新)

    方法一: <?php function Counter()//定义函数 { $five = "00000";//声明变量,$five,$four等变量表示零的个数,放在数字前 ...

  8. 1 java开发工具IDEA的使用

    IntelliJ IDEA 2017.1汉化破解版安装图文教程(附汉化补丁) 注册码:http://idea.lanyus.com/  点击在线生成 IntelliJ IDEA 2017.1正式版发布 ...

  9. 阿里云 全部端口port

  10. 邓_ PHP·笔记(函数总结)

    PHP 指 PHP:超文本预处理器(译者注:PHP: Hypertext Preprocessor,递归命名) PHP 是一种服务器端的脚本语言,类似 ASP PHP 脚本在服务器上执行 PHP 支持 ...