1

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.

user692942
  • 16,398
  • 7
  • 76
  • 175
Billy Thorton
  • 213
  • 1
  • 4
  • 12
  • Please compare both of your functions with existing RC4 implementations and test vectors. If they differ, try and print out intermediate values for both the existing good implementation and your implementation. Then try to see when they start to differ, and fix your code. Do this until your output matches that of the RC4 cipher. If you do that with both your Python and VBScript code they should become compatible. StackOverflow is for concise questions only; it is not for debugging support. – Maarten Bodewes Feb 27 '19 at 01:32
  • I'm not looking for just debugging support, I'm asking about how to implement RC4 in VBScript. I'm not sure where I went wrong on my VBScript implementation. I feel very confident about my Python implementation. From my own debugging, I can't quite find where I went wrong - the two algorithms appear identical, but the cipher text produced from the VBScript can't be deciphered through using a key like "test" in Python. Can you at least provide some more VBScript debugging tools that could be of assistance? – Billy Thorton Feb 27 '19 at 02:02
  • For crypto code I find myself falling back on good old println or log statements, removing them when they are not required anymore. Often you need to compare with a specific notation and just looking at the variable view or memory view of the program is error prone and usually doesn't cut it. Use hexadecimals or even binary to print out the values. – Maarten Bodewes Feb 27 '19 at 02:10
  • I've tried using Wscript.Echo for most of the VBScript, but I get type mismatches or other errors. What other logging mechanisms? Are you familiar with VBScript? Can you help? – Billy Thorton Feb 27 '19 at 02:56

1 Answers1

4

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

user692942
  • 16,398
  • 7
  • 76
  • 175
  • Thank you!! That is really helpful. Would I replace all the uses of Asc and Chr? Does Mid have any issues? – Billy Thorton Feb 27 '19 at 05:50
  • Yes, as you want Unicode not ANSI character points. You might have an issue with [`Mid()`](https://learn.microsoft.com/en-us/previous-versions//wffts6k3%28v%3dvs.85%29) because Unicode is multiple byte, you could try `MidB()` but it gets tricky to work with binary in VBScript. You might find implementing a Python function that [uses ASCII characters instead of Unicode](https://stackoverflow.com/q/227459/692942) easier than trying to work with Unicode in VBScript. – user692942 Feb 27 '19 at 05:58
  • Good to know! Would you know of any good current ASCII implementation of RC4 in VBScript? – Billy Thorton Feb 27 '19 at 13:58
  • The one in your original question seems fine. – user692942 Feb 27 '19 at 14:23
  • There is also this old [4GuysFromRolla article](http://www.4guysfromrolla.com/webtech/010100-1.shtml). – user692942 Feb 27 '19 at 14:34