后台

 
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);

最新文章

  1. Linux根文件系统分析之init和busybox
  2. CentOS 6.3 中安装VirtualBOX增强工具失败:Building the main Guest Additions module[FAILED]
  3. EJB之Timer
  4. (转)JS产生随机数的几个用法!
  5. CSS实现垂直居中
  6. 【C#】C# 实现发送手机短信
  7. Eclipse groovy in action
  8. 将Oracle JDBC驱动库安装到本地仓库
  9. NCache实现Oracle数据与分布式缓存数据同步的3个步骤
  10. [HNOI2014]道路堵塞
  11. Text-CNN-文本分类-keras
  12. 使用svn创建分支!
  13. 剑指offer例题分享--7
  14. ie.360,qq浏览器这种ie内核浏览器默认阻止弹窗
  15. python3用BeautifulSoup用re.compile来匹配需要抓取的href地址
  16. Jmeter响应中中文乱码怎么解决
  17. 【 js 基础 】【读书笔记】Javascript “继承”
  18. office每次打开都要重新配置
  19. Checked Exception &amp; Unchecked Exception
  20. 三、Django之请求与响应-Part 1

热门文章

  1. (转)基于MVC4+EasyUI的Web开发框架经验总结(9)--在Datagrid里面实现外键字段的转义操作
  2. logger日志
  3. &lt;meta http-equiv=&quot;refresh&quot; content=&quot;3&quot;&gt; 什么意思?
  4. day005 流程控制 (if / for / while)
  5. BPM结束任务
  6. PAT_A1128#N Queens Puzzle
  7. 多叉树结构的数据,parent表示法转成children表示法
  8. win10、win7 使用centos配置网络,可以让Xshell进行连接,虚拟机进行上网;
  9. docker安装部署
  10. SBC37x交叉编译平台QT+OPENCV