加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

vb.net – 加密到SHA1 visual basic – VB 2010

发布时间:2020-12-17 07:25:52 所属栏目:百科 来源:网络整理
导读:我有一个在线SMF论坛,当用户注册时,密码在数据库中用SHA1加密.我需要创建一个具有登录功能的vb程序,只有论坛成员才能登录.现在,我陷入困境的部分是如何在Visual Basic中将密码加密为SHA1?我包含了一些我不知道的代码是否正确所以请帮助我. Imports System.S
我有一个在线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岁)!

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读