我要的是输入密码的时候,输进数据库的时候用M5D进行加密,当输入密码的时候再用M5D解密进行匹配,有谁有例子或者代码在的,谢谢了,本人使用的是C#
解决方案 »
- 使用Area建立两个不同文件夹,存在相同的Controller,注册路由加上了命名空间参数,还是出错!
- 讨论下杨涛老师的分页控件(MvcPager)
- 征求高手!!求助HyperLink的Text文件不能绑定和读取?
- 声明static方法和普通方法的区别有哪些?什么时候用?
- 如何实现网页邮件(webMail)的收发?
- 大家帮我看看这句的意思,看错在哪里?谢谢!
- There is already an open DataReader associated with ..."问题的解决
- cookie的问题,很简单,50分相送。
- 请问怎样把MVC3的音乐商城在MVC4里面运行!!!
- 水晶报表中如何显示图像?
- 重装系统,重新注册IE WEB controls后treeview,toolbar曾经添加的项目不能用了?
- VB 中的RGB()函数 相当于C#中的那个方法啊!
密码加密后不可还原的。思路应该是把输入的密码进行MD5后和数据库的比较!
using System.IO;
using System.Text;
using System.Security.Cryptography;/// <summary>
/// 一个简单的使用.NET非对称加密算法的例子
/// 本例的程序很简单,仅用于说明如何在.NET里面使用非对称(RSA)算法。
/// Kwanhong 2005.9
/// </summary>
class Class1
{
public static void Main(string[] args)
{
Class1 c=new Class1();
c.StartDemo();
} public void StartDemo()
{
//RSA的加解密过程:
// 有 rsa1 和 rsa2 两个RSA对象。
// 现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
// rsa2 获取得公钥之后,用来加密要发送的数据内容。
// rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。 RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider(); string publickey;
publickey=rsa1.ToXmlString(false); //导出 rsa1 的公钥 string plaintext;
plaintext="你好吗?这是用于测试的字符串。"; //原始数据
Console.WriteLine("原始数据是:\n{0}\n",plaintext); rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息 //rsa2开始加密
byte[] cipherbytes;
cipherbytes=rsa2.Encrypt(
Encoding.UTF8.GetBytes(plaintext),
false); /*//////////////////////////////////////////////*/
Console.WriteLine("加密后的数据是:");
for(int i=0; i< cipherbytes.Length; i++)
{
Console.Write("{0:X2} ",cipherbytes[i]);
}
Console.WriteLine("\n");
/*//////////////////////////////////////////////*/ //rsa1开始解密
byte[] plaintbytes;
plaintbytes = rsa1.Decrypt(cipherbytes,false); Console.WriteLine("解密后的数据是:");
Console.WriteLine(Encoding.UTF8.GetString(plaintbytes)); Console.ReadLine();
}
}
using System.Security.Cryptography;
using System.Text;
using System.IO;/// <summary>
/// 使用对称加密的例子
/// </summary>
class Class2
{ static void Main(string[] args)
{
Class2 c=new Class2();
c.StartDemo();
} public void StartDemo()
{
//establish symmetric algorithm
SymmetricAlgorithm sa = Rijndael.Create(); //key and iv
sa.GenerateKey(); //产生随机的 (32*8) 位的密钥
//sa.GenerateIV(); //初始向量,在ECB模式里面可以不用IV
sa.Mode = CipherMode.ECB; //块处理模式
sa.Padding = PaddingMode.Zeros; //末尾数据块的填充模式
Console.WriteLine("密钥是:"); ///////////
for (int i=0; i<sa.Key.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",sa.Key[i]); ///////////
} ///////////
Console.WriteLine("\n"); ///////////
//establish crypto stream
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,sa.CreateEncryptor(),CryptoStreamMode.Write); //
string plaintext; //原始文本
byte[] cipherbytes; //加密后的数据
byte[] finalbytes; //解密后的数据 plaintext="How are you? 这是一行文字。";
byte[] plainbytes = Encoding.UTF8.GetBytes(plaintext);
Console.WriteLine("原始文本是:\n{0}\n",plaintext);
//display plaint text byte array in hex format
Console.WriteLine("原始数据是:"); ///////////
for (int i=0; i<plainbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",plainbytes[i]); ///////////
} ///////////
Console.WriteLine("\n"); /////////// //加密过程
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherbytes = ms.ToArray();
ms.Close(); //display ciphertext byte array in hex format
Console.WriteLine("加密后的数据是:"); ///////////
for (int i=0; i<cipherbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",cipherbytes[i]);///////////
} ///////////
Console.WriteLine("\n"); ///////////
//下面的为加密过程
ms=new MemoryStream(cipherbytes);
cs=new CryptoStream(ms,sa.CreateDecryptor(),CryptoStreamMode.Read);
finalbytes=new byte[plainbytes.Length];
cs.Read(finalbytes,0,plainbytes.Length); Console.WriteLine("解密后的数据是:"); ///////////
for (int i=0; i<finalbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",finalbytes[i]); ///////////
} ///////////
Console.WriteLine("\n"); /////////// string finaltext=Encoding.UTF8.GetString(finalbytes); Console.WriteLine("解密后的文本是:\n{0}\n\n",finaltext );
Console.WriteLine("按任意键继续......");
Console.ReadLine();
}
}
Pass=hualuo.SYS.MD5.EncryptDES(Pass,"你的种子");//解密
//异常:密码错误
if (hualuo.SYS.MD5.DecryptDES(this.lDB.ResultTable.Rows[0]["user_psw"].ToString(),"你的种子") != this.txtPassword.Text)
{
this.txtPassword.Text = "";
this.lblInfo.Text = "密码错误!请确认后输入!";
return;
}
string hashedPassword = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password.Text, "MD5");
解密后和页面输入的字符串比较
private Byte[] pwdToMd5(string pwd)
{
MD5CryptoServiceProvider md5Hasher=new MD5CryptoServiceProvider();
Byte[] hashedBytes;
UTF8Encoding encoder=new UTF8Encoding();
hashedBytes=md5Hasher.ComputeHash(encoder.GetBytes(pwd));
return hashedBytes;
}
从数据库取出密码并判断密码是否正确:
private void checkPwd(string inputPwd)
{
string sql="select * from EmployeeMessage where E_PassWord=@pwd";
con = new SqlConnection(ConfigurationSettings.AppSettings["connectStr"]);
cmd = new SqlCommand(sql,con);
SqlParameter pwd = new SqlParameter("@pwd",SqlDbType.Binary,16);
pwd.Value = pwdToMd5(inputPwd);
cmd.Parameters.Add(pwd);
con.Open();
read = cmd.ExecuteReader();
if(read.Read())
{
// 密码正确
}
else
{
//密码错误
}
read.close();
con.close();
cmd.dispose();
}
[email protected]
发到的话,就在这里回复,我收到解决后就送分咯
http://www.cnitblog.com/ChengKing/articles/4009.html
Pass=hualuo.SYS.MD5.EncryptDES(Pass,"你的种子");//解密
//异常:密码错误
if (hualuo.SYS.MD5.DecryptDES(this.lDB.ResultTable.Rows[0]["user_psw"].ToString(),"你的种子") != this.txtPassword.Text)
{
this.txtPassword.Text= "";
this.lblInfo.Text= "密码错误!请确认后输入!";
return;
}
这个可以的吗