Расшифровка RC4 с ключом в Python - PullRequest
1 голос
/ 19 марта 2012

Я вытащил этот код отсюда для asp http://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript,, который я затем запускаю через base64.

Интересно, кто-нибудь может помочь мне разобраться, как написать часть дешифрования, но на Python.Поскольку расшифровка произойдет на моей странице Python Server.

Найдена эта http://www.id -snippet.com / 20801 / python-rc4-cipher / , но она не расшифровывает RC4asp из первой ссылки.

-Jim

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

Не могу понятьпочему python decrypt не отображает исходную строку при использовании того же ключа «1234»

...