SNB Solutions - Baja California Software Company
Welcome Guest Search | Active Topics | Log In | Register

Tag as favorite
Encrypt decrypt string
stoian_bycovich
#1 Posted : Friday, February 26, 2010 2:48:11 PM

Rank: Administration

Medals:

Groups: Administrators
Joined: 1/2/2010
Posts: 79
Points: 10,237
Location: Cabo San Lucas

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
There is a class to encrypt ot decrypt strings

Imports System.IO

Imports System.Text.Encoding

Imports System.Security.Cryptography



Public Class Crypto

Private Shared PKey As String = "My key string goes here"

Private Shared IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}



Private Sub New()
End Sub



Public Shared Function EncryptString(ByVal StringToEncrypt As String) As String

Return Encrypt(StringToEncrypt, PKey)
End Function



Public Shared Function DecryptString(ByVal StringToDecrypt As String) As StringReturn Decrypt(StringToDecrypt, PKey)
End Function



Private Shared 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 = UTF8.GetBytes(strText)

Dim ms As New MemoryStreamDim cs As New CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)

cs.FlushFinalBlock()

cs.Close()

cs = Nothing

des.Clear()

des = Nothing

Dim arrX() As Byte = ms.ToArray
ms.Close()

ms = Nothing

System.GC.Collect()

Return System.Convert.ToBase64String(arrX) Catch ex As Exception
Return "Encryption failed."

End Try

End Function




Private Shared 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 = System.Convert.FromBase64String(strText)

Dim ms As New MemoryStreamDim 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()
cs.Close()

cs = Nothing

des.Clear()

des = Nothing

Dim arrX() As Byte = ms.ToArray
ms.Close()

ms = Nothing

System.GC.Collect()

Return encoding.GetString(arrX) Catch ex As Exception
Return "Decryption failed."

End Try

End Function

End Class

Best regards,
Stoyan Bukovich
Executive manager
SNB Solutions

Cell.: +52 6241 616969
E-mail: s.bucovich@snb-bg.com
Web: www.snb-bg.com
Sponsor  
 
Eannouncements.net SNB Solutions Eanuncios.net

Eannouncements.net the place for your announcement.

SNB Solutions - Software and computing services company.

Eanuncios.net el lugar para su anuncio.

Users browsing this topic
Guest
Tag as favorite
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.