//
you're reading...
ASP.NET, IT-Center, VB.NET, Web Based

Simple Encrypt/Decrypt String, ASP.NET/VB

This sample class for Encrypt/Descrypt string :

Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class globalSecurity
Protected key As String = "&/?@*>:>"

Public Sub New()
 'constructor
 End Sub

Public Function encryptString(ByVal strtext As String) As String
 Return Encrypt(strtext, key)
 End Function

Public Function decryptString(ByVal strtext As String) As String
 Return Decrypt(strtext, key)
 End Function

'The function used to encrypt the text
 Private Function Encrypt(ByVal strText As String, ByVal strEncrKey _
 As String) As String
 Dim byKey() As Byte = {}
 Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
Try
 byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8))
Dim des As New DESCryptoServiceProvider()
 Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
 Dim ms As New MemoryStream()
 Dim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
 cs.Write(inputByteArray, 0, inputByteArray.Length)
 cs.FlushFinalBlock()
 Return Convert.ToBase64String(ms.ToArray())
Catch ex As Exception
 Return ex.Message
 End Try
End Function

'The function used to decrypt the text
 Private Function Decrypt(ByVal strText As String, ByVal sDecrKey _
 As String) As String
 Dim byKey() As Byte = {}
 Dim IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}
 Dim inputByteArray(strText.Length) As Byte
Try
 byKey = System.Text.Encoding.UTF8.GetBytes(Left(sDecrKey, 8))
 Dim des As New DESCryptoServiceProvider()
 inputByteArray = Convert.FromBase64String(strText)
 Dim ms As New MemoryStream()
 Dim cs As New CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
 cs.FlushFinalBlock()
 Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray())
Catch ex As Exception
 Return ex.Message
 End Try
End Function
End Class

How to use? please see here :

dim myString as string="test my string"
With New globalSecurity
   myString=    .encryptString(myString)
   myString=    .decryptString(myString)
End With

About berbagisolusi

Berbagi merupakan sebuah bentuk simbol keikhlasan untuk membantu dan menolong, sedangkan solusi adalah cara menyelesaikan masalah. Setiap manusia pasti mengalami masalah, tetapi kita tidak perlu mengalami masalah yang sama jika orang lain pernah mengalami dan kita tahu hal tersebut.

Discussion

No comments yet.

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

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

December 2012
M T W T F S S
« Nov   Sep »
 12
3456789
10111213141516
17181920212223
24252627282930
31  

Archives

Web Statistic

Blog Stats

  • 149,551 hits
%d bloggers like this: