Skip to content

Instantly share code, notes, and snippets.

@Sh1n0g1
Created July 5, 2017 11:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sh1n0g1/7ccb958ef6a53c2b0855ae21c6591186 to your computer and use it in GitHub Desktop.
Save Sh1n0g1/7ccb958ef6a53c2b0855ae21c6591186 to your computer and use it in GitHub Desktop.
Encrypt/Decrypt RC4 by a String Key
import sys
def rc4init(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]
return box
def rc4crypt(data, box):
x=0
y=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)
argvs = sys.argv
argc = len(argvs)
if (argc !=4):
print "python ./rc4.py file1 file2 key"
quit()
with open(argvs[1], mode='rb') as file:
fileContent = file.read()
sbox=rc4init(argvs[3])
decdata=rc4crypt(fileContent, sbox)
with open(argvs[2], 'wb') as EncOrDecrypted_file:
EncOrDecrypted_file.write(decdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment