How to Encrypt Strings in .NET –The easiest way

Whatever application you develop been it for the national security or for a one-man provision kiosk, some data should be encrypted. Things like database configuration variables, user login passwords and some application settings. In this article we will go through two methods of encrypting your variable with a basic knowledge in computing. The first been saving your records in bytes and the other in scrambled strings

Saving as bytes

Public Shared Function encrypt(ByVal password As String) As Byte()

Dim byteValue() As Byte = Encoding.UTF8.GetBytes(password)

Return byteValue

End Function

 

Public Shared Function decrypt(ByVal pass As Byte()) As String 

Dim password As String = Encoding.UTF8.GetString(pass)

Return password

End Function

 

For example, to encrypt your password fields, in the database design make the data type of the field and image. Then in your code write the simple methods below. You are done. At least you have encrypted the field to some extent; no one can easily get the password by easily checking from the backed

Encrypting Strings

In cases where you want to save your password as strings, or saving some application settings in the “My Settings” one simple way is by encrypting the string by adding a chosen value to each character in the string to change its meaning. Check the methods below

    Private Const code As Integer = 5

    Private Function StrEncrypt(ByVal str As String) As String

        Dim scrambled As String = “”

        Dim letters() As Char = str.ToCharArray

        For i As Integer = 0 To letters.Length – 1

            scrambled = scrambled + ChrW(AscW(letters(i)) + code)

        Next

        Return scrambled

    End Function

 

    Private Function StrDencrypt(ByVal str As String) As String

        Dim unscrambled As String = “”

        Dim letters() As Char = str.ToCharArray

        For i As Integer = 0 To letters.Length – 1

            unscrambled = unscrambled + ChrW(AscW(letters(i)) – code)

        Next

        Return unscrambled

    End Function

Check the picture below ,when the runnibg the application

encrpt

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