1.加密

 public class EncryptHepler {
// 验值
static string saltValue = "XXXX";
// 密码值
static string pwdValue = "XXXX"; /// <summary>
/// 加密
/// </summary>
public static string Encrypt( string input ) {
byte[ ] data = System.Text.UTF8Encoding.UTF8.GetBytes( input );
byte[ ] salt = System.Text.UTF8Encoding.UTF8.GetBytes( saltValue ); // AesManaged - 高级加密标准(AES) 对称算法的管理类
System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
// Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
// 通过 密码 和 salt 派生密钥
System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt ); aes.BlockSize = aes.LegalBlockSizes[].MaxSize;
aes.KeySize = aes.LegalKeySizes[].MaxSize;
aes.Key = rfc.GetBytes( aes.KeySize / );
aes.IV = rfc.GetBytes( aes.BlockSize / ); // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor( );
// 加密后的输出流
System.IO.MemoryStream encryptStream = new System.IO.MemoryStream( );
// 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
( encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write ); // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
encryptor.Write( data, , data.Length );
encryptor.Close( );
// 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );
return encryptedString;
}

2.解密

 /// <summary>
/// 解密
/// </summary>
public static string Decrypt( string input ) {
byte[ ] encryptBytes = Convert.FromBase64String( input );
byte[ ] salt = Encoding.UTF8.GetBytes( saltValue );
System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt ); aes.BlockSize = aes.LegalBlockSizes[].MaxSize;
aes.KeySize = aes.LegalKeySizes[].MaxSize;
aes.Key = rfc.GetBytes( aes.KeySize / );
aes.IV = rfc.GetBytes( aes.BlockSize / ); // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );
// 解密后的输出流
System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( ); // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
// 将一个字节序列写入当前 CryptoStream (完成解密的过程)
decryptor.Write( encryptBytes, , encryptBytes.Length );
decryptor.Close( ); // 将解密后所得到的流转换为字符串
byte[ ] decryptBytes = decryptStream.ToArray( );
string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, , decryptBytes.Length );
return decryptedString;
}
}//class end

最新文章

  1. Javascript高性能编程-提高Dom访问速度
  2. Xamarin Android -创建Splash Screen (一)
  3. 使用go的ssh包快速打造一个本地命令行ssh客户端
  4. css div 清理浮动的2种方法
  5. WPF:MenuItem样式
  6. Share Point 创建 TimerJob
  7. Idea基本设置
  8. Linux上iptables防火墙的基本应用
  9. NOIP2005 篝火晚会
  10. Project Management - 1) Schedule Your Project
  11. ios 解决有关火星坐标的问题
  12. 基于GPUImage的实时美颜滤镜
  13. oracle中job定时调用存储过程的实例
  14. util 学习
  15. debian6 更新python版本到python3.3
  16. Linux 高可用(HA)集群基本概念详解
  17. ArcGIS10.2直连PostgreSQL存在问题
  18. win7电脑关机时间长怎么办
  19. (2环境架设)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  20. mongodb数据库安装

热门文章

  1. hadoop2 作业执行过程之map过程
  2. 图解java8 stream 的几个转换方法
  3. SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]
  4. WPF 之 设置Dialog的父窗体
  5. ava SE ---逻辑运算符
  6. nodejs的mysql模块学习(六)连接池的创建和使用
  7. css文字截取
  8. hdu3336
  9. 设置input(radio,checkbox)和lable对齐的问题
  10. .NET下实现分布式缓存系统Memcached (转自网络)