Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 19-Jun-17 8:50am
Comments
Dave Kreskowiak 19-Jun-17 12:09pm    
NEVER store a clear test or encrypted password in the database. Use a cryptographic hash of the salted password instead and store the resulting bytes. The hash cannot be reversed back into the password and is thus more secure.

Also, NEVER use string concatenation to build an SQL query. Always use parameterized queries to mitigate against SQL Injection Attacks. Your code, as it stands, can be easily broken just by putting a ' character in the user ID or password.

F-ES Sitecore 19-Jun-17 12:12pm    
Implementing encryption when your code is open to SQL injection attacks is a bit pointless. That aside, surely you need to search on the encrypted version of the text?

comm.CommandText = "SELECT uname,uid,urole FROM users WHERE uid= '" & Trim(Me.txtUID.Text) & "' AND upwd= '<THIS SHOULD BE THE AES ENCRYPTED PASSWORD>'"

However what you traditionally do is retrieve the password based just on the ID, then encrypt the password supplied and check the encrypted version matches the stored version.
Richard MacCutchan 19-Jun-17 12:44pm    
Actually passwords should never be encrypted.
F-ES Sitecore 20-Jun-17 4:14am    
Not in a real world app, no.
Richard MacCutchan 20-Jun-17 4:18am    
And you think these questions are not connected to real world apps? :)

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[^]
 
Share this answer
 
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900