Skip to content

Instantly share code, notes, and snippets.

@hsauers5
Created November 30, 2020 18:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsauers5/491f9dde975f1eaa97103427eda50071 to your computer and use it in GitHub Desktop.
Save hsauers5/491f9dde975f1eaa97103427eda50071 to your computer and use it in GitHub Desktop.
Implementation of RC4 cipher in Python.
# Harry Sauers
# rc4.py
# demo of RC4 encryption algorithm
def key_scheduling(key):
sched = [i for i in range(0, 256)]
i = 0
for j in range(0, 256):
i = (i + sched[j] + key[j % len(key)]) % 256
tmp = sched[j]
sched[j] = sched[i]
sched[i] = tmp
return sched
def stream_generation(sched):
stream = []
i = 0
j = 0
while True:
i = (1 + i) % 256
j = (sched[i] + j) % 256
tmp = sched[j]
sched[j] = sched[i]
sched[i] = tmp
yield sched[(sched[i] + sched[j]) % 256]
def encrypt(text, key):
text = [ord(char) for char in text]
key = [ord(char) for char in key]
sched = key_scheduling(key)
key_stream = stream_generation(sched)
ciphertext = ''
for char in text:
enc = str(hex(char ^ next(key_stream))).upper()
ciphertext += (enc)
return ciphertext
def decrypt(ciphertext, key):
ciphertext = ciphertext.split('0X')[1:]
ciphertext = [int('0x' + c.lower(), 0) for c in ciphertext]
key = [ord(char) for char in key]
sched = key_scheduling(key)
key_stream = stream_generation(sched)
plaintext = ''
for char in ciphertext:
dec = str(chr(char ^ next(key_stream)))
plaintext += dec
return plaintext
if __name__ == '__main__':
ed = input('Enter E for Encrypt, or D for Decrypt: ').upper()
if ed == 'E':
plaintext = input('Enter your plaintext: ')
key = input('Enter your secret key: ')
result = encrypt(plaintext, key)
print('Result: ')
print(result)
elif ed == 'D':
ciphertext = input('Enter your ciphertext: ')
key = input('Enter your secret key: ')
result = decrypt(ciphertext, key)
print('Result: ')
print(result)
else:
print('Error in input - try again.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment