记录个小小的加密算法
2009-11-07 12:04 苏飞 阅读(538) 评论(4) 编辑 收藏 举报
///
/// 解密
///
/// 要解密的字符串
///
public static string Decode(string str)
{
return MD5PWD.Decode(str, "http://www.cnblogs.com/sufei");
}
阅读全文:http://www.cckan.net/forum.php?mod=viewthread&tid=169
直接看代码有注释
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
/// <summary>
/// MD5PWD 的摘要说明
/// </summary>
public class MD5PWD
{
/// <summary>
/// 开 发 人:苏飞
/// 开发时间:2009年08月
/// 主要功能:加密功能
/// </summary>
public MD5PWD()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 加密
/// </summary>
/// <param name="str">加密串</param>
/// <param name="key">Key</param>
/// <returns></returns>
private static string Encode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}
/// <summary>
/// Des 解密 GB2312
/// </summary>
/// <param name="str">Desc string</param>
/// <param name="key">Key ,必须为8位 </param>
/// <returns></returns>
private static string Decode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
}
/// <summary>
/// 加密
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns></returns>
public static string Encode(string str)
{
return MD5PWD.Encode(str, "http://www.cnblogs.com/sufei");
}
/// <summary>
/// 解密
/// </summary>
/// <param name="str">要解密的字符串</param>
/// <returns></returns>
public static string Decode(string str)
{
return MD5PWD.Decode(str, "http://www.cnblogs.com/sufei");
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;
/// <summary>
/// MD5PWD 的摘要说明
/// </summary>
public class MD5PWD
{
/// <summary>
/// 开 发 人:苏飞
/// 开发时间:2009年08月
/// 主要功能:加密功能
/// </summary>
public MD5PWD()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 加密
/// </summary>
/// <param name="str">加密串</param>
/// <param name="key">Key</param>
/// <returns></returns>
private static string Encode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}
/// <summary>
/// Des 解密 GB2312
/// </summary>
/// <param name="str">Desc string</param>
/// <param name="key">Key ,必须为8位 </param>
/// <returns></returns>
private static string Decode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] buffer = new byte[str.Length / 2];
for (int i = 0; i < (str.Length / 2); i++)
{
int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num2;
}
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
stream.Close();
return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());
}
/// <summary>
/// 加密
/// </summary>
/// <param name="str">要加密的字符串</param>
/// <returns></returns>
public static string Encode(string str)
{
return MD5PWD.Encode(str, "http://www.cnblogs.com/sufei");
}
/// <summary>
/// 解密
/// </summary>
/// <param name="str">要解密的字符串</param>
/// <returns></returns>
public static string Decode(string str)
{
return MD5PWD.Decode(str, "http://www.cnblogs.com/sufei");
}
}
本人的博客不再维护从2013年就不再维护了
需要我帮助的朋友请到我的个人论坛 http://www.sufeinet.com 进行讨论,感谢大家对我的支持!