Imports Microsoft.VisualBasic
Imports System.Security.Cryptography


Public Class CCOMEncryption

    Private privateEncryptionKey As String = ConfigurationManager.AppSettings("TRIPLE_DES_CRYPTO_P")

    Public Sub New()

    End Sub

    Public Function EncryptTripleDESAndEncode(ByVal value As String) As String
        '######################################################################
        ' RODCOPELAND    OCT 2, 2008     ISSUE ID 4710
        '######################################################################
        Dim salt As Byte()
        Try
            Dim des As New TripleDESCryptoServiceProvider
            des.IV = New Byte(7) {}
            salt = createRandomSalt(7)
            Dim pdb As New PasswordDeriveBytes(privateEncryptionKey, salt)
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
            Dim ms As New IO.MemoryStream((value.Length * 2) - 1)
            Dim encStream As New CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)
            Dim plainBytes As Byte() = Text.Encoding.UTF8.GetBytes(value)
            encStream.Write(plainBytes, 0, plainBytes.Length)
            encStream.FlushFinalBlock()
            Dim encryptedBytes(CInt(ms.Length - 1)) As Byte
            ms.Position = 0
            ms.Read(encryptedBytes, 0, CInt(ms.Length))
            encStream.Close()
            Return Convert.ToBase64String(encryptedBytes)
        Catch ex As Exception
            Throw ex
        Finally
            clearBytes(salt) 'Clear the buffers
        End Try
    End Function
    Public Function DecryptTripleDESAndDecode(ByVal value As String) As String
        '######################################################################
        ' RODCOPELAND    OCT 2, 2008     ISSUE ID 4710
        '######################################################################
        Dim salt As Byte()
        Try
            Dim des As New TripleDESCryptoServiceProvider
            des.IV = New Byte(7) {}
            salt = createRandomSalt(7)
            Dim pdb As New PasswordDeriveBytes(privateEncryptionKey, salt)
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, New Byte(7) {})
            Dim encryptedBytes As Byte() = Convert.FromBase64String(value)
            Dim ms As New IO.MemoryStream(value.Length)
            Dim decStream As New CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)
            decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
            decStream.FlushFinalBlock()
            Dim plainBytes(CInt(ms.Length - 1)) As Byte
            ms.Position = 0
            ms.Read(plainBytes, 0, CInt(ms.Length))
            decStream.Close()
            Return Text.Encoding.UTF8.GetString(plainBytes)
        Catch ex As Exception
            Throw ex
        Finally
            clearBytes(salt) 'Clear the buffers
        End Try
    End Function

    Public Function EncryptStringRSA(ByVal value As String) As String
        Return "not completed"
    End Function
    Public Function DecryptStringRSA(ByVal value As String) As String
        Return "not completed"
    End Function

    Public Function EncryptStringRijnDael(ByVal value As String) As String
        Return "not completed"
    End Function
    Public Function DecryptStringRijnDael(ByVal value As String) As String
        Return "not completed"
    End Function


    Private Function createRandomSalt(ByVal Length As Integer) As Byte()
        '######################################################################
        ' RODCOPELAND    OCT 2, 2008     ISSUE ID 4710
        '######################################################################
        'GENERATE RANDOM SALT VALUE OF A SPECIFIED LENGTH
        '##############################################################
        Dim randBytes() As Byte
        If Length >= 1 Then
            randBytes = New Byte(Length) {}
        Else
            randBytes = New Byte(0) {}
        End If
        Dim rand As New RNGCryptoServiceProvider()
        rand.GetBytes(randBytes)
        Return randBytes
    End Function
    Sub clearBytes(ByVal Buffer() As Byte)
        '######################################################################
        ' RODCOPELAND    OCT 2, 2008     ISSUE ID 4710
        '######################################################################
        'CLEAR THE BYTES IN BUFFER SO THEY CANT LATER BE READ FROM MEMORY.
        '#################################################################
        If Buffer Is Nothing Then
            Throw New ArgumentException("Buffer")
        End If
        '####################################
        'SET EACH TO ZERO
        '####################################
        Dim x As Integer
        For x = 0 To Buffer.Length - 1
            Buffer(x) = 0
        Next x

    End Sub

End Class
