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

vb.net – 加密到SHA1 visual basic – VB 2010,第1张

概述我有一个在线SMF论坛,当用户注册时,密码在数据库中用SHA1加密.我需要创建一个具有登录功能的vb程序,只有论坛成员才能登录.现在,我陷入困境的部分是如何在Visual Basic中将密码加密为SHA1?我包含了一些我不知道的代码是否正确所以请帮助我. Imports System.Security.CryptographyPublic Class Form2Private Sub But 我有一个在线SMF论坛,当用户注册时,密码在数据库中用SHA1加密.我需要创建一个具有登录功能的vb程序,只有论坛成员才能登录.现在,我陷入困境的部分是如何在Visual Basic中将密码加密为SHA1?我包含了一些我不知道的代码是否正确所以请帮助我.

imports System.Security.CryptographyPublic Class Form2Private 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 = passwordSHAEnd SubPublic 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 FunctionPrivate Sub Form2_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.LoadEnd SubEnd Class

谢谢你,请不要因为我还在学习(我15岁)!

解决方法
imports System.IOPublic Class Form1Private 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 IfEnd SubFunction 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 strResultEnd FunctionPrivate 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 SubPrivate 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 IfEnd SubPrivate 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 IfEnd SubEnd Class

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

总结

以上是内存溢出为你收集整理的vb.net – 加密到SHA1 visual basic – VB 2010全部内容,希望文章能够帮你解决vb.net – 加密到SHA1 visual basic – VB 2010所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1247685.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存