使用AES_DECRYPT将密码解密到文本框 [英] Decrypt password to a textbox using AES_DECRYPT

查看:173
本文介绍了使用AES_DECRYPT将密码解密到文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在VB.NET 2010和MySQL中使用AES_ENCRYPT加密密码,但在尝试使用AES_DECRYPT将加密密码检索到文本框时,它显示为空。

请有人帮我丢失的代码。



我的代码是....



我有什么试过:



加密代码:

 sql.CommandText =INSERT INTO用户(uid, uname,urole,upwd,mob)VALUES('& Me.lblUID.Text&','& Me.txtUser.Text&','& Me.cmbRole.SelectedValue& ',AES_ENCRYPT('& pwd&','& ed&'),'& Me.txtPwd.Text&')




我试过的解密代码:

 comm.CommandText =SELECT uname,uid,urole FROM users WHERE uid =' &修剪(Me.txtUID.Text)& '和upwd ='& AES_DECRYPT(Me.txtPwd.Text)& '







解体功能:



 Dim cipherBytes As Byte()= Convert.FromBase64String(cipherText)
使用加密器As Aes = Aes.Create()
Dim pdb As New Rfc2898DeriveBytes(EncryptionKey,New Byte) (){& H49,& H76,& H61,& H6E,& H20,& H4D,_
& H65,& H64,& H76,& H65,& H64,& H65,_
& H76})
encryptor.Key = pdb.GetBytes(32)
encryptor.IV = pdb.GetBytes(16)
使用ms As New MemoryStream()
使用cs As New CryptoStream(ms,encryptor.CreateDecryptor(),CryptoStreamMode.Write)
cs.Write(cipherBytes,0,cipherBytes.Length)
cs.Close ()
结束使用
cipherText = Encoding.Unicode.GetString(ms.ToArray())
结束使用
结束使用
返回cip herText

解决方案

评论都很好,如果可能的话,不要进行sql连接并更改密码的方式。



但是,要回答当前的问题,您使用变量ed中的任何键加密字符串,但是当您解密时,不提供相同的密钥。



请参阅 MySQL: :MySQL 5.7参考手册:: 12.13加密和压缩函数 [ ^ ]


不是您问题的直接解决方案,而是您遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

I am able to encrypt password using AES_ENCRYPT in VB.NET 2010 and MySQL, but while trying to retrieve the encrypted password into a textbox using AES_DECRYPT it is showing null.
Please anyone can help me with the missing code.

My code is....

What I have tried:

encryption code:

sql.CommandText = "INSERT INTO users(uid,uname,urole,upwd,mob)VALUES('" & Me.lblUID.Text & "','" & Me.txtUser.Text & "','" & Me.cmbRole.SelectedValue & "', AES_ENCRYPT('" & pwd & "','" & ed & "'),'" & Me.txtPwd.Text & "')"



decryption code which I tried:

comm.CommandText = "SELECT uname,uid,urole FROM users WHERE uid= '" & Trim(Me.txtUID.Text) & "' AND upwd= '" & AES_DECRYPT(Me.txtPwd.Text) & "' "




DECRYPT FUNCTION:

Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
        Using encryptor As Aes = Aes.Create()
            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, _
             &H65, &H64, &H76, &H65, &H64, &H65, _
             &H76})
            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)
            Using ms As New MemoryStream()
                Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)
                    cs.Write(cipherBytes, 0, cipherBytes.Length)
                    cs.Close()
                End Using
                cipherText = Encoding.Unicode.GetString(ms.ToArray())
            End Using
        End Using
        Return cipherText

解决方案

The comments are all good, don't do sql concatenation and change how you do passwords, if possible.

However, to answer the immediate question, you encrypt the string using a key of whatever is in the variable ed but when you decrypt you do not provide the same key.

See MySQL :: MySQL 5.7 Reference Manual :: 12.13 Encryption and Compression Functions[^]


Not directly a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]


这篇关于使用AES_DECRYPT将密码解密到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