zss1100
爱就要负责

加密方法
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加密
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import 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(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(str as String) as OleDbDataReader
   myCmd=New OleDbCommand(str,myConn)
   myConn.open()
   getdatareader=myCmd.ExecuteReader()
End Function
function executesql(sql as string)
   myCmd=new OleDbCommand(sql,myConn)
   myConn.open()
   myCmd.ExecuteNonQuery()
   myConn.close()
end function

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(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(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> 

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(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(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.Security.Cryptography
Imports System.IO
Imports System.Text

     ' 加密
    Public Shared Function EncryptText(ByVal strText As String) As String
        Return Encrypt(strText, "&%#@?,:*")
    End Function

 
    '加密函数
    Private Shared 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(ByVal strText As String) As String
        Return Decrypt(strText, "&%#@?,:*")
    End Function

    '解密函数
    Private Shared 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(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged

        TextBox2.Text = EncryptText(TextBox1.Text)

        TextBox2.Text = DecryptText(TextBox1.Text)

    End Sub


posted on 2007-03-04 17:41  打你鸟鸟头  阅读(233)  评论(0编辑  收藏  举报