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

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

    List Index Problems


    I'm still trying to translate some VB6 code to Python. However the way VB6 can read lists, I cannot properly do the same thing in Python. Here is the Python code I've gotten so far.
    Code:
    def starkey(self, key):
        a_list = list()
        is_valid = [False]
        for i in range(1, 14):
            arrayKey[i - 1] = key[i:0]
        v = 3
        for i in range(0, 12):
            c = arrayKey.append(i)
            n = val(c)
            n2 = v * 2
            n = n ^ n2
            v = v + n
        v = v % 10
        if hex(v) == arrayKey.append(12):
            is_valid[0] = True
        v = 194
        for i in range(11, 0, -1):
            if v < 7:
                v2 = 0x13AC9741
                for i in range(11, 1, -1):
                    c = arrayKey.append(i.upper())
                    arrayKey[i] = c
                    if ord(c) <= ord('7'):
                        v = v2
                        c2 = v and 0xFF
                        c2 = c2 and 7
                        c2 = c2 ^ c
                        v = int(v) >> 3
                        arrayKey[i] = c2
                        v2 = v
                    elif ord(c) < 65:
                        c2 = int(i)
                        c2 = c2 and 1
                        c2 = c2 ^ c
                        arrayKey[i] = c2
            c = arrayKey.append(i)
            n = int(v/12)
            n2 = v % 12
            v = v - 17
            c2 = arrayKey.append(n2)
            arrayKey[i] = c2
            arrayKey[n2] = c
    RIght now the arrayKey value is wrong. It should be something like:
    Code:
    a_list.append(item)
    While a_list = list().
    The problem is that I can't do a lot of this code without errors. Where the function has:
    Code:
        for i in range(1, 14):
            arrayKey[i - 1] = key[i:0]
    I tried doing:
    Code:
    >>> a_list = list()
    >>> a_list.append(5)
    >>> a_list[5 - 1] = key[5:0]
    Traceback (most recent call last):
      File "<pyshell#54>", line 1, in ?
        a_list[5 - 1] = key[5:0]
    IndexError: list assignment index out of range
    >>>
    I used 5 as i in range(1, 14) just for testing. The key is 13 numbers, but they are in a string, for instance, key = '1234567890123'. Does anyone know an alternative solution to this list problem? Just incase you need the VB6 code, here it is:
    Code:
    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
    Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    It would help if you explained what the code is supposed to - it is not immediately obvious, and most people (well, me at least) will not have the time or inclination to read through reams of code to puzzle it out.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    http://forums.devshed.com/showpost.p...89&postcount=4


    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by DevCoach
    It would help if you explained what the code is supposed to - it is not immediately obvious, and most people (well, me at least) will not have the time or inclination to read through reams of code to puzzle it out.

    Dave - The Developers' Coach
    "This is the decoding process in which Battle.net decodes its Cdkeys, this function is built only for the decoding of Starcraft cdkeys only"
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Yes, I know this. I didn't say what the code does because I don't believe it is necessary. Also, there isn't exactly a lot of code. The VB6 code does not have to be read, it is only there is someone who plans on helping me wants to look at it. The rest of the code besides the whole function, is very small, it is just what I used to have, and what I tried doing that failed. All I need to do is figure out how to properly use a list in Python that would do the same thing it is doing in the VB6 function. The function is a starcraft cdkey decoder. It is needed to successfully connect to the Battle.net server on Starcraft.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    My post was a bit delayed, I didn't realize you guys posted while I was typing. Also, I did what you showed me sfb, and that received an error. Parts of the fucntion have to be set up differently. I'll work on that.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    from my very very limited knowlege of vb and how it uses arrays. You can use a python dictionary to kinda act the same. However i really don't see why you can't just convert how it does aways to how python does lists.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    I don't know anything about VB (or starcraft ), so it is hard for me to translate the code to python, but here is a try:
    Code:
    def starkey(key):
        arrayKey = list(key)
        is_valid = False
        v = 3
        for i in range(12):
            c = arrayKey[i]
            n = ord(c)
            n2 = v*2
            n = n ^ n2
            v += n
        v = v % 10
        if hex(v) == arrayKey[12]:
            is_Valid = True
        v = 194
        for i in range(11,0,-1):
            if v > 7:
                c = arrayKey[i]
                n = v/12
                n2 = v % 12
                v -= 17
                c2 = arrayKey[n2]
                arrayKey[i] = c2
                arrayKey[n2] = c
            else:
                # here comes the continue stuff
                # I don't really know what &H13... is
        decode_starcraft_key = ''.join(arrayKey)
        print decode_starcraft_key
    Regards, mawe
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I have the conintue part already complete. However, it was the arrayKey thing that was messing me up. Thanks mawe, I'll try out that code and see how it works.
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Thanks for all the help guys, I fixed my function and it sent back some data now. I'll find out if the data is correct or not by going to another forums (Battle.net based programming). Here is the function, so far.
    Code:
    def starkey(self, key):
        arrayKey = list(key)
        is_valid = [False]
        v = 3
        for i in range(12):
            c = arrayKey[i]
            n = int(c)
            n2 = v * 2
            n = n ^ n2
            v = v + n
        v = v % 10
        if hex(v) == arrayKey[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 = arrayKey[i]
                    if ord(c) <= ord('7'):
                        v = v2
                        c2 = v and 0xFF
                        c2 = c2 and 7
                        c2 = c2 ^ c
                        v = int(v) >> 3
                        arrayKey[i] = c2
                        v2 = v
                    elif ord(c) < 65:
                        c2 = int(i)
                        c2 = c2 and 1
                        c2 = c2 ^ c
                        arrayKey[i] = c2
            c = arrayKey[i]
            n = int(v/12)
            n2 = v % 12
            v = v - 17
            c2 = arrayKey[n2]
            arrayKey[i] = c2
            arrayKey[n2] = c
            decode_starcraft_key = ''.join(arrayKey)
            return decode_starcraft_key
    Update: The data I've received is incorrect, here is my new code:
    Code:
    >>> def starkey(key):
        arrayKey = list(key)
        is_valid = [False]
        v = 3
        for i in range(12):
            c = arrayKey[i]
            n = int(c)
            n2 = v * 2
            n ^= n2
            v += n
        v = v % 10
        if hex(v) == arrayKey[12]:
            is_valid[0] = True
        v = 194
        for i in range(11, 0, -1):
            if v < 7:
                v2 = 0x13AC9741
                for i in range(11, 1, -1):
                    c = arrayKey[i]
                    if ord(c) <= ord('7'):
                        v = v2
                        c2 = v & 0xFF
                        c2 &= 7
                        c2 ^= c
                        v = int(v) >> 3
                        arrayKey[i] = c2
                        v2 = v
                    elif ord(c) < 65:
                        c2 = int(i)
                        c2 &= 1
                        c2 ^= c
                        arrayKey[i] = c2
            c = arrayKey[i]
            n = int(v/12)
            n2 = v % 12
            v -= 17
            c2 = arrayKey[n2]
            arrayKey[i] = c2
            arrayKey[n2] = c
            decode_starcraft_key = ''.join(arrayKey)
            return decode_starcraft_key
    
    >>> starkey('1234567890123')
    '1224567890133'
    I was wondering if doing arrayKey = list(key) was wrong. Could this be the problem? Someone else ran this same function but with VB6 code, they received 5263531935083.
    Last edited by †Yegg†; April 17th, 2005 at 03:22 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    11
    have you guys looked at this project

    vb2py
  22. #12
  23. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Wow! That program is great. Thanks a lot wx uab. I'll see how good it translates for my function.
    Update: I tested it and it isn't all that great. It's good for small simple code. But that function is far too complicated for it.
    Last edited by †Yegg†; April 17th, 2005 at 03:58 PM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Originally Posted by †Yegg†
    Update: The data I've received is incorrect, here is my new code:
    Code:
    >>> def starkey(key):
        arrayKey = list(key)
        is_valid = [False]
        v = 3
        for i in range(12):
            c = arrayKey[i]
            n = int(c)
            n2 = v * 2
            n ^= n2
            v += n
        v = v % 10
        if hex(v) == arrayKey[12]:
            is_valid[0] = True
        v = 194
        for i in range(11, 0, -1):
            if v < 7:
                v2 = 0x13AC9741
                for i in range(11, 1, -1):
                    c = arrayKey[i]
                    if ord(c) <= ord('7'):
                        v = v2
                        c2 = v & 0xFF
                        c2 &= 7
                        c2 ^= c
                        v = int(v) >> 3
                        arrayKey[i] = c2
                        v2 = v
                    elif ord(c) < 65:
                        c2 = int(i)
                        c2 &= 1
                        c2 ^= c
                        arrayKey[i] = c2
            c = arrayKey[i]
            n = int(v/12)
            n2 = v % 12
            v -= 17
            c2 = arrayKey[n2]
            arrayKey[i] = c2
            arrayKey[n2] = c
            decode_starcraft_key = ''.join(arrayKey)
            return decode_starcraft_key
    
    >>> starkey('1234567890123')
    '1224567890133'
    I was wondering if doing arrayKey = list(key) was wrong. Could this be the problem?
    Perhaps, but, your code has:

    Code:
            if v < 7:
                v2 = 0x13AC9741
                ...
    where the original VB has

    Code:
            if v < 7 then goto continue
            v2 = 0x13AC9741
            ...
    Which is one problem.

    Someone else ran this same function but with VB6 code, they received 5263531935083.
    When I do the minimum changes to get it to run as VBScript, I get 0647355912683 out of it.

    I also have a Python version which gives 0647355912683:

    Code:
    def deKey(sKey):
        aryKey = list(sKey)
        shortKey, checksum = aryKey[:12], aryKey[12]
    
        v = 3
        for c in shortKey:
            n = int(c)
            v += n ^ (v * 2)
            
        v %= 10
        if v == checksum:
            print "Checksum match"
    
        
        # Next calculations
        v = 194
        for i in reversed(range(12)):
            if v < 7: break
            
            n, n2 = divmod(v, 12) 
            v -= 17
            
            aryKey[i], aryKey[n2] = aryKey[n2], aryKey[i]
    
    
        # Next calculations
        v2 = 0x13ac9741
        for i in reversed(range(12)):
            c = aryKey[i]
            
            if ord(c) <= 55:
                c2 = (v2 and 0xff) and 7
                c2 ^= int(c)
    
                aryKey[i] = c2
                v2 >>= 3
                
            elif ord(c) < 65:
                c2 = (i and 0xff) and 1
                c2 ^= int(c)
                aryKey[i] = c2
    
        return ''.join([str(x) for x in aryKey])
    
    print deKey('1234567890123')
    Whether that is actually the right output, I don't know, but the two do agree, which is as good as any result I have yet from this.
    Last edited by sfb; April 17th, 2005 at 09:21 PM.
  26. #14
  27. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Thanks for the help everyone. Someone in another forums (different site) gave me the correct code, a few of the numbers in my range() function were off by one. Anyways, here is the new code and it returns the correct key.
    Code:
    def starkey(key):
        arrayKey = list(key)
        v = 3
        for i in range(0, 12):
            c = arrayKey[i]
            n = int(c)
            n2 = v * 2
            n ^= n2
            v += n
        v %= 10
        if(hex(v) == arrayKey[12]):
            valid = [True]
        v = 194
        for i in range(11, -1, -1):
            if(v < 7):
                break
            c = arrayKey[i]
            n = int(v/12)
            n2 = v % 12
            v -= 17
            c2 = arrayKey[n2]
            arrayKey[i] = c2
            arrayKey[n2] = c
        v2 = 0x13AC9741
        for i in range(11, -1, -1):
            c = arrayKey[i].upper()
            arrayKey[i] = c
            if(ord(c) <= ord('7')):
                v = v2
                c2 = v & 0xFF
                c2 &= 7
                c2 ^= int(c)
                v = int(v) >> 3
                arrayKey[i] = c2
                v2 = v
            elif(ord(c) < 65):
                c2 = int(i)
                c2 &= 1
                c2 ^= int(c)
                arrayKey[i] = c2
        decode_starcraft_key = ''.join(map(str, arrayKey))
        return decode_starcraft_key

IMN logo majestic logo threadwatch logo seochat tools logo