Question database encryption

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
hi every body
1- if i have aform with 5 textboxes for example connected to data base how can i encrypt it to save it in the database and how can i decrypt it when i call it from the data base ?
2- if i have many sql queries like sum insert avreage with conditions how can i make it with the encrypted data?
thank u and i need help
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
If you were to encrypt data stored in a database it would only be the text. There's not much point encrypting numbers, and it's numbers that you would be performing aggregate functions on, so there's no problem.
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
thank u for ur help but i need to decrypt those text when i retrieve it in the form like a grid view or text boxes and also how can encrypt it when i save it in a database if is there any simple application plz attach it
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
The database doesn't know or care about encryption. It simply saves the data you tell it to save. If you want to save encrypted data then you encrypt it and then save the result in the database. After you retrieve the data from the database you decrypt it.

The .NET Framework has an entire namespace dedicated to encryption, i.e. System.Security.Cryptography. You must not have made any effort at all to find information because there's plenty out there.

encrypt text vb.net - Google Search
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
its so hard for me to find it iam just need asimple code to encrypt two textbox before inserting its value in ms acces databse and the codes of decrypt them when i show it in textbox or a grid view plz help
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
Developer_mahmoud, jmcilhinney has given you a link to a Google search that gives you some very good resources on doing Encryption in VB.NET. You can also have a look on my website SatalKeto.co.uk I have at least one article on there about encryption in VB.NET.
From what jmcilhinney has given you (or using the encryption stuff from my website) you should be able to do what you need. If there is something specific you're struggling with, then specify what it is rather than just saying it is hard, we wont write the code for you.
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
dear satal
iam not that lazy beblieve me i searshed alot but honestly i didnt found adata base encryption code its all about how to encrypt and decrypt password and a string text box but wat i want is how to decrypt a grid view or abinding textboxes basicly i have afield called item name i encrypt this field but when i show it in a grid view or even a textboxes i cant decrypt it thats all any way thank u for ur help
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
You wouldn't decrypt a GridView you would decrypt the data and then load it into the GridView, so by filling a DataTable then decrypting the entries that have been encrypted (which would usually be all entries in a particular column). The same go for the TextBoxes, you would get the data from the database, decrypt it and then put it into the TextBox. When saving you would read the TextBox, encrypt it then save it to the database.
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
thank u very much


Private lbtVector() As Byte = {240, 3, 45, 29, 0, 76, 173, 59}
Private lscryptoKey As String = "ChangeThis!"

'Author : Nikhil Gupta
'Description : This function encrypts a given string
'Parameters : String
'Return Values: Encrypted String
'Called From : Business Layer

'Encrypt String
Public Function psEncrypt(ByVal sInputVal As String) As String
Dim loCryptoClass As New TripleDESCryptoServiceProvider
Dim loCryptoProvider As New MD5CryptoServiceProvider
Dim lbtBuffer() As Byte

Try
lbtBuffer = System.Text.Encoding.ASCII.GetBytes(sInputVal)
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
loCryptoClass.IV = lbtVector
sInputVal = Convert.ToBase64String(loCryptoClass.CreateEncryptor().TransformFinalBlock(lbtBuffer, 0, lbtBuffer.Length()))
psEncrypt = sInputVal
Catch ex As CryptographicException
Throw ex
Catch ex As FormatException
Throw ex
Catch ex As Exception
Throw ex
Finally
loCryptoClass.Clear()
loCryptoProvider.Clear()
loCryptoClass = Nothing
loCryptoProvider = Nothing
'Pass the encrypted strings to text box
txtEncryptedData.Text = sInputVal
End Try

'Pass the encrypted strings to text box
Return sInputVal
End Function

'Function to Decrypt Strings
Public Function psDecrypt(ByVal sQueryString As String) As String

Dim buffer() As Byte
Dim loCryptoClass As New TripleDESCryptoServiceProvider
Dim loCryptoProvider As New MD5CryptoServiceProvider

Try

