.NET Framework 中的加密解密
来源:互联网 发布:java web listview 编辑:程序博客网 时间:2024/05/17 05:05
.NET Framework 提供许多标准加密算法的实现。 相关类可在命名空间:System.Security.Cryptography
中找到。详细请参考MSDN。要使用对称加密解密,需要一个RijndaelManaged类的实例。
写了一个SHA加密解密的模块,以下是事例代码:
加密解密数据#region 加密解密数据
//创建RijndaelManaged 实例
private static RijndaelManaged _createRijndaelManaged(string password)
...{
RijndaelManaged rjdM = new RijndaelManaged();
PasswordDeriveBytes deriver = new PasswordDeriveBytes(UnicodeEncoding.UTF8.GetBytes(password), null);
byte[] ivZeros = new byte[8];
byte[] pbeKey = deriver.CryptDeriveKey("TripleDES", "SHA1", 192, ivZeros);
rjdM.Key = pbeKey;
rjdM.IV = new byte[] ...{ 159, 178, 198, 14, 163, 152, 193, 44, 70, 121, 203, 225, 141, 65, 131, 86 };
return rjdM;
}
//加密数据
public static byte[] Encrypt(string password, byte[] toEncrypt)
...{
RijndaelManaged rjdM = _createRijndaelManaged(password);
ICryptoTransform encryptor = rjdM.CreateEncryptor(rjdM.Key, rjdM.IV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
//解密数据
public static byte[] Decrypt(string password, byte[] encrypted)
...{
RijndaelManaged rjdM = _createRijndaelManaged(password);
ICryptoTransform decryptor = rjdM.CreateDecryptor(rjdM.Key, rjdM.IV);
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[encrypted.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return fromEncrypt;
}
#endregion
//创建RijndaelManaged 实例
private static RijndaelManaged _createRijndaelManaged(string password)
...{
RijndaelManaged rjdM = new RijndaelManaged();
PasswordDeriveBytes deriver = new PasswordDeriveBytes(UnicodeEncoding.UTF8.GetBytes(password), null);
byte[] ivZeros = new byte[8];
byte[] pbeKey = deriver.CryptDeriveKey("TripleDES", "SHA1", 192, ivZeros);
rjdM.Key = pbeKey;
rjdM.IV = new byte[] ...{ 159, 178, 198, 14, 163, 152, 193, 44, 70, 121, 203, 225, 141, 65, 131, 86 };
return rjdM;
}
//加密数据
public static byte[] Encrypt(string password, byte[] toEncrypt)
...{
RijndaelManaged rjdM = _createRijndaelManaged(password);
ICryptoTransform encryptor = rjdM.CreateEncryptor(rjdM.Key, rjdM.IV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
//解密数据
public static byte[] Decrypt(string password, byte[] encrypted)
...{
RijndaelManaged rjdM = _createRijndaelManaged(password);
ICryptoTransform decryptor = rjdM.CreateDecryptor(rjdM.Key, rjdM.IV);
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
byte[] fromEncrypt = new byte[encrypted.Length];
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
return fromEncrypt;
}
#endregion
- .NET Framework 中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- Net中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- .Net中的加密解密
- .net中的加密解密方法
- .NET中的DES加密解密
- asp.net中的加密解密
- 解密.net framework中的MSCOREE.DLL
- .net 中的几种加密解密方法
- .NET中的加密与解密技术
- .net 中的几种加密解密方法
- php 怎样去掉一个string 后面的一个或多个字符(how to remove the last n chars from a string)?
- JSON劫持以及ASP.NET
- 养成良好的绘制 UML 序列图的习惯
- Jode 反编译专家
- Oracle 9.2下的“System.Exception: System.Data.OracleClient requires Oracle client software version 8.1.7 or greater
- .NET Framework 中的加密解密
- 使用 UML 为 Web 应用程序构架建模
- Struts+Spring+Hibernate组装web应用
- 文件和数据库存储数据的速度比较
- XMLHttpRequest对象
- 附属于商业企业的社区应该如何发展?
- 我们如何拿到投资?(转载于李善友的BLOG)
- 基于UML和ASP.NET实现三层B/S结构系统开发
- .net 2.0 访问Oracle --与Sql Server的差异,注意事项,常见异常