如何使用Python解密使用rc4加密的文件?

Gal*_*Gal 1 python encryption rc4-cipher

我得到了一个用rc4密钥加密的文件。

我获得了该密钥,并希望使用python脚本对其进行解密。

我怎样才能做到这一点?

niv*_*xer 5

经过3秒钟的Google搜索,发现了以下内容:http : //www.emoticode.net/python/python-implementation-of-rc4-algorithm.html

因此对其进行了修改:

import base64

data = base64.b64decode("<encrypted file contents>")
key = "<rc4 key>"

S = range(256)
j = 0
out = []

#KSA Phase
for i in range(256):
    j = (j + S[i] + ord( key[i % len(key)] )) % 256
    S[i] , S[j] = S[j] , S[i]

#PRGA Phase
i = j = 0
for char in data:
    i = ( i + 1 ) % 256
    j = ( j + S[i] ) % 256
    S[i] , S[j] = S[j] , S[i]
    out.append(chr(ord(char) ^ S[(S[i] + S[j]) % 256]))

print ''.join(out)
Run Code Online (Sandbox Code Playgroud)

不知道这是否行得通,因为您没有给我们提供任何数据来使用..下次,请发布数据示例,您已经尝试过的代码以及遇到的错误。


编辑-使用文件

import base64

with open("/path/to/file.txt", "r") as encrypted_file:
    data = base64.b64decode(encrypted_file.read())
key = "<rc4 key>"

S = range(256)
j = 0
out = []

#KSA Phase
for i in range(256):
    j = (j + S[i] + ord( key[i % len(key)] )) % 256
    S[i] , S[j] = S[j] , S[i]

#PRGA Phase
i = j = 0
for char in data:
    i = ( i + 1 ) % 256
    j = ( j + S[i] ) % 256
    S[i] , S[j] = S[j] , S[i]
    out.append(chr(ord(char) ^ S[(S[i] + S[j]) % 256]))

decrypted_text = ''.join(out)
with open('decrypted.txt', 'w') as decrypted_file:
    decrypted_file.write(decrypted_text)
Run Code Online (Sandbox Code Playgroud)