Home arrow VB.Net arrow Page 6 - String Encryption With Visual Basic .NET

String Encryption With Visual Basic .NET

In this article Brian Patterson will cover some of the encryption classes provided in the .NET Framework. Once you have got a grasp on how to use these algorithms, he will wrap things up with a sample order application that will encrypt credit card information before saving it to either a SQL Server or Oracle database server.

Author Info:
By: Wrox Team
Rating: 4 stars4 stars4 stars4 stars4 stars / 282
November 14, 2002
  1. · String Encryption With Visual Basic .NET
  2. · Encryption and the .NET Framework
  3. · Encryption and Byte Arrays
  4. · Building the Encryption Class
  5. · Creating the Database
  6. · Decrypting the Order Data
  7. · Conclusion

print this article

String Encryption With Visual Basic .NET - Decrypting the Order Data
(Page 6 of 7 )

As you may recall, when we started building this application we added a form to the project called AllOrders.vb. From the Project Explorer, go to this form and double click on the form. We want our code to be placed in the Form Load event. When the form loads, the data will be retrieved from the database, decrypted and shown in the ListView control. We could simply use a Grid control and bind to the database, but since we are dealing with encrypted data, we would have no way to decrypt the data before it is displayed in the control. Therefore, we must loop through every record, decrypting and displaying it in the ListView.

Add the following code (in bold) to the form load event.

Private Sub AllOrders_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dr As DataRow
Dim CCNumber As String
Dim TDES As TripleDES
Dim lvi As ListViewItem

' load all of the orders
Dim gsConn As SqlConnection = New SqlConnection("data source=dotnetserver;initial catalog=3DESOrders;
Trusted_Connection=true;workstation id=POWERHOUSE;packet size=4096")

' Declare our select command to retrieve all orders from the database
Dim selectCMD As SqlCommand = New SqlCommand("SELECT * from CUSTOMER_ORDER", gsConn)
selectCMD.CommandTimeout = 30
Dim itemDA As SqlDataAdapter = New SqlDataAdapter()
itemDA.SelectCommand = selectCMD

' Fill a dataset with all the orders.
Dim orderDS As DataSet = New DataSet()
itemDA.Fill(orderDS, "CUSTOMER_ORDER")

' Loop through all the orders decrypting the credit card
' information
For Each dr In orderDS.Tables("CUSTOMER_ORDER").Rows
TDES = New TripleDES()
CCNumber = TDES.Decrypt(dr.Item("CC_NUMBER"))

' Now that the information has been decrypted, lets
' add it to our ListView control.
lvi = New ListViewItem()
lvi.Text = dr.Item("FIRST_NAME")

End Sub

Again, this code is intended for SQL Server, to use it you must change the data source specification to point to the server you intend to use. To use this with Oracle, all you need to do is change to connection string to use the Oracle driver as well as change all Sql references to OleDb. Change the connection string to the following line of code for use with Oracle:

Dim gsConn As OleDbConnection = New OleDbConnection ("Provider=MSDAORA.1;Password=orders;User ID=orders;Data Source=tst")

With this code in place you are now able to load and decrypt the data from the database. One thing that is missing, however, is code to actually open the form up. Return to Form1 of the project and double click the View Orders button so we can add some code to its Click event. Add the following code to the click event of that button.

Dim frmAllOrders As New AllOrders()

We can proceed by running the application and clicking the View Orders button. After a few moments you should decrypted data displayed in the ListView as seen here.

blog comments powered by Disqus

- MyClass - Implementing Polymorphism in VB.Net
- Building a News Ticker Using VB.Net
- Everything You Wanted to Know About Forms In...
- Building Assemblies with VB.Net
- Simple VB.NET Notify Icon with Panel Applica...
- Regular Expressions in .NET
- String Encryption With Visual Basic .NET
- Deploying Applications in VB.NET: Part 1/2
- Watching Folder Activity in VB.NET
- Creating A Windows Service in VB.NET
- Implementing The Google Web Service In VB.NET
- Migrating to Visual Basic.NET from Visual Ba...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials