我有一個在線SMF論壇,並且當用戶註冊時,密碼在數據庫中用SHA1加密。我需要創建一個只有論壇成員可以登錄的登錄功能的vb程序。現在我堅持的部分是如何在Visual Basic中將密碼加密到SHA1中?我包含了一些我不知道的代碼是否正確,請幫助我。加密到SHA1 visual basic - VB 2010
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歲)!
btw:散列!=加密 –
Sha1不是加密,而是散列。請使用鹽...... **不像LinkedIn ** http://money.cnn.com/2012/06/06/technology/linkedin-password-hack/index.htm?hpt=hp_t3 –
@EricJ。哇。真是一堆白癡。謝謝你的提醒。 – sarnold