Encryption and Decryption in VB.NET


How to secure the data at the time of transmissions in VB.NET?before going this chapter we see the concepts of encryption and decryption.

Encryption is the conversion of data into a form, called a cipher text, that cannot be easily understood by unauthorized people. Decryption is the process of converting encrypted data back into its original form,

Encryption:

original text ————– encrypt—————>cipher text

Decryption:

cipher text—————- decrypt—————->original text

The use of encryption/decryption is as old as the art of communication. In wartime, a cipher, often incorrectly called a “code,” can be employed to keep the enemy from obtaining the contents of data transmissions.

How to implement encryption and decryption in VB.NET?

Here,i will explain about the above form i just created for demo purpose,thae forms have three text boxes and two buttons.

Control          –   Name of Control

Textbox1       –   txtInput

Textbox2       –    txtEncryptText

TextBox3       –    txtDecryptText

Button1         –    btnEncrypt

Button2         –    btnDecrypt

>>Labels are used for identification purpose.

Just add a module.vb in your application,because you can access whole application in that modules whatever you write like vraiables,functions,procedures etc..

Module.vb

–>Namespaces

Imports System.Text
Imports System.Windows.Forms

Module Module1 –module beginning

—- declare public variables

Private Const INT_lens As Integer = 1
Public str As StringBuilder
Public searchStr As String
Dim b As Integer = 6
Dim p() As Integer = {2, 4, 7, 9, 3, INT_lens}
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim c As Integer
Dim lens As Integer

Encryption Function
Public Function Encrypt(ByVal inputstr As String)

str = New StringBuilder(inputstr)
lens = str.Length
While (lens < b) OrElse (lens Mod b)
str.Append(” “)
lens += INT_lens
End While
For i = 0 To ((lens / b) – INT_lens)
For j = 0 To (b – INT_lens)
k = p(j) + 100
c = (6 * i + j)
str.Replace(str.Chars(c), Chr(Asc(str.Chars(c)) + k), c, INT_lens)
Next
Next
Return str.ToString
str = Nothing

End Function

Decryption Function
Public Function Decrypt(ByVal inputstr As String)

str = New StringBuilder(inputstr)
lens = str.Length
While (lens < b) OrElse (lens Mod b)
str.Append(” “)
lens += INT_lens
End While

For i = 0 To ((lens / b) – INT_lens)
For j = 0 To (b – INT_lens)
k = p(j) + 100
c = (6 * i + j)
str.Replace(str.Chars(c), Chr(Asc(str.Chars(c)) – k), c, INT_lens)
Next
Next
Return str.ToString
str = Nothing
End Function

End Module –module end

Call both these functions on specific button click event

for Encryption

Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click

Me.txtEncryptText.Text = Encrypt(Me.txtInput.Text)

End Sub

for Decryption

Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecrypt.Click

Me.txtDecryptText.Text = Decrypt(Me.txtEncryptText.Text)

End Sub

Finally you will get output like this..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s