Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27514

encode string help

$
0
0
Hi All,

A while back a member helped me generate keys by using the date (many thanx) code below for encoding
Code:

  Private Function GenerateKey(ExpireDate As Date) As String
        Dim KeySegments(4) As String
        KeySegments(0) = ExpireDate.ToString("yyyy").Substring(0, 2)
        KeySegments(1) = ExpireDate.ToString("yy")
        KeySegments(2) = ExpireDate.ToString("MM")
        KeySegments(3) = ExpireDate.ToString("dd")

        Dim Ue As New UnicodeEncoding()

        Dim bsT1() As Byte = Ue.GetBytes(KeySegments(0))
        Dim bsT2() As Byte = Ue.GetBytes(KeySegments(1))
        Dim bsT3() As Byte = Ue.GetBytes(KeySegments(2))
        Dim bsT4() As Byte = Ue.GetBytes(KeySegments(3))

        Dim retValue As String = (bsT1(0) + 17).ToString("00") & Convert.ToChar(bsT1(2) + 17)
        retValue &= (bsT2(0) + 17).ToString("0-0") & Convert.ToChar(bsT2(2) + 17)
        retValue &= (bsT3(0) + 17).ToString("00-") & Convert.ToChar(bsT3(2) + 17)
        retValue &= (bsT4(0) + 17).ToString("00") & Convert.ToChar(bsT4(2) + 17)

        Return retValue

    End Function

and then to decode
Code:

Private Function DegenerateKey(inKey As String) As Date
        Dim key As String = inKey.Replace("-", "")
        Dim year As String = key.Substring(0, 6)
        Dim month As String = key.Substring(6, 3)
        Dim day As String = key.Substring(9, 3)

        Dim newDate As String = String.Empty

        'Extract the year
        newDate &= Convert.ToChar(Convert.ToInt16(year.Substring(0, 2)) - 17)
        newDate &= Convert.ToChar(Encoding.ASCII.GetBytes(year.Substring(2, 1))(0) - 17)
        newDate &= Convert.ToChar(Convert.ToInt16(year.Substring(3, 2)) - 17)
        newDate &= Convert.ToChar(Encoding.ASCII.GetBytes(year.Substring(5, 1))(0) - 17)

        newDate &= "-"

        'Extract the month
        newDate &= Convert.ToChar(Convert.ToInt16(month.Substring(0, 2)) - 17)
        newDate &= Convert.ToChar(Encoding.ASCII.GetBytes(month.Substring(2, 1))(0) - 17)

        newDate &= "-"

        'Extract the day
        newDate &= Convert.ToChar(Convert.ToInt16(day.Substring(0, 2)) - 17)
        newDate &= Convert.ToChar(Encoding.ASCII.GetBytes(day.Substring(2, 1))(0) - 17)

        Return Convert.ToDateTime(newDate)

    End Function

What I wish to do is add a set of 1's and 0's as a string into this to make it look like it belongs.

for example i wish to have todays date and the following string enocoded: 03/04/2013 11001111

the length of the ones and zeros will always be 8 (at this stage) and I wish to use it along with the key to say weather or not different options are turned on.

the key with date currently tells the program it can still run, but I have 8 different options that will be turned on or off based on a 1 or 0

any help is much appriciated.

Viewing all articles
Browse latest Browse all 27514

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>