2013-10-17 160 views
1

我的项目包含2种形式,一种用于注册用户,另一种用于登录。我正在使用紧凑的本地数据库来存储密码。我写了一个函数来在用户注册时加密密码。然后,我写了另一个用户登录时解密相同的密码。加密/解密存储在精简数据库中的密码

第一部分,加密,工作得很好。用户注册,我可以看到数据库上加密的密码。但是,当我尝试登录时,密码未被解密。这是我的功能。

Module EncryptionModule 

    Public Function base64Encode(ByVal sData As String) As String 

     Try 
      Dim encData_Byte As Byte() = New Byte(sData.Length - 1) {} 
      encData_Byte = System.Text.Encoding.UTF8.GetBytes(sData) 
      Dim encodedData As String = Convert.ToBase64String(encData_Byte) 
      Return (encodedData) 

     Catch ex As Exception 

      Throw (New Exception("Error is base64Encode" & ex.Message)) 

     End Try 


    End Function 

    Public Function base64Decode(ByVal sData As String) As String 

     Dim encoder As New System.Text.UTF8Encoding() 
     Dim utf8Decode As System.Text.Decoder = encoder.GetDecoder() 
     Dim todecode_byte As Byte() = Convert.FromBase64String(sData) 
     Dim charCount As Integer = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length) 
     Dim decoded_char As Char() = New Char(charCount - 1) {} 
     utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0) 
     Dim result As String = New [String](decoded_char) 
     Return result 

    End Function 

End Module 

这是注册用户的日常和加密的密码:

Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click 

         'If the username is taken or used on the 
        'database, then create account 
        If MasterTableAdapter.CheckUserName(txtUserName.Text) = Nothing Then 

         Dim pwd As String = base64Encode(Trim(txtConfirmPassword.Text)) 

         MasterTableAdapter.CreateAccount(txtFName.Text, txtLName.Text, txtUserName.Text, pwd, int1) 

         MsgBox("An account has been created for: " & vbNewLine & _ 
         "Employee: " & txtFName.Text & " " & txtLName.Text & vbNewLine & _ 
         "User Name: " & txtUserName.Text & vbNewLine & _ 
         "Access Level: " & strAccessLevel) 

         Me.Close() 

        Else 

         MessageBox.Show("The username is in use. Please select another username.", "Authentication Error", MessageBoxButtons.OK, _ 
               MessageBoxIcon.Error) 

        End If 

End Sub 

这里登录并解密从登录表单的密码程序:

私人小组btnLogin_Click (发送者为对象,例如作为EventArgs的)把手btnLogin.Click

Dim pwd As String = base64Decode(Trim(txtPassword.Text)) 

      If Not MasterTableAdapter.Login(txtUserName.Text, pwd) = Nothing Then 
       'frmWelcomePage.Show() 

       MsgBox("SUCCESS") 

      Else 

       'If no match, display error, clear text boxes and send focus back to the username text box. 
       MessageBox.Show("Username or password do not match", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
       txtPassword.Text = Nothing 
       txtUserName.Text = Nothing 

       txtUserName.Focus() 

      End If 
End if 

End Sub 

我ñ在整个加密领域,所以我不知道我在这里做错了什么。

+2

字符串转换为base64编码是很难'crypting' – Steve

+0

如果你坚持做下去你的方式,如果你编码一个字符串,然后立即解码它,你会得到同样的东西吗? – Plutonix

+0

@Plutonix我只是试了一下,并没有奏效。我没有得到同样的结果。我是新手,所以我一直在摸索。 –

回答

2

首先,Base64编码是而不是加密。许多人可以看看B64字符串,并知道如何解读它。你应该像podiluska建议的那样研究哈希技术。

这就是说,由于您的解码方法无法解读您编码的内容,这意味着您在其中一个或另一个中有错误。你在做什么,可以做简单的编码:

Dim s As String = "MySecretPassword" 

' convert to byte array 
Dim bArry() As Byte = System.Text.Encoding.UTF8.GetBytes(s) 
' convert bytes to Base64: 
Dim sb64 As String = System.Convert.ToBase64String(barry) 

为了解码是正好相反:

' Base64 -> Byte Array 
Dim bOut() As Byte = System.Convert.FromBase64String(sb64) 
' Byte Arry -> clear text 
Dim sOut As String = System.Text.Encoding.UTF8.GetString(bOut) 
4

您不应该解密密码。

当用户创建一个密码,你应该生成一个散列(即:从该密码不能被重建的值)

当用户试图登录,您应该比较给定的哈希值密码与存储的散列。