Imports System.IO
Imports System.Security.Cryptography
Imports System.Text

Public Class cCryptography

    'Dim EncryptionKey As String = "$kldfKFSAK37236780!!*+++hHUDO723BNU!$hask+*jhds7!2929j$+jP*!hWrT$kldfKFSAK37236780!!*+++hHUDO723BNU!$hask+*jhds7!2929j$+jP*!hWrT"
    Shared EncryptionKey As String = "$kldfKFSAK37236780!!*+++hHUDO723BNU!$hask+*jhds7!2929j$+jP*!hWrT$kldfKFSAK37236780!!*+++hHUDO723BNU!$hask+*jhds7!2929j$+jP*!hWrT"

    Public Shared Function Encrypt(clearText As String) As String
        Dim clearBytes As Byte() = Encoding.Unicode.GetBytes(clearText)
        Using encryptor As Aes = Aes.Create()
            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)
            Using ms As New MemoryStream()
                Using cs As New CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write)
                    cs.Write(clearBytes, 0, clearBytes.Length)
                    cs.Close()
                End Using
                clearText = Convert.ToBase64String(ms.ToArray())
            End Using
        End Using
        Return clearText
    End Function
    Public Shared Function Decrypt(cipherText As String) As String
        Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
        Using encryptor As Aes = Aes.Create()
            Dim pdb As New Rfc2898DeriveBytes(EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, &H76})
            encryptor.Key = pdb.GetBytes(32)
            encryptor.IV = pdb.GetBytes(16)
            Using ms As New MemoryStream()
                Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)
                    cs.Write(cipherBytes, 0, cipherBytes.Length)
                    cs.Close()
                End Using
                cipherText = Encoding.Unicode.GetString(ms.ToArray())
            End Using
        End Using
        Return cipherText
    End Function




End Class

Public Class cCryptography2
    Shared _key As String = "!#$a54?3"
    Public Shared Function Encrypt(ByVal strQueryString As String) As String
        Dim oES As New Encryption64()
        Return oES.Encrypt(strQueryString, _key)
    End Function

    Public Shared Function Decrypt(ByVal strQueryString As String) As String
        Dim oES As New Encryption64()
        Return oES.Decrypt(strQueryString, _key)
    End Function

End Class


Public Class Encryption64
    Private key() As Byte = {}
    Private IV() As Byte = {&H12, &H34, &H56, &H78, &H90, &HAB, &HCD, &HEF}

    Public Function Decrypt(ByVal stringToDecrypt As String,
        ByVal sEncryptionKey As String) As String
        Dim inputByteArray(stringToDecrypt.Length) As Byte
        Try
            key = System.Text.Encoding.UTF8.GetBytes(Left(sEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider()
            inputByteArray = Convert.FromBase64String(stringToDecrypt)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateDecryptor(key, 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 e As Exception
            Return e.Message
        End Try
    End Function

    Public Function Encrypt(ByVal stringToEncrypt As String,
        ByVal SEncryptionKey As String) As String
        Try
            key = System.Text.Encoding.UTF8.GetBytes(Left(SEncryptionKey, 8))
            Dim des As New DESCryptoServiceProvider()
            Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(
                stringToEncrypt)
            Dim ms As New MemoryStream()
            Dim cs As New CryptoStream(ms, des.CreateEncryptor(key, IV),
                CryptoStreamMode.Write)
            cs.Write(inputByteArray, 0, inputByteArray.Length)
            cs.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray())
        Catch e As Exception
            Return e.Message
        End Try
    End Function

End Class


Public Class cCryptography3
    Shared _key As String = "!#?"


    'Public Shared Function Encrypt(ByVal strQueryString As String) As String

    '    Using hasher As MD5 = MD5.Create()    ' create hash object

    '        ' Convert to byte array and get hash
    '        Dim dbytes As Byte() =
    '             hasher.ComputeHash(Encoding.UTF8.GetBytes(strQueryString & _key))

    '        ' sb to create string from bytes
    '        Dim sBuilder As New StringBuilder()

    '        ' convert byte data to hex string
    '        For n As Integer = 0 To dbytes.Length - 1
    '            sBuilder.Append(dbytes(n).ToString("X2"))
    '        Next n

    '        Return sBuilder.ToString()
    '    End Using

    'End Function

    Public Shared Function Encrypt(ByVal input As String) As String
        Dim stringBytes As Byte() = System.Text.Encoding.Unicode.GetBytes(input & _key)
        Dim sbBytes As StringBuilder = New StringBuilder(stringBytes.Length * 2)

        For Each b As Byte In stringBytes
            sbBytes.AppendFormat("{0:X2}", b)
        Next

        Return sbBytes.ToString()
    End Function


    Public Shared Function Decrypt(ByVal hexInput As String) As String
        Dim numberChars As Integer = hexInput.Length
        Dim bytes As Byte() = New Byte(numberChars / 2 - 1) {}

        For i As Integer = 0 To numberChars - 1 Step 2
            bytes(i / 2) = Convert.ToByte(hexInput.Substring(i, 2), 16)
        Next

        Return System.Text.Encoding.Unicode.GetString(bytes).Replace(_key, "")
    End Function
End Class

