1

我从这里为asp http://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript提取了这段代码,然后我通过base64运行。

我想知道是否有人可以帮助我弄清楚如何用 Python 编写解密片段。因为解密将发生在我的 Python 服务器页面上。

找到这个http://www.id-snippet.com/20801/python-rc4-cipher/,但它没有从第一个链接解密 RC4 asp。

-吉姆

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% ASP 页面

'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 服务器页面

 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")

无法弄清楚为什么使用相同的密钥“1234”时python解密不呈现原始字符串

4

0 回答 0