.net数据加密
来源:互联网 发布:篮球单循环赛制算法 编辑:程序博客网 时间:2024/04/30 03:42
using System;
using System.Security.Cryptography;
using System.Text;
namespace LanSheng.Utility
{
/// <summary>
/// DES加密/解密类。
/// </summary>
public class DESEncrypt
{
public DESEncrypt()
{
}
#region ========加密========
/// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
return Encrypt(Text,"KZZHJ");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text,string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
catch { return Text; }
}
#endregion
#region ========解密========
/// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
return Decrypt(Text, "KZZHJ");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text,string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
catch { return Text;}
}
#endregion
}
}
- .net数据加密
- .net数据加密
- .net数据加密方法
- .net对称算法数据加密
- 用ASP.NET加密Cookie数据
- 用ASP.NET加密Cookie数据
- 用ASP.NET加密Cookie数据
- 用ASP.NET加密Cookie数据
- 用ASP.NET加密Cookie数据
- ASP.NET中用MD5对数据加密
- 如何加密ASP.NET配置数据
- asp.net MD5数据加密和解密
- .NET中加密和解密---加密网络数据
- .Net 加密
- .net加密
- .Net中使用散列算法对数据进行加密
- .Net中使用散列算法对数据进行加密
- ASP.NET(C#)常用数据加密和解密方法汇总
- 用于主题检测的临时日志(2859bf27-6cff-4904-a176-01482b27bce8 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- VC中自定义消息的步骤与截获消息的方法
- 安装Wamp后 Apache无法启动的解决方法
- 意外之喜,获一小奖
- MySql数据库连接字符串,webconfig配置
- .net数据加密
- 得到Team中的人员
- webconfig中配置各种数据库的连接字符串
- My first csdn blog.
- .net开源论坛
- Sql Server实用操作小技巧总结(转)
- 容器的范围
- 权限1
- PHY