c# des 加密解密
2009-05-22 14:50
246 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
namespace Test
{
/// <summary>
/// DES加密/解密类
/// </summary>
class ClassDES
{
public ClassDES()
{
}
/// <summary>
/// 密钥
/// </summary>
/// <param name="text">密钥文本</param>
/// <param name="type">类型(sha1或md5)</param>
/// <returns></returns>
public static string SetKey(string text,string type)
{
if (type == "md5")
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "md5").ToLower();
}
else
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "sha1").ToLower();
}
}
//========加密========#region ========加密========
/// <summary>
/// 加密数据
/// </summary>
/// <param name="text">要加密的文本</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string Encrypt(string text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(text);
des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(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();
}
//========解密========#region ========解密========
/// <summary>
/// 解密数据
/// </summary>
/// <param name="text">解密文本</param>
/// <param name="sKey">解密密钥</param>
/// <returns></returns>
public static string Decrypt(string text,string sKey)
{
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(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(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());
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Web.Security;
namespace Test
{
/// <summary>
/// DES加密/解密类
/// </summary>
class ClassDES
{
public ClassDES()
{
}
/// <summary>
/// 密钥
/// </summary>
/// <param name="text">密钥文本</param>
/// <param name="type">类型(sha1或md5)</param>
/// <returns></returns>
public static string SetKey(string text,string type)
{
if (type == "md5")
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "md5").ToLower();
}
else
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(text, "sha1").ToLower();
}
}
//========加密========#region ========加密========
/// <summary>
/// 加密数据
/// </summary>
/// <param name="text">要加密的文本</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string Encrypt(string text,string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray=Encoding.Default.GetBytes(text);
des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(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();
}
//========解密========#region ========解密========
/// <summary>
/// 解密数据
/// </summary>
/// <param name="text">解密文本</param>
/// <param name="sKey">解密密钥</param>
/// <returns></returns>
public static string Decrypt(string text,string sKey)
{
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(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(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());
}
}
}
相关文章推荐
- C#中使用DES和AES加密解密
- [网络收集]C# 各种加密,解密算法【MD5,DES,线性,卷帘】
- C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- [转]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- DES 加密/解密-C#
- 用 Java 解密 C# 加密的数据(DES)
- C# 加密解密(DES,3DES,MD5,Base64) 类
- 黄聪:C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- C#中使用DES和AES加密解密
- C# DES 加密解密
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
- [读书笔记]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- C#加密解密(DES,AES,Base64,md5,SHA256,RSA,RC4)
- C#字符串的加密与解密(DES和TripleDES算法的加密解密过程)
- C#中使用DES和AES加密解密
- c# 调用PB中的b64_size加密解密 DES
- C# 的 DES 加密解密
- C# 與JAVA 共用的DES 加密解密方法
- C# 加密解密(DES,3DES,MD5,Base64) 类
- c# DES 加密解密