'Imports Microsoft.VisualBasic
'Imports System
'Imports System.IO
'Imports System.Xml
'Imports System.Text
'Imports System.Security.Cryptography
'Public Module 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
'    Public Function encryptstr(ByVal strencrypt As String) As String
'        'dim oES = New ExtractAndSerialize.Encryption64())
'        Return Encrypt(strencrypt, "!#$a54?3")
'    End Function
'    Public Function decryptstr(ByVal strencrypt As String) As String
'        'dim oES = New ExtractAndSerialize.Encryption64())
'        Return Decrypt(strencrypt, "!#$a54?3")
'    End Function
'End Module
Imports System
Imports System.Web
Imports System.Web.Caching
Imports System.Web.UI
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Mail
Imports System.Math
Imports System.IO
Imports System.EnterpriseServices
Imports System.Drawing
'Imports System.Web.HttpWriter
Imports System.Web.Security
Imports System.Resources.Tools
Imports System.Security.Cryptography
Imports System.Configuration
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Data.Sql
Imports System.Text
Public Module Encryption
    Public plainText As String
    Public cipherText As String
    Public passPhrase As String = "at@c>0!n^h$n"        ' can be any string
    Public saltValue As String = "n^h$n!0<c@ta"
    Public hashAlgorithm As String = "SHA1"
    Public passwordIterations As Integer = 2
    Public initVector As String = "@1B2c3D4e5F6g7H8" ' must be 16 bytes
    Public keySize As Integer = 256
    Public Function Encrypt(ByVal plainText As String) As String
        Dim initVectorBytes As Byte()
        initVectorBytes = System.Text.Encoding.ASCII.GetBytes(initVector)

        Dim saltValueBytes As Byte()
        saltValueBytes = System.Text.Encoding.ASCII.GetBytes(saltValue)

        Dim plainTextBytes As Byte()
        plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText)

        Dim password As PasswordDeriveBytes
        password = New PasswordDeriveBytes(passPhrase, _
                                           saltValueBytes, _
                                           hashAlgorithm, _
                                           passwordIterations)


        Dim keyBytes As Byte()
        keyBytes = password.GetBytes(keySize / 8)

        ' Create uninitialized Rijndael encryption object.
        Dim symmetricKey As RijndaelManaged
        symmetricKey = New RijndaelManaged()
        symmetricKey.Mode = CipherMode.CBC
        Dim encryptor As ICryptoTransform
        encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes)

        ' Define memory stream which will be used to hold encrypted data.
        Dim memoryStream As MemoryStream
        memoryStream = New MemoryStream()

        ' Define cryptographic stream (always use Write mode for encryption).
        Dim cryptoStream As CryptoStream
        cryptoStream = New CryptoStream(memoryStream, _
                                        encryptor, _
                                        CryptoStreamMode.Write)
        ' Start encrypting.
        cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length)

        ' Finish encrypting.
        cryptoStream.FlushFinalBlock()

        ' Convert our encrypted data from a memory stream into a byte array.
        Dim cipherTextBytes As Byte()
        cipherTextBytes = memoryStream.ToArray()

        ' Close both streams.
        memoryStream.Close()
        cryptoStream.Close()

        ' Convert encrypted data into a base64-encoded string.
        Dim cipherText As String
        cipherText = Convert.ToBase64String(cipherTextBytes)

        ' Return encrypted string.
        Encrypt = cipherText
        Return Encrypt
    End Function
    Public Function Decrypt(ByVal cipherText As String) As String
        ' Convert strings defining encryption key characteristics into byte
        ' arrays. Let us assume that strings only contain ASCII codes.
        ' If strings include Unicode characters, use Unicode, UTF7, or UTF8
        ' encoding.
        Dim initVectorBytes As Byte()
        initVectorBytes = Encoding.ASCII.GetBytes(initVector)

        Dim saltValueBytes As Byte()
        saltValueBytes = Encoding.ASCII.GetBytes(saltValue)

        ' Convert our ciphertext into a byte array.
        Dim cipherTextBytes As Byte()
        cipherTextBytes = Convert.FromBase64String(cipherText)

        ' First, we must create a password, from which the key will be 
        ' derived. This password will be generated from the specified 
        ' passphrase and salt value. The password will be created using
        ' the specified hash algorithm. Password creation can be done in
        ' several iterations.
        Dim password As PasswordDeriveBytes
        password = New PasswordDeriveBytes(passPhrase, _
                                           saltValueBytes, _
                                           hashAlgorithm, _
                                           passwordIterations)

        ' Use the password to generate pseudo-random bytes for the encryption
        ' key. Specify the size of the key in bytes (instead of bits).
        Dim keyBytes As Byte()
        keyBytes = password.GetBytes(keySize / 8)

        ' Create uninitialized Rijndael encryption object.
        Dim symmetricKey As RijndaelManaged
        symmetricKey = New RijndaelManaged()

        ' It is reasonable to set encryption mode to Cipher Block Chaining
        ' (CBC). Use default options for other symmetric key parameters.
        symmetricKey.Mode = CipherMode.CBC

        ' Generate decryptor from the existing key bytes and initialization 
        ' vector. Key size will be defined based on the number of the key 
        ' bytes.
        Dim decryptor As ICryptoTransform
        decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes)

        ' Define memory stream which will be used to hold encrypted data.
        Dim memoryStream As MemoryStream
        memoryStream = New MemoryStream(cipherTextBytes)

        ' Define memory stream which will be used to hold encrypted data.
        Dim cryptoStream As CryptoStream
        cryptoStream = New CryptoStream(memoryStream, _
                                        decryptor, _
                                        CryptoStreamMode.Read)

        ' Since at this point we don't know what the size of decrypted data
        ' will be, allocate the buffer long enough to hold ciphertext;
        ' plaintext is never longer than ciphertext.
        Dim plainTextBytes As Byte()
        ReDim plainTextBytes(cipherTextBytes.Length)

        ' Start decrypting.
        Dim decryptedByteCount As Integer
        decryptedByteCount = cryptoStream.Read(plainTextBytes, _
                                               0, _
                                               plainTextBytes.Length)

        ' Close both streams.
        memoryStream.Close()
        cryptoStream.Close()

        ' Convert decrypted data into a string. 
        ' Let us assume that the original plaintext string was UTF8-encoded.
        Dim plainText As String
        plainText = Encoding.UTF8.GetString(plainTextBytes, _
                                            0, _
                                            decryptedByteCount)

        ' Return decrypted string.
        Decrypt = plainText
        Return Decrypt
    End Function
End Module


