using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; public class CryptographyHelper
{
private static string sKey = "";
private static string sIV = "ABCDEFGH"; /// <summary>
/// 进行DES加密。
/// </summary>
/// <param name="pToEncrypt">要加密的字符串。</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>以Base64格式返回的加密字符串。</returns>
public static string Encrypt(string pToEncrypt)
{
if (string.IsNullOrEmpty(pToEncrypt))
return string.Empty;
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Convert.ToBase64String(ms.ToArray());
ms.Close();
return str;
}
} /// <summary>
/// 进行DES解密。
/// </summary>
/// <param name="pToDecrypt">要解密的以Base64</param>
/// <param name="sKey">密钥,且必须为8位。</param>
/// <returns>已解密的字符串。</returns>
public static string Decrypt(string pToDecrypt)
{
if (string.IsNullOrEmpty(pToDecrypt))
return string.Empty;
byte[] inputByteArray = Convert.FromBase64String(pToDecrypt.Replace(" ", "+"));
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sIV);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
}

最新文章

  1. Nginx 相关
  2. mysql小结
  3. 阿里云 esc 云服务器无缘无故的一次/usr/bin 目录丢失导致整个服务无法启动 # ./shutdown.sh ./shutdown.sh: line 41: dirname:command not found cannot find /catalina.sh the find /catalina.sh The file is absent or does not have execute
  4. java中string内存的相关知识点
  5. 一种c#深拷贝方式完胜java深拷贝(实现上的对比)
  6. How to make project not set to be build
  7. 重构前的程序:通过rsync命令抓取日志文件
  8. NOI2010 海拔
  9. windows下配置Nginx+Mysql+Php7
  10. MyBatis 注解
  11. NYOJ 105 其余9个
  12. 浅谈PHP的CI框架(一)
  13. 转转hybrid app web静态资源离线系统实践
  14. 计算n的阶乘有多少个尾随零
  15. java - 并发编程易错实例
  16. Redis过期策略(转)
  17. upstream timed out (110: Connection timed out) while reading response header from upstream
  18. BZOJ1922:[SDOI2010]大陆争霸(最短路)
  19. 03: centos中配置使用svn
  20. How to Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x

热门文章

  1. c++学习--面向对象一
  2. 安装Office 2010后出现正在配置
  3. opendove中的odgw所需要的内核模块
  4. C#实现获取文本文件的编码的一个类(区分GB2312和UTF8)-来自转载收集
  5. Windows Azure初体验
  6. js对象常用2中构造方法
  7. mysql设置外网访问
  8. java io 流分类表
  9. cx_Oracle 中文乱码问题解决
  10. ubuntu &#39;Unable to correct problems, you have held broken packages&#39; 错误