Skip to main content
 首页 » 编程设计

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

2025年05月04日74sxdcgaq8080

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

我得到了那个 key ,想用 python 脚本解密它。

我该怎么做?

请您参考如下方法:

在 Google 搜索 3 秒后找到以下内容: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) 

不确定这是否可行,因为您没有提供任何数据供我们使用。下次请发布数据示例、您已经尝试过的代码以及遇到的错误。


编辑——处理文件

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)