c#实现MD5加密
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(puteHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
return;
}
else
{
//将获取的密码加密与数据库中加了密的密码相比较
byte[] by = puteHash(utf.GetBytes(texPassword.Text.Trim()));
string resultPass = System.Text.UTF8Encoding.Unicode.GetString(by);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = puteHash(result);
this.tbMd5pass.Text = BitConverter.ToString(output).Replace("-",""); //tbMd5pass为输出加密文本的文本框
t2 = t2.Replace("-", "");
return t2;
}
பைடு நூலகம்
/**//// <summary>
/// MD5 16位加密 加密后密码为小写
/// </summary>
/// <param name="ConvertString"></param>
SqlDataReader dr=comm.ExecuteReader();
if(dr.Read())
{
//用户存在,对密码进行检查
if(dr.GetValue(0).Equals(resultPass))
{
string user_name=dr.GetValue(1).ToString();
{
Console.WriteLine(UserMd5("8"));
Console.WriteLine(GetMd5Str("8"));
}
/**//// <summary>
/// MD5 16位加密 加密后密码为大写
}
}
}
方法三
C# MD5加密
C#开发笔记 一、C# MD5-16位加密实例,32位加密实例(两种方法)
环境:2005/sql server2000/xp测试通过
1.MD5 16位加密实例
方法一
首先,先简单介绍一下MD5
MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来, 经md2、md3和md4发展而来。
comm.Parameters.Add("@name",SqlDbType.NVarChar,40);
comm.Parameters["@name"].Value=name;
try
{
conn.Open();
comm.Connection=conn;
string user_Accountno=texName.Text.Trim();
Session["logon_name"]=user_name;
Session["logon_Accountno"]=user_Accountno;
//登录成功,进行页面导向
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace md5
{
class Program
{
static void Main(string[] args)
{
this.RegisterStartupScript("wc","<script language=javascript>alert('网络连接有异,请稍后重试。')</script>");
}
finally
{
conn.Close();
conn.ConnectionString=strConn;
SqlCommand comm = new SqlCommand();
string name = texName.Text.Trim().ToString();
mandText="select Ruser_pwd,Ruser_nm from Ruser where Accountno = @name";
string t2 = BitConverter.ToString(puteHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
方法二
C# md5加密(上)
string a; //加密前数据
string b; //加密后数据
b=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a,"MD5")
else
{
this.RegisterStartupScript("nu","<script language=javascript>alert('用户名不存在,请检查。')</script>");
}
}
catch(Exception exec)
using System;
using System.Security.Cryptography;
方法2
public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
byte[] targetData = puteHash(fromData);
string byte2String = null;
if(texName.Text.Trim()=="")
{
this.RegisterStartupScript("sf","<script language='javascript'>alert('用户名不能为空');document.all('texName').focus()</script>");
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
}
else
{
this.RegisterStartupScript("wp","<script language='javascript'>alert('密码错误,请检查。')</script>");
}
}
MD5具有很好的安全性(因为它具有不可逆的特征,加过密的密文经过解密后和加密前的东东相同的可能性极小)
引用
using System.Security.Cryptography;
using System.Text;
具体代码如下(写在按钮的Click事件里):
byte[] result = Encoding.Default.GetBytes(this.tbPass.Text.Trim()); //tbPass为输入密码的文本框
t2 = t2.ToLower();
for (int i=0; i<targetData.Length; i++)
{
byte2String += targetData[i].ToString("x");
}
return byte2String;
}
using System.Security.Cryptography;
}
C# MD5加密
using System.Security.Cryptography;
private void btnOK_Click(object sender, System.EventArgs e)
{
string strConn = "server=192.168.0.51;database=chengheng;User id=sa; password=123";
/// <summary>
/// 给一个字符串进行MD5加密
/// </summary>
/// <param name="strText">待加密字符串</param>
/// <returns>加密后的字符串</returns>
public static string MD5Encrypt(string strText)
return;
}
else if(texPassword.Text.Trim()=="")
{
this.RegisterStartupScript("sfs","<script language='javascript'>alert('密码不能为空');document.all('texPassword').focus()</script>");
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = puteHash(System.Text.Encoding.Default.GetBytes(strText));
return System.Text.Encoding.Default.GetString(result);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(puteHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
return;
}
else
{
//将获取的密码加密与数据库中加了密的密码相比较
byte[] by = puteHash(utf.GetBytes(texPassword.Text.Trim()));
string resultPass = System.Text.UTF8Encoding.Unicode.GetString(by);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = puteHash(result);
this.tbMd5pass.Text = BitConverter.ToString(output).Replace("-",""); //tbMd5pass为输出加密文本的文本框
t2 = t2.Replace("-", "");
return t2;
}
பைடு நூலகம்
/**//// <summary>
/// MD5 16位加密 加密后密码为小写
/// </summary>
/// <param name="ConvertString"></param>
SqlDataReader dr=comm.ExecuteReader();
if(dr.Read())
{
//用户存在,对密码进行检查
if(dr.GetValue(0).Equals(resultPass))
{
string user_name=dr.GetValue(1).ToString();
{
Console.WriteLine(UserMd5("8"));
Console.WriteLine(GetMd5Str("8"));
}
/**//// <summary>
/// MD5 16位加密 加密后密码为大写
}
}
}
方法三
C# MD5加密
C#开发笔记 一、C# MD5-16位加密实例,32位加密实例(两种方法)
环境:2005/sql server2000/xp测试通过
1.MD5 16位加密实例
方法一
首先,先简单介绍一下MD5
MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data security inc的ronald l. rivest开发出来, 经md2、md3和md4发展而来。
comm.Parameters.Add("@name",SqlDbType.NVarChar,40);
comm.Parameters["@name"].Value=name;
try
{
conn.Open();
comm.Connection=conn;
string user_Accountno=texName.Text.Trim();
Session["logon_name"]=user_name;
Session["logon_Accountno"]=user_Accountno;
//登录成功,进行页面导向
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace md5
{
class Program
{
static void Main(string[] args)
{
this.RegisterStartupScript("wc","<script language=javascript>alert('网络连接有异,请稍后重试。')</script>");
}
finally
{
conn.Close();
conn.ConnectionString=strConn;
SqlCommand comm = new SqlCommand();
string name = texName.Text.Trim().ToString();
mandText="select Ruser_pwd,Ruser_nm from Ruser where Accountno = @name";
string t2 = BitConverter.ToString(puteHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
t2 = t2.Replace("-", "");
方法二
C# md5加密(上)
string a; //加密前数据
string b; //加密后数据
b=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a,"MD5")
else
{
this.RegisterStartupScript("nu","<script language=javascript>alert('用户名不存在,请检查。')</script>");
}
}
catch(Exception exec)
using System;
using System.Security.Cryptography;
方法2
public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString);
byte[] targetData = puteHash(fromData);
string byte2String = null;
if(texName.Text.Trim()=="")
{
this.RegisterStartupScript("sf","<script language='javascript'>alert('用户名不能为空');document.all('texName').focus()</script>");
/// </summary>
/// <param name="ConvertString"></param>
/// <returns></returns>
public static string GetMd5Str(string ConvertString)
}
else
{
this.RegisterStartupScript("wp","<script language='javascript'>alert('密码错误,请检查。')</script>");
}
}
MD5具有很好的安全性(因为它具有不可逆的特征,加过密的密文经过解密后和加密前的东东相同的可能性极小)
引用
using System.Security.Cryptography;
using System.Text;
具体代码如下(写在按钮的Click事件里):
byte[] result = Encoding.Default.GetBytes(this.tbPass.Text.Trim()); //tbPass为输入密码的文本框
t2 = t2.ToLower();
for (int i=0; i<targetData.Length; i++)
{
byte2String += targetData[i].ToString("x");
}
return byte2String;
}
using System.Security.Cryptography;
}
C# MD5加密
using System.Security.Cryptography;
private void btnOK_Click(object sender, System.EventArgs e)
{
string strConn = "server=192.168.0.51;database=chengheng;User id=sa; password=123";
/// <summary>
/// 给一个字符串进行MD5加密
/// </summary>
/// <param name="strText">待加密字符串</param>
/// <returns>加密后的字符串</returns>
public static string MD5Encrypt(string strText)
return;
}
else if(texPassword.Text.Trim()=="")
{
this.RegisterStartupScript("sfs","<script language='javascript'>alert('密码不能为空');document.all('texPassword').focus()</script>");
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = puteHash(System.Text.Encoding.Default.GetBytes(strText));
return System.Text.Encoding.Default.GetString(result);