2012-06-07 57 views
4

我有一個在線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歲)!

+2

btw:散列!=加密 –

+0

Sha1不是加密,而是散列。請使用鹽...... **不像LinkedIn ** http://money.cnn.com/2012/06/06/technology/linkedin-password-hack/index.htm?hpt=hp_t3 –

+0

@EricJ。哇。真是一堆白癡。謝謝你的提醒。 – sarnold

回答

12
Imports System.IO 
Public Class Form1 

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown 
    If e.KeyValue = Keys.Enter Then 
     TextBox2.Text = getSHA1Hash(TextBox1.Text) 
     Label3.Text = TextBox2.Text.Length 
    End If 
End Sub 

Function getSHA1Hash(ByVal strToHash As String) As String 

    Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider 
    Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash) 

    bytesToHash = sha1Obj.ComputeHash(bytesToHash) 

    Dim strResult As String = "" 

    For Each b As Byte In bytesToHash 
     strResult += b.ToString("x2") 
    Next 

    Return strResult 

End Function 

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
    Dim fs As New FileStream("location.txt", FileMode.OpenOrCreate, FileAccess.Write) 
    Dim sr As New StreamWriter(fs) 
    fs.SetLength(0) 
    sr.WriteLine(Me.Location.X) 
    sr.WriteLine(Me.Location.Y) 
    sr.Close() 
    fs.Close() 
End Sub 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    If File.Exists("location.txt") Then 
     Dim fs As New FileStream("location.txt", FileMode.Open, FileAccess.Read) 
     Dim sr As New StreamReader(fs) 

     Me.Location = New Point(sr.ReadLine, sr.ReadLine) 
     sr.Close() 
     fs.Close() 
    End If 
End Sub 

Private Sub TextBox1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp 
    If e.KeyValue = Keys.Escape Then 
     Application.Exit() 
    End If 
End Sub 
End Class 

enter image description here
所以它:-)我爲您創建了一個小程序,希望這將是有益的,快樂的學習。
提示:忽略額外的定位代碼,它只不過是一個懶惰程序員的舊習慣......順便說一句,散列是一種方式,加密是兩種方式(您可以加密並解密以獲取相同的數據,但你無法取消散列數據)。

+0

嗨,感謝您花時間爲我創建一個程序,但是當我比較論壇中的散列密碼數據庫和使用你給我的程序,我得到不同的結果只有相同的字符數字。你知道爲什麼嗎?再次非常感謝您花時間幫助我=) –

+0

如果您使用鹽漬SHA1,結果「必須」會不同,如果您感到困惑,請嘗試http://windows.podnova.com/software/65512.htm,它包含最流行的哈希算法,以幫助緩解你的痛苦:-)提供的sha1毫無疑問是正確的,如果你想發佈你的驗證碼 – sarepta

0

未經測試!

,如果你改變這個什麼...

 

... 

Dim encPassword As String = "" 

For Each b As Byte In bytesToHash 
    encPassword += b.ToString("x2") 
Next 

... 

這個

 
Dim encPassword As String = "" 

encPassword = System.Text.Encoding.ASCII.GetString(bytesToHash); 
+0

感謝您的建議隊友,但它沒有工作,因爲當我現在散列,字符都是不同的.. –