#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Conversion Problem


    I'm pretty much done converting this code from VB6 to Python, the only problem is that VB6 used aryKey() as a String and I use aKey() (same purpose) as a list() function in Python.
    Here is the whole VB6 function:
    Code:
    Public Function RShift(ByVal pnValue As Long, ByVal pnShift As Long) As Double
    On Error Resume Next
    RShift = CDbl(pnValue \ (2 ^ pnShift))
    End Function
    Public Function DecodeStarcraftKey(ByVal sKey As String) As String
    On Error Resume Next
    Dim R As Double, n As Double, n2 As Double, v As Double, _
    v2 As Double, keyvalue As Double, c1 As Byte, c2 As Byte, c As Byte, _
    bValid As Boolean, i As Integer, aryKey(0 To 12) As String
    For i = 1 To 13
        aryKey(i - 1) = Mid$(sKey, i, 1)
    Next i
    v = 3
    For i = 0 To 11
        c = aryKey(i)
        n = Val(c)
        n2 = v * 2
        n = n Xor n2
        v = v + n
    Next i
    v = v Mod 10
    If Hex(v) = aryKey(12) Then
        bValid = True
    End If
    v = 194
    For i = 11 To 0 Step -1
        If v < 7 Then GoTo continue
        c = aryKey(i)
        n = CInt(v / 12)
        n2 = v Mod 12
        v = v - 17
        c2 = aryKey(n2)
        aryKey(i) = c2
        aryKey(n2) = c
    Next i
    continue:
    v2 = &H13AC9741
    For i = 11 To 0 Step -1
        c = UCase$(aryKey(i))
        aryKey(i) = c
        If Asc(c) <= Asc("7") Then
            v = v2
            c2 = v And &HFF
            c2 = c2 And 7
            c2 = c2 Xor c
            v = RShift(CLng(v), 3)
            aryKey(i) = c2
            v2 = v
        ElseIf Asc(c) < 65 Then
            c2 = CByte(i)
            c2 = c2 And 1
            c2 = c2 Xor c
            aryKey(i) = c2
        End If
    Next i
    DecodeStarcraftKey = Join(aryKey, "")
    Erase aryKey()
    End Function
    Here is the function in Python:
    Code:
    def shift(value, shift):
        a = str(value / 2*2*2)
        if '.' in a:
            a = a.replace(a[a.find('.'):], '')
            a = int(a)
            return a
        else:
            return int(a)
    def aKey(d):
         return list(d)
    def remain(a, b):
        c = a * b
        return c - a
    def starkey(key):
        is_valid = [False]
        for i in range(1, 13):
            aKey(i - 1) = key[i:0]
        v = 3
        for i in range(0, 11)
            c = aKey(i)
            n = val(c)
            n2 = v * 2
            n = n ^ n2
            v = v + n
        v = remain(v, 10)
        if hex(v) == aKey(12):
            is_valid[0] = True
        v = 194
        for i in range(11, 0, -1):
            if v < 7:
                v2 = 0x13AC9741
                for i in range(11, 0, -1):
                    c = aKey(i).upper()
                    aKey(i) = c
                    if ord(c) <= ord('7'):
                        v = v2
                        c2 = v and 0xFF
                        c2 = c2 and 7
                        c2 = c2 ^ c
                        v = shift(int(v), 3)
                        aKey(i) = c2
                        v2 = v
                    elif ord(c) < 65:
                        c2 = int(i)
                        c2 = c2 and 1
                        c2 = c2 ^ c
                        aKey(i) = c2
            c = aKey(i)
            n = int(v/12)
            n2 = remain(v, 12)
            v = v - 17
            c2 = aKey(n2)
            aKey(i) = c2
            aKey(n2) = c
        return ''.join(aKey)
    When I do (in Python) ''.join(aKey) I receive:
    Code:
    >>> ''.join(aKey)
    Traceback (most recent call last):
      File "<pyshell#60>", line 1, in ?
        ''.join(aKey)
    TypeError: sequence expected, function found
    If I dump the aKey() function and I just use list(), I receive:
    Code:
    >>> ''.join(list)
    Traceback (most recent call last):
      File "<pyshell#67>", line 1, in ?
        ''.join(list)
    TypeError: sequence expected, type found
    I'm sure there is a better way to fixing this up. Any help is appreciated, whether I messed up on something else in the code or you can help with the main problem, so far.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    can you explain what exactly you're trying to do with that function?
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    This is the decoding process in which Battle.net decodes its Cdkeys, this function is built only for the decoding of Starcraft cdkeys only. There are other functions that were made which are for other game products. This function decodes the key given to the program so that it can properly send it to Battle.net the way the Battle.net receives them from the game client that you are connecting to its server with. It's one of the more complicated tasks done for connecting to a Battle.net product through it's actual game hash files. It's confusing to me too. So I can't really go too much into detail with it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    The specific problem you have is that you have a function named 'aKey', and you never declare aKey as a list in the decoding function, so Python thinks you are trying to join a function into a string somehow;

    (
    Code:
    def aKey(d):
         return list(d)
    
    ...
    
    def blah():
       return ''.join(aKey)
    )


    The general problem you have is VB is ugly

    Code:
    def shift(value, shift):
        a = str(value / 2*2*2)
        if '.' in a:
            a = a.replace(a[a.find('.'):], '')
            a = int(a)
            return a
        else:
            return int(a)
    If that is actually doing bit-shifting, Python has bit-shifting operators built in (>> and <<):

    Code:
    >>> 5 >> 1
    2
    >>> 5 << 1
    10

    Code:
    def remain(a, b):
        c = a * b
        return c - a
    - This isn't calculating the remainder
    - a % b does calculate the remainder

    Code:
    aKey(i - 1) = key[i:0]
    The left half of this is VB;

    Code:
    aKey[i-1] =
    However this whole function
    Code:
    for i in range(1, 13):
            aKey(i - 1) = key[i:0]
    appears to be VB bodgery to make up for the lack of good data types. I think

    Code:
    aKey = list(key)
    would do the same thing...
    Last edited by sfb; April 10th, 2005 at 06:26 PM.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Thanks for the help sfb. As for bit-shifting, I looked around on the modules on the Python site and I found the operator module. It has a function lshift(), and rshift(). In VB6 the program using RShift() and LShift(), I'm pretty sure these are meant to do the same thing. Should i use operator.lshift() and operator.rshift() in my program?
    Code:
    lshift( a, b) 
    
    __lshift__( a, b) 
    
    Return a shifted left by b.
    Code:
    rshift( a, b) 
    
    __rshift__( a, b) 
    
    Return a shifted right by b.
    Update: Ignore this post, it does the same thing.
    Last edited by †Yegg†; April 10th, 2005 at 07:31 PM.

IMN logo majestic logo threadwatch logo seochat tools logo