1

I pulled this code from here for asp http://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript, which i then run thru base64.

I am wonder if anyone can help me figure out how to write the decryption piece but in Python. As the decryption will happen on my Python Server Page.

Found this http://www.id-snippet.com/20801/python-rc4-cipher/, but it doesn't decrypt the RC4 asp from the first link.

-Jim

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ASP page

'Base64
Function Base64Encode(inData)
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim cOut, sOut, I

'For each group of 3 bytes
For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup

'Create one long from this 3 bytes.
nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
  &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

'Oct splits the long To 8 groups with 3 bits
nGroup = Oct(nGroup)

'Add leading zeros
nGroup = String(8 - Len(nGroup), "0") & nGroup

'Convert To base64
pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
    Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
    Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
    Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
'Add the part To OutPut string
sOut = sOut + pOut

'Add a new line For Each 76 chars In dest (76*3/4 = 57)
'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
Next
Select Case Len(inData) Mod 3
Case 1: '8 bit final
  sOut = Left(sOut, Len(sOut) - 2) + "=="
Case 2: '16 bit final
  sOut = Left(sOut, Len(sOut) - 1) + "="
End Select
Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function

'RC4
Function RunRC4(sMessage, strKey)
Dim kLen, x, y, i, j, temp
Dim s(256), k(256)
'Init keystream
klen = Len(strKey)
For i = 0 To 255
    s(i) = i
    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 255
    temp = s(i)
    s(i) = s(j)
    s(j) = temp
Next
'Drop n bytes from keystream
x = 0
y = 0
For i = 1 To 3072
    x = (x + 1) Mod 255
    y = (y + s(x)) Mod 255
    temp = s(x)
    s(x) = s(y)
    s(y) = temp
Next
'Encode/Decode
For i = 1 To Len(sMessage)
    x = (x + 1) Mod 255
    y = (y + s(x)) Mod 255
    temp = s(x)
    s(x) = s(y)
    s(y) = temp
    RunRC4 = RunRC4 & Chr(s((s(x) + s(y)) Mod 255) Xor Asc(Mid(sMessage, i, 1)))
Next
End Function
encStr = Base64Encode(RunRC4(username,"1234"))

Python Server Page

 def decode64(in_str):
    import base64
     decodedStr = base64.b64decode(in_str)
 return decodedStr

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,y = 0, 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)
 decStr = rc4crypt(decode64(encStr), "1234")

Can't figure out why python decrypt doesn't render the original string when using the same key "1234"

6
  • 1
    Punch "python rc4" into your favorite search engine. My first hit was exactly what you need. If you need base64 code, punch "python base64" in next. Commented Mar 19, 2012 at 15:46
  • Have, everything i find is for encrypting...I need decrypting..I don't think RC4 is a oneway hash right? Commented Mar 19, 2012 at 15:47
  • 7
    RC4's encryption and decryption operations are identical. RC4 generates a pseudo-random byte stream and XORs the data with it. So it's its own reverse. (In fact, the link I gave you explains this.) Commented Mar 19, 2012 at 15:50
  • Alright, so perhaps the ASP/VBscript encryption part is not working.. Commented Mar 19, 2012 at 15:55
  • 1
    Or you aren't using exactly the same key. (For example, you may be using the key in ASCII in one case and in hex in the other. Or you may be including a terminating zero byte in one case and not the other. And so on.) Commented Mar 19, 2012 at 15:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.