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.comWeb:
www.snb-bg.com