buffer = Convert.FromBase64String(sQueryString)
loCryptoClass.Key = loCryptoProvider.ComputeHash(ASCIIEncoding.ASCII.GetBytes(lscryptoKey))
loCryptoClass.IV = lbtVector
Return Encoding.ASCII.GetString(loCryptoClass.CreateDecryptor().TransformFinalBlock(buffer, 0, buffer.Length()))
Catch ex As Exception
Throw ex
Finally
loCryptoClass.Clear()
loCryptoProvider.Clear()
loCryptoClass = Nothing
loCryptoProvider = Nothing
End Try

'show the decrypted data
Return sQueryString

this code working normally with me but it just encrypt and decrypt english language wat about if ineed other language like arabic
another question wat will be the code by these functions to decrypt the textbox 1 for example before iget it from the database and the grid view if u dont mind i know iam disturbing u but so sorry
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
When you code please use the code tags, it makes your code actually readable. You can do the code tags like
Code:
[code ][/code ]
(you will need to remove the spaces before the "]")

I've never done anything with other languages, but I would assume that changing the code from using ASCII to UTF8 should work, but you will have to test it out as while I believe UTF8 has support for other languages I could be wrong.
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
no it doesnt work
but satal u dont answer my second question
wat will be the code by these functions to decrypt the textbox 1 for example before iget it from the database and the grid view if u dont mind
 

Satal Keto

Well-known member
Joined
May 15, 2006
Messages
86
Programming Experience
5-10
no it doesnt work
What doesn't work? Are you not able to use UTF8? Does UTF8 not support Arabic? Be more specific with your response!

I don't remember seeing that question, but ow well. As I mentioned before for the TextBox you would get the encrypted string from the database, then if you have a look at the code you posted, you might notice that there is a decrypt function, perhaps you could use something like that to decrypt the encrypted string, then put it in the TextBox. The GridView you would get a DataTable of the information that you want from the database, then you would go through each row in that DataTable, decrypting the value in a manner which would be reminisent of the TextBox, then you would use the DataTable with the decrypted entries as the DataSource for the GridView.

If you don't know how to get the information from the database then Google it, there are a plethora of tutorials on the Internet, which will show you how to do it. If you're unable to do that then perhaps you should try and do something a bit easier.
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
thank u satal 4 ur help
UTF8 not support arabic
and about my encryptoin question u want me to searsh on google basicly u know that this forumsis to learn and to help each other i searched in google but invain any way thank u so much for ur help
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
i will test it again
my second question i have afield called item name and i work with dataset and i have afunction called decrypt so to decrypt that field in ther datagridview first i will show the gridview then i will use this code
[decrypt(datagridview1.columns(itemname)]
is it right?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
i will test it again
my second question i have afield called item name and i work with dataset and i have afunction called decrypt so to decrypt that field in ther datagridview first i will show the gridview then i will use this code
[decrypt(datagridview1.columns(itemname)]
is it right?
No it isn't right. As we have said more than once, you get the data, then you decrypt it, then you display it. You populate a DataTable, you loop through that DataTable to decrypt each row, then you bind the data to the grid.
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
thank u soooooooo much i owe you ok that about the grid view wat about the textbox how can i decrypt it before binding it
 

developer_mahmoud

Well-known member
Joined
Nov 13, 2010
Messages
64
Programming Experience
1-3
cmd = ("select itemname from item ")

DS.Clear()

CON.Open()
Dim DataAdapter1 As New OleDbDataAdapter(cmd, CON)
Dim dt As DataTable
DataAdapter1.Fill(DS, "dt")

Dim row As DataRow
For Each row In dt.Rows
Dim x As String
x = row("itemname")
x = decrypt(x)
Next row
CON.Close()
DataGridView1.DataSource = DS
DataGridView1.DataMember = "dt"
imake it like this but it doesnt work
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
In your loop, you are getting the data from the DataRow and assigning it to 'x'. You then decrypt the data stored in 'x' and assign the result back to 'x'. You then don't actually do anything with 'x'. You simply discard the decrypted result and go ahead and bind your DataTable, which still contains the original encrypted data. If you want to display the decrypted data then you have to display the decrypted data.
 
Top Bottom