Skip to content

Instantly share code, notes, and snippets.

@t3ntman
Last active March 25, 2022 06:15
Show Gist options
  • Select an option

  • Save t3ntman/201e439bc7818a25af236cac6b3eacc6 to your computer and use it in GitHub Desktop.

Select an option

Save t3ntman/201e439bc7818a25af236cac6b3eacc6 to your computer and use it in GitHub Desktop.
# rc4 encryption
# modified from source: https://github.com/bozhu/RC4-Python/blob/master/rc4.py
import sys
def crypt(key, data):
S = list(range(256))
j = 0
for i in list(range(256)):
j = (j + S[i] + ord(key[i % len(key)])) % 256
S[i], S[j] = S[j], S[i]
j = 0
y = 0
out = []
for char in data:
j = (j + 1) % 256
y = (y + S[j]) % 256
S[j], S[y] = S[y], S[j]
if version == 2:
out.append(unichr(ord(char) ^ S[(S[j] + S[y]) % 256]))
if version == 3:
out.append(chr(ord(char) ^ S[(S[j] + S[y]) % 256]))
return ''.join(out)
if __name__ == '__main__':
if sys.version_info.major == 2:
version = 2
else:
version = 3
plain = 'this is plaintext data'
key = 'this is a super complex key'
# testing encryption
encrypted = crypt(key, plain)
print('encrypted: ' + encrypted)
print(type(encrypted))
# testing decryption
decrypted = crypt(key, encrypted)
print('decrypted: ' + decrypted)
print(type(decrypted))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment