Reputation: 180
I found a couple RC4 scripts online for python. I used one from http://www.joonis.de/en/code/rc4-algorithm that I simplified and am using just for decrypting files that have been encrypted with RC4 with a 40byte key.
testFile = 'input.xml'
key = 'Utood9dRzY2ugHYi9jl9ug2UNytIIxjk6Ptouaig'
out=file('output.xml','wb')
with open (testFile, "rb") as f:
data = f.read()
S = range(256)
j = 0
for i in range(256):
j = (j + S[i] + ord(key[i % len(key)])) % 256
S[i] , S[j] = S[j] , S[i]
for char in data:
i = 0
j = 0
i = ( i + 1 ) % 256
j = ( j + S[i] ) % 256
S[i] , S[j] = S[j] , S[i]
out.write(chr(ord(char) ^ S[(S[i] + S[j]) % 256]))
out.close()
I know what the result should be because I have successfully decrypted the input.xml file @ http://rc4.online-domain-tools.com/ but when I do it through the script the output is wrong.
I'm kind of at a loss of where the problem may lie. If anybody can tell me what I'm doing wrong I'd highly appreciate it!
I want to say I may need to find out how to code the algorithm to work with hex values vs integers but it seems pythons already has it hex. I'm not too sure though.
Link to testfile = http://bayfiles.net/file/1kPgD/bVuyGw/input.xml
Upvotes: 2
Views: 4811
Reputation: 3947
According to the algorithm you pasted http://www.joonis.de/en/code/rc4-algorithm, error is in these 3 lines:
for char in data:
i = 0
j = 0
It should be:
i = 0
j = 0
for char in data:
Upvotes: 1