我有一个在线 SMF 论坛,当用户注册时,密码在数据库中使用 SHA1 加密。我需要创建一个只有论坛成员才能登录的登录功能的 vb 程序。现在我陷入困境的部分是如何在 Visual Basic 中将密码加密为 SHA1?我包含了一些我不知道是否正确的代码,所以请帮助我。
Imports System.Security.Cryptography
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' declare those variables
Dim password As String
Dim passwordSHA As String
password = txtPassword.Text ' give password the value of the password textbox
Call passwordEncryptSHA(password) ' Lets call the first password encryption function for SHA1
passwordSHA = passwordEncryptSHA(password) ' give the variable the returned SHA value
' finally we will display both values in the corresponding textboxes
txtSHA1.Text = passwordSHA
End Sub
Public Function passwordEncryptSHA(ByVal password As String) As String
Dim sha As New SHA1CryptoServiceProvider ' declare sha as a new SHA1CryptoServiceProvider
Dim bytesToHash() As Byte ' and here is a byte variable
bytesToHash = System.Text.Encoding.ASCII.GetBytes(password) ' covert the password into ASCII code
bytesToHash = sha.ComputeHash(bytesToHash) ' this is where the magic starts and the encryption begins
Dim encPassword As String = ""
For Each b As Byte In bytesToHash
encPassword += b.ToString("x2")
Next
Return encPassword ' boom there goes the encrypted password!
End Function
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
谢谢,请不要吝啬,因为我还在学习(我 15 岁)!