高分求加密(解密)代码 我正在做一个用户注册.用户登陆界面想在注册界面实现,密码的加密,然后充入数据库,想在登陆页面读取数据库的信息(解密).请大侠帮忙,我用的是 C#+SQL , .NET程序 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用enterprise 里的 Cryptography http://www.csdn.net/develop/article/26/26096.shtm http://dotnet.aspx.cc/article/1af1179e-43e5-4d4f-61ae-84922b5cba74/read.aspx孟子写的加密解密类 public string EncryptPassword(string PasswordString,string PasswordFormat ) { if (PasswordFormat="SHA1"){ EncryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1"); } elseif (PasswordFormat="MD5") { EncryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5"); } else { EncryptPassword=""; } 这个是一个类,把它编译成dll,你就可以直接用了,是desusing System;using System.IO; using System.Security.Cryptography;namespace bh{/// <summary>/// Security 的摘要说明。/// </summary>public class Security{const string KEY_64 = "xxxxxxxx"; const string IV_64 = "xxxxxxxx"; //注意了,是8个字符,64位 public Security(){//// TODO: 在此处添加构造函数逻辑//}public static string Encode(string data) //加密{ byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; MemoryStream ms = new MemoryStream(); CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length); } public static string Decode(string data) //解密{ byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); byte[] byEnc; try { byEnc = Convert.FromBase64String(data); } catch { return null; } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(byEnc); CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read); StreamReader sr = new StreamReader(cst); return sr.ReadToEnd(); }}} SHA1加密方法FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1"); MD5加密方法FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5"); 我用FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");加密以后在登陆页面如何判断,密码的正确性呢 wangsheng1984(可米) ( ) 信誉:100 Blog 2007-03-09 13:38:52 得分: 0 我用FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");加密以后在登陆页面如何判断,密码的正确性呢-----------------------------------------------------------------密码MD5加密判断的原理是这样的首先,把用户密码加密后保存到数据库判断的时候,把数据库密码取出,把当前用户输入的密码加密,两相对比如果相等,那就是密码匹配,反之就是密码不匹配把这个道理理解之后,比用什么现成加密组件都强 还有,MD5和SHA1都是不可逆加密所以用“把数据库密码取出,把当前用户输入的密码加密,两相对比”的方式 using System;using System.IO;using System.Security.Cryptography;using System.Text;namespace MDcode{/// <summary>/// Rijndael 的摘要说明。/// </summary>public class Rijndael{private System.Security.Cryptography.SymmetricAlgorithm sa;private string key;private string IV;public Rijndael(){sa = new RijndaelManaged();key = "!w@o#s$h%i^s&j*f(z)x_womenzuihao"; //"2S(a5%aE9&$820blOF*!ao43KplpPTq82%ggj(ai18*afkv3AfghKO28vs9favi(jFf*Fdja98w8JIlfo3v0f%lfjOi3jIf9*2jNv!eq";//这个自己可以改掉IV = "1011[{shuyu733}]"; //"v%fjh1aaG&94r8fi*fyh4afg0ir3QYgjOi0fatj3iV5Y(jkg6e!fg0r3f7GVR9gmrn(0ogEnJ6q%gjir30h(T530hbw!gmKeq2g*(n0j";//这个也可以改掉}private byte[] getKey(){string gKey = this.key;sa.GenerateKey();byte[] bTemp = sa.Key;int keyLength = bTemp.Length;if(this.key.Length > keyLength){gKey = gKey.Substring(0, keyLength);}else{gKey = gKey.PadRight(keyLength, ' ');}return ASCIIEncoding.ASCII.GetBytes(gKey);}private byte[] getIV(){string gIV = this.IV;sa.GenerateIV();byte[] bTemp = sa.IV;int IVLength = bTemp.Length;if(this.IV.Length > IVLength){gIV = gIV.Substring(0, IVLength);}else{gIV = gIV.PadRight(IVLength, ' ');}return ASCIIEncoding.ASCII.GetBytes(gIV);}public string RijndaelEncrypt(string re){byte[] reIn = UTF8Encoding.UTF8.GetBytes(re);MemoryStream ms = new MemoryStream();sa.Key = this.getKey();sa.IV = this.getIV();System.Security.Cryptography.ICryptoTransform it = sa.CreateEncryptor();System.Security.Cryptography.CryptoStream cs = new CryptoStream(ms, it, System.Security.Cryptography.CryptoStreamMode.Write);cs.Write(reIn, 0, reIn.Length);cs.FlushFinalBlock();byte[] reOut=ms.ToArray();ms.Close();cs.Close();return Convert.ToBase64String(reOut);}public string RijndaelDencrypt(string rd){byte[] rdIn = Convert.FromBase64String(rd);MemoryStream ms = new MemoryStream(rdIn, 0, rdIn.Length);sa.Key = this.getKey();sa.IV = this.getIV();System.Security.Cryptography.ICryptoTransform it = sa.CreateDecryptor();System.Security.Cryptography.CryptoStream cs = new CryptoStream(ms, it, System.Security.Cryptography.CryptoStreamMode.Read);StreamReader sr = new StreamReader(cs);string str = sr.ReadToEnd();ms.Close();cs.Close();return str;}}}实例化Rijndael对象RijndaelEncrypt为加密方法RijndaelDencrypt为解密方法 sql 分组 asp.net弹出层效果,遮盖背景的最好!能实现在弹出层上操作数据(比如登陆)更好!! 求助 关于一个Gridview的复杂问题 HTTPModule 安装的简单问题 一直困扰的UrlRewrite的问题,请指教 如果用OpenGL, DirectX, Direct 3D開發asp.net圖形,應該怎麽布署程序? 用C#实现datalist绑定的问题 生成HTML flexpaper显示swf问题,就差这一步了 关于GridView的两个属性的为题 ASP.NET 程序在本地已经调试成功,但放到服务器上却报错,如何解决???
孟子写的加密解密类
{
if (PasswordFormat="SHA1"){
EncryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
}
elseif (PasswordFormat="MD5")
{ EncryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
}
else
{
EncryptPassword="";
}
using System;
using System.IO;
using System.Security.Cryptography;namespace bh
{
/// <summary>
/// Security 的摘要说明。
/// </summary>
public class Security
{
const string KEY_64 = "xxxxxxxx";
const string IV_64 = "xxxxxxxx"; //注意了,是8个字符,64位
public Security()
{
//
// TODO: 在此处添加构造函数逻辑
//
}public static string Encode(string data) //加密
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateEncryptor(byKey,byIV),CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(),0,(int)ms.Length); } public static string Decode(string data) //解密
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
} DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms,cryptoProvider.CreateDecryptor(byKey,byIV),CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
}
}
FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"SHA1");
MD5加密方法
FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");
我用FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString ,"MD5");加密以后在登陆页面如何判断,密码的正确性呢-----------------------------------------------------------------密码MD5加密判断的原理是这样的
首先,把用户密码加密后保存到数据库
判断的时候,把数据库密码取出,把当前用户输入的密码加密,两相对比
如果相等,那就是密码匹配,反之就是密码不匹配把这个道理理解之后,比用什么现成加密组件都强
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MDcode
{
/// <summary>
/// Rijndael 的摘要说明。
/// </summary>
public class Rijndael
{
private System.Security.Cryptography.SymmetricAlgorithm sa;
private string key;
private string IV;public Rijndael()
{
sa = new RijndaelManaged();
key = "!w@o#s$h%i^s&j*f(z)x_womenzuihao"; //"2S(a5%aE9&$820blOF*!ao43KplpPTq82%ggj(ai18*afkv3AfghKO28vs9favi(jFf*Fdja98w8JIlfo3v0f%lfjOi3jIf9*2jNv!eq";//这个自己可以改掉
IV = "1011[{shuyu733}]"; //"v%fjh1aaG&94r8fi*fyh4afg0ir3QYgjOi0fatj3iV5Y(jkg6e!fg0r3f7GVR9gmrn(0ogEnJ6q%gjir30h(T530hbw!gmKeq2g*(n0j";//这个也可以改掉}
private byte[] getKey()
{
string gKey = this.key;sa.GenerateKey();
byte[] bTemp = sa.Key;
int keyLength = bTemp.Length;if(this.key.Length > keyLength)
{
gKey = gKey.Substring(0, keyLength);
}
else
{
gKey = gKey.PadRight(keyLength, ' ');
}return ASCIIEncoding.ASCII.GetBytes(gKey);
}
private byte[] getIV()
{
string gIV = this.IV;sa.GenerateIV();
byte[] bTemp = sa.IV;
int IVLength = bTemp.Length;if(this.IV.Length > IVLength)
{
gIV = gIV.Substring(0, IVLength);
}
else
{
gIV = gIV.PadRight(IVLength, ' ');
}return ASCIIEncoding.ASCII.GetBytes(gIV);
}public string RijndaelEncrypt(string re)
{
byte[] reIn = UTF8Encoding.UTF8.GetBytes(re);
MemoryStream ms = new MemoryStream();sa.Key = this.getKey();
sa.IV = this.getIV();System.Security.Cryptography.ICryptoTransform it = sa.CreateEncryptor();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(ms, it, System.Security.Cryptography.CryptoStreamMode.Write);cs.Write(reIn, 0, reIn.Length);
cs.FlushFinalBlock();byte[] reOut=ms.ToArray();
ms.Close();
cs.Close();return Convert.ToBase64String(reOut);
}public string RijndaelDencrypt(string rd)
{
byte[] rdIn = Convert.FromBase64String(rd);
MemoryStream ms = new MemoryStream(rdIn, 0, rdIn.Length);sa.Key = this.getKey();
sa.IV = this.getIV();System.Security.Cryptography.ICryptoTransform it = sa.CreateDecryptor();
System.Security.Cryptography.CryptoStream cs = new CryptoStream(ms, it, System.Security.Cryptography.CryptoStreamMode.Read);StreamReader sr = new StreamReader(cs);
string str = sr.ReadToEnd();
ms.Close();
cs.Close();return str;
}
}
}
实例化Rijndael对象
RijndaelEncrypt为加密方法
RijndaelDencrypt为解密方法