加密方法
Public function EncryptPassword(ByVal PasswordString As String, ByVal PasswordFormat As String) As String
Dim fa As System.Web.Security.FormsAuthentication
If PasswordFormat = "SHA1" Then
EncryptPassword = fa.HashPasswordForStoringInConfigFile(PasswordString, "SHA1")
ElseIf PasswordFormat = "MD5" Then
EncryptPassword = fa.HashPasswordForStoringInConfigFile(PasswordString, "MD5")
Else
EncryptPassword = ""
End If
End Function
MD5加密
VB.NET版
<
%@ Import
Namespace =
Namespace="System.Data.OleDb" %>
<%@ Import Namespace =Namespace="System.Data" %>
<form id="Form1" runat="server">
<asp:Button id="Button2" runat="server" Text="Button" onclick="denglu"></asp:Button>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button" onclick="zhuce"></asp:Button></form>
<script language="vb" runat="server">
Public Function EncryptPassword()function EncryptPassword(ByVal PasswordString As String) As String
Dim fa As System.Web.Security.FormsAuthentication
EncryptPassword = fa.HashPasswordForStoringInConfigFile(PasswordString, "MD5")
End Function
Dim sql,mima as String
Dim myconn as OleDbConnection
Dim myCmd as OleDbCommand
Function getdatareader()Function getdatareader(str as String) as OleDbDataReader
myCmd=New OleDbCommand(str,myConn)
myConn.open()
getdatareader=myCmd.ExecuteReader()
End Function
Function executesql()function executesql(sql as string)
myCmd=new OleDbCommand(sql,myConn)
myConn.open()
myCmd.ExecuteNonQuery()
myConn.close()
end function
Sub Page_Load()Sub Page_Load(sender As Object, e As EventArgs)
Dim strConn,username As String
strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&server.MapPath("/data/data.mdb")
myConn = New OledbConnection(strConn)
end sub
Sub zhuce()sub zhuce(sender As Object, e As EventArgs)
mima=EncryptPassword(textbox2.text)
sql="insert into users(username,mima) values('"& textbox1.text &"','" & mima & "')"
executesql(sql)
myConn.close()
end sub
Sub denglu()sub denglu(sender As Object, e As EventArgs)
mima=EncryptPassword(textbox2.text)
sql="select username,mima from users where username='"& textbox1.text &"' and mima='"& mima &"'"
Dim Reader as OleDbDataReader=getdatareader(sql)
if reader.read() then
response.write("OK")
end if
myConn.close()
End sub
</script> C#版
<%@ Import Namespace =Namespace="System.Data" %>
<form id="Form1" runat="server">
<asp:Button id="Button2" runat="server" Text="Button" onclick="denglu"></asp:Button>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button" onclick="zhuce"></asp:Button></form>
<script language="vb" runat="server">
Public Function EncryptPassword()function EncryptPassword(ByVal PasswordString As String) As String
Dim fa As System.Web.Security.FormsAuthentication
EncryptPassword = fa.HashPasswordForStoringInConfigFile(PasswordString, "MD5")
End Function
Dim sql,mima as String
Dim myconn as OleDbConnection
Dim myCmd as OleDbCommand
Function getdatareader()Function getdatareader(str as String) as OleDbDataReader
myCmd=New OleDbCommand(str,myConn)
myConn.open()
getdatareader=myCmd.ExecuteReader()
End Function
Function executesql()function executesql(sql as string)
myCmd=new OleDbCommand(sql,myConn)
myConn.open()
myCmd.ExecuteNonQuery()
myConn.close()
end function
Sub Page_Load()Sub Page_Load(sender As Object, e As EventArgs)
Dim strConn,username As String
strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&server.MapPath("/data/data.mdb")
myConn = New OledbConnection(strConn)
end sub
Sub zhuce()sub zhuce(sender As Object, e As EventArgs)
mima=EncryptPassword(textbox2.text)
sql="insert into users(username,mima) values('"& textbox1.text &"','" & mima & "')"
executesql(sql)
myConn.close()
end sub
Sub denglu()sub denglu(sender As Object, e As EventArgs)
mima=EncryptPassword(textbox2.text)
sql="select username,mima from users where username='"& textbox1.text &"' and mima='"& mima &"'"
Dim Reader as OleDbDataReader=getdatareader(sql)
if reader.read() then
response.write("OK")
end if
myConn.close()
End sub
</script> C#版
public
string
TOMD5(
string
STR)
... {
System.Security.Cryptography.MD5CryptoServiceProvider ssc = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(STR);
byte[] MD5Out = ssc.ComputeHash(MD5Source);
string STROut = "";
for (int i = 0; i < MD5Out.Length; i++)
STROut = STROut + MD5Out[i].ToString("X2");
return STROut;
}
... {
System.Security.Cryptography.MD5CryptoServiceProvider ssc = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] MD5Source = System.Text.Encoding.UTF8.GetBytes(STR);
byte[] MD5Out = ssc.ComputeHash(MD5Source);
string STROut = "";
for (int i = 0; i < MD5Out.Length; i++)
STROut = STROut + MD5Out[i].ToString("X2");
return STROut;
}
DES加密
Imports
System.Security.Cryptography
Imports System.IO
Private Shared KEY_64() As Byte = { 42 , 16 , 93 , 156 , 78 , 4 , 218 , 32 }
Private Shared IV_64() As Byte = { 55 , 103 , 246 , 79 , 36 , 99 , 167 , 3 }
' 标准的DES加密
Public Shared Function Encrypt() Function Encrypt(ByVal value As String) As String
If value <> "" Then
Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
'再转换为一个字符串
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End If
End Function
' 标准的DES解密
Public Shared Function Decrypt() Function Decrypt(ByVal value As String) As String
If value <> "" Then
Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider
'从字符串转换为字节组
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)
Return sr.ReadToEnd()
End If
End Function
所有字段。。用page.request的时候都要先转换 " " 为 " + " 。然后再解密
即把 Dim buffer As Byte () = Convert.FromBase64String(value)改为
Dim buffer As Byte () = Convert.FromBase64String(value.replace( " " , " + " ))
Imports System.IO
Private Shared KEY_64() As Byte = { 42 , 16 , 93 , 156 , 78 , 4 , 218 , 32 }
Private Shared IV_64() As Byte = { 55 , 103 , 246 , 79 , 36 , 99 , 167 , 3 }
' 标准的DES加密
Public Shared Function Encrypt() Function Encrypt(ByVal value As String) As String
If value <> "" Then
Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
'再转换为一个字符串
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
End If
End Function
' 标准的DES解密
Public Shared Function Decrypt() Function Decrypt(ByVal value As String) As String
If value <> "" Then
Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider
'从字符串转换为字节组
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)
Return sr.ReadToEnd()
End If
End Function
所有字段。。用page.request的时候都要先转换 " " 为 " + " 。然后再解密
即把 Dim buffer As Byte () = Convert.FromBase64String(value)改为
Dim buffer As Byte () = Convert.FromBase64String(value.replace( " " , " + " ))
方法二:
VB.NET版
Imports
System.Security.Cryptography
Imports System.IO
Imports System.Text
' 加密
Public Shared Function EncryptText() Function EncryptText(ByVal strText As String) As String
Return Encrypt(strText, "&%#@?,:*")
End Function
' 加密函数
Private Shared Function Encrypt() Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim des As New DESCryptoServiceProvider
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
' 解密
Public Shared Function DecryptText() Function DecryptText(ByVal strText As String) As String
Return Decrypt(strText, "&%#@?,:*")
End Function
' 解密函数
Private Shared Function Decrypt() Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
Public Sub ShowRes() Sub ShowRes(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
TextBox2.Text = EncryptText(TextBox1.Text)
TextBox2.Text = DecryptText(TextBox1.Text)
End Sub
Imports System.IO
Imports System.Text
' 加密
Public Shared Function EncryptText() Function EncryptText(ByVal strText As String) As String
Return Encrypt(strText, "&%#@?,:*")
End Function
' 加密函数
Private Shared Function Encrypt() Function Encrypt(ByVal strText As String, ByVal strEncrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim des As New DESCryptoServiceProvider
Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
' 解密
Public Shared Function DecryptText() Function DecryptText(ByVal strText As String) As String
Return Decrypt(strText, "&%#@?,:*")
End Function
' 解密函数
Private Shared Function Decrypt() Function Decrypt(ByVal strText As String, ByVal sDecrKey As String) As String
Dim byKey() As Byte = {}
Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Dim inputByteArray(strText.Length) As Byte
Try
byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
Dim des As New DESCryptoServiceProvider
inputByteArray = Convert.FromBase64String(strText)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
Return ex.Message
End Try
End Function
Public Sub ShowRes() Sub ShowRes(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged
TextBox2.Text = EncryptText(TextBox1.Text)
TextBox2.Text = DecryptText(TextBox1.Text)
End Sub
C#版
//
加密
public string EncryptText( string strText)
... {
return Encrypt(strText, "&%#@?,:*");
}
// 加密函数
public string Encrypt( string strText, string sEncrKey)
... {
byte[] byKey = ...{ };
byte[] IV = ...{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
...{
byKey = Encoding.UTF8.GetBytes(sEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
...{
return ex.Message;
}
}
// 解密
public string DecryptText( string strText)
... {
return Decrypt(strText, "&%#@?,:*");
}
// 解密函数
public string Decrypt( string strText, string sDecrKey)
... {
byte[] byKey = ...{ };
byte[] IV = ...{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new byte[strText.Length];
try
...{
byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText.Replace(" ","+"));
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
...{
return ex.Message;
}
}
public string EncryptText( string strText)
... {
return Encrypt(strText, "&%#@?,:*");
}
// 加密函数
public string Encrypt( string strText, string sEncrKey)
... {
byte[] byKey = ...{ };
byte[] IV = ...{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
try
...{
byKey = Encoding.UTF8.GetBytes(sEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception ex)
...{
return ex.Message;
}
}
// 解密
public string DecryptText( string strText)
... {
return Decrypt(strText, "&%#@?,:*");
}
// 解密函数
public string Decrypt( string strText, string sDecrKey)
... {
byte[] byKey = ...{ };
byte[] IV = ...{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = new byte[strText.Length];
try
...{
byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText.Replace(" ","+"));
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
...{
return ex.Message;
}
}