Billy Thorton
Billy Thorton

Reputation: 223

RC4 Using VBScript and Python

I've been trying to learn VBScript, and gave myself a bit of a challenge. I wanted to create a function in VBScript that encrypts something in RC4, and decrypts in Python (essentially the same process/algorithm, but I wanted to verify that it was correct).

For my python code, I have the following:

def rc4crypt(data, key):
    x = 0
    box = range(256)
    for i in range(256):
        x = (x + box[i] + ord(key[i % len(key)])) % 256
        box[i], box[x] = box[x], box[i]
    x = 0
    y = 0
    out = []
    for char in data:
        x = (x + 1) % 256
        y = (y + box[x]) % 256
        box[x], box[y] = box[y], box[x]
        out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256]))

    return ''.join(out)

From what I can tell from my internet research, this is a pretty standard way to implement RC4. However, when it comes to the VBScript, I've been having a bit of a hard time.

Here is my code so far:

Function RunRC4(sMessage, strKey)
    Dim kLen, x, y, i, j, temp, l
    Dim s(256), k(256)

    'Init keystream
    kLen = Len(strKey)
    For i = 0 To 255
        s(i) = i
        l = Mid(strKey, (i Mod kLen) + 1, 1)
        k(i) = Asc(Mid(strKey, (i Mod kLen) + 1, 1))

    Next

    j = 0
    For i = 0 To 255
        j = (j + k(i) + s(i)) Mod 256
        temp = s(i)
        s(i) = s(j)
        s(j) = temp
    Next

    'Encode/Decode
    For i = 1 To Len(sMessage)
        x = (x + 1) Mod 256
        y = (y + s(x)) Mod 256
        temp = s(x)
        s(x) = s(y)
        s(y) = temp

        temp1 = Asc(Mid(sMessage, i, 1))
        temp2 = Chr(s((s(x) + s(y)) Mod 256))
        RunRC4 = RunRC4 & Chr(temp1 Xor temp2)
    Next
End Function

It's very similar to a lot of the other posts out there. There are a couple of related posts that I've found with folks asking similar questions, but not quite the answer I'm looking for:

Encrypt a string in java and decryption in VBScript using RC4

RC4 decryption with key in Python

As you might be able to tell, both of those use a pretty similar script. But neither seem to work properly when trying to decode. I've also looked other places, including an algorithm like:

https://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript

Would anyone be able to assist? It would be good to know if you were able to successfully run each function. Ideally the goal would be to run an encryption with VBScript, take the output and decrypt with Python, and get the expected original result.

Upvotes: 1

Views: 1004

Answers (1)

user692942
user692942

Reputation: 16681

The problem is going to be encoding. Just looking at the Python documentation the chr() function

Returns a character (a string) from an integer (represents unicode code point of the character)

whereas in VBScript the Chr() function

Returns the character associated with the specified ANSI character code

which isn't equivalent, instead you need to use ChrW() function in VBScript.

ChrW is provided for 32-bit platforms that use Unicode characters. Its argument is a Unicode (wide) character code, thereby avoiding the conversion from ANSI to Unicode.

Also, when returning the character code you will need to use the AscW() function instead of the Asc() function which is equivalent to the Python ord() function.

AscW is provided for 32-bit platforms that use Unicode characters. It returns the Unicode (wide) character code, thereby avoiding the conversion from Unicode to ANSI.


Useful Links

Upvotes: 4

Related Questions