Generating Token for AutoLogin

Utilize the following VB.NET code to generate a token for AutoLogin:

VendorPassword and VendorBlock are stored in the SSO Vendor table.

Imports System.Configuration

Imports System.IO

Imports System.Security.Cryptography

Imports System.Text

Public Shared Function GetVendorToken(ByVal returnUrl As String, ByVal vendorPassword As String, ByVal vendorBlock As String, ByVal User As String, ByVal Password As String) As String

        Return Encrypt(GetTimeStamp() & "|" & returnUrl & "|" & User & "|" & Password, vendorPassword, vendorBlock)

    End Function

 

  Private Shared Function Encrypt(ByVal text As String, ByVal password As String, ByVal block As String) As String

        Dim provider As SymmetricAlgorithm = Nothing

        Dim buffer As MemoryStream = Nothing

        Dim writer As CryptoStream = Nothing

        Try

            provider = New RijndaelManaged

            buffer = New  MemoryStream(text.Length)

            writer = New  CryptoStream( _

             buffer, _

             provider.CreateEncryptor(FromHexaDecimal(password), FromHexaDecimal(block)), _

             CryptoStreamMode.Write)

            Dim encoding As  ASCIIEncoding = New ASCIIEncoding

            Dim bytes() As  Byte = encoding.GetBytes(text)

            writer.Write(bytes, 0, bytes.Length)

            writer.Close()

            Return ToHexaDecimal(buffer.ToArray())

        Finally

            provider.Clear()

            buffer.Close()

            '    writer.Close()

        End Try

    End Function

 

 Private Shared Function ToHexaDecimal(ByVal bytes() As Byte) As String

        If bytes Is Nothing Then

            Return ""

        End If

        Dim buffer As  StringBuilder = New StringBuilder

        Dim length As Integer = bytes.Length

        For n As Integer = 0 To length - 1

            buffer.Append(String.Format("{0,2:x}", bytes(n)).Replace(" ", "0"))

        Next

        Return buffer.ToString()

    End Function

 

Public Shared Function Decrypt(ByVal encrypted As String, ByVal password As String, ByVal block As String) As String

        Dim provider As  SymmetricAlgorithm = Nothing

        Dim buffer As  MemoryStream = Nothing

        Dim reader As  CryptoStream = Nothing

        Try

            provider = New RijndaelManaged

            Dim bytes() As Byte = FromHexaDecimal(encrypted)

            buffer = New  MemoryStream(bytes.Length)

            reader = New  CryptoStream( _

             buffer, _

             provider.CreateDecryptor(FromHexaDecimal(password), FromHexaDecimal(block)), _

             CryptoStreamMode.Read)

            buffer.Write(bytes, 0, bytes.Length)

            buffer.Position = 0

            Dim decrypted As String = New  StreamReader(reader).ReadToEnd()

            reader.Close()

            Return decrypted

        Finally

            provider.Clear()

            buffer.Close()

            '    writer.Close()

        End Try

    End Function

 

  Private Shared Function FromHexaDecimal(ByVal hexadecimal As String) As Byte()

        If hexadecimal Is Nothing OrElse hexadecimal.Length = 0 Then

            Return New Byte() {}

        End If

        Dim hasOddLength As  Boolean = (hexadecimal.Length And 1) = 1

        If hasOddLength Then

            Throw New  Exception("The hexadecimal string must have an even length (2 characters per byte).")

        End If

        Dim length As  Integer = hexadecimal.Length

        Dim bytes() As  Byte = New  Byte(CType(length / 2, Integer) - 1) {}

        For n As  Integer = 0 To length - 2 Step 2

            Dim hexValue As  String = hexadecimal.Substring(n, 2)

            bytes(CType(n / 2, Integer)) = Convert.ToByte(hexValue, 16)

        Next

        Return bytes

    End Function

 

 Private Shared Function GetTimeStamp() As String

        Dim buffer As StringBuilder = New StringBuilder

        Dim now As  DateTime = DateTime.Now

        buffer.Append(now.Year)

        buffer.Append(GetAsTwoDigits(now.Month))

        buffer.Append(GetAsTwoDigits(now.Day))

        buffer.Append(GetAsTwoDigits(now.Hour))

        buffer.Append(GetAsTwoDigits(now.Minute))

        buffer.Append(GetAsTwoDigits(now.Second))

        buffer.Append(GetAsThreeDigits(now.Millisecond))

        Return buffer.ToString()

    End Function

 

  Private Shared Function GetAsTwoDigits(ByVal number As Integer) As String

        Return String.Format("{0,2:d}", number).Replace(" ", "0")

    End Function

 

Private Shared Function GetAsThreeDigits(ByVal number As Integer) As String

        Return String.Format("{0,3:d}", number).Replace(" ", "0")

    End Function