【问题标题】:Encrypting to SHA1 visual basic - VB 2010加密到 SHA1 Visual Basic - VB 2010
【发布时间】:2012-06-07 00:40:21
【问题描述】:

我有一个在线 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 岁)!

【问题讨论】:

  • Sha1 不是加密,而是散列。请使用盐... 不像 LinkedIn money.cnn.com/2012/06/06/technology/linkedin-password-hack/…
  • @EricJ。哇。真是一群白痴。感谢您的警告。
  • 您的代码看起来或多或少是正确的,如果其意图是获取纯文本密码并返回该密码的 SHA1 哈希(未加盐!!!请使用加盐)。究竟是什么不适合你?
  • 嗨,埃里克,我该怎么加盐呢?请问你能给我一个关于如何加盐的线索吗?我确实提到我是这个主题的新手 0.0 无论如何,我感谢您的贡献,谢谢!
  • 谁能帮帮我?我还是卡住了!

标签: vb.net


【解决方案1】:
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


所以:-)这是我为你创建的一个小程序,希望它有用,学习愉快。
提示:忽略额外的定位代码,这不过是一个懒惰的程序员的旧习惯......顺便说一句,哈希是一种方式,加密是两种方式(您可以先编码然后解密以获取相同的数据,但是您无法取消散列数据)。

【讨论】:

  • 您好,感谢您花时间为我创建一个程序,但是当我比较论坛数据库中的哈希密码并使用您给我的程序时,我只得到相同的字符编号的不同结果。你知道为什么吗?再次感谢您花时间帮助我 =)
  • 如果您使用加盐 SHA1,结果“必须”不同,如果您感到困惑,试试这个windows.podnova.com/software/65512.htm,它包含最流行的散列算法,以帮助减轻您的痛苦 :-) 提供的 sha1毫无疑问是正确的,如果你想发布你的验证码
【解决方案2】:

未经测试!

如果你改变它会怎样......

... Dim encPassword As String = "" For Each b As Byte In bytesToHash encPassword += b.ToString("x2") 下一个 ...

到这里

Dim encPassword As String = "" encPassword = System.Text.Encoding.ASCII.GetString(bytesToHash);

【讨论】:

  • 感谢您的建议,但它不起作用,因为当我现在散列时,字符都不同..
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 1970-01-01
相关资源
最近更新 更多