首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python解密用rc4加密的文件?

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

Stack Overflow用户
提问于 2015-04-13 22:24:26
回答 2查看 21.3K关注 0票数 2

我有一个用rc4密钥加密的文件。

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

我该怎么做呢?

EN

回答 2

Stack Overflow用户

发布于 2015-04-13 22:45:27

在谷歌搜索3秒后找到了以下内容:http://www.emoticode.net/python/python-implementation-of-rc4-algorithm.html

对其进行如下修改:

代码语言:javascript
复制
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)

不确定这是否会起作用,因为您没有给我们提供任何数据。下一次请发布一个数据示例,您已经尝试过的代码,以及您收到的错误。

编辑--处理文件

代码语言:javascript
复制
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)
票数 5
EN

Stack Overflow用户

发布于 2019-12-20 17:56:26

PyCryptodome现在做到了:https://pycryptodome.readthedocs.io/en/latest/src/cipher/arc4.html

代码语言:javascript
复制
from Crypto.Cipher import ARC4
key = b'Very long and confidential key'
cipher = ARC4.new(key)
msg = cipher.encrypt(b'Open the pod bay doors, HAL')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29607753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档