Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Encrypt decrypt string

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

Hope it helps

3 Comments

  • hai , i have error using decryptstring in asp.net . my error is 'Invalid character in a Base-64 string'and how to convert to frombasestring64 to byte.
    please help me.


  • I have a similar error to ramesh. I believe that my error stems from the fact that I am encrypting text, url encoding it and sending it via the query string. On the other end, I am parsing the query string, url decoding it, and then decrypting it.

    It works if I perform this process through a file or database, but not passed via a query string. Any ideas? My only thought is that the encryption of the text yields non-display characters such as linefeed.

  • How do I change this in the HTML work with your code?

Comments have been disabled for this content.