Skip to content

Instantly share code, notes, and snippets.

@mosesrenegade
Created May 26, 2018 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosesrenegade/6c01cf881a310738312294a4446c48dc to your computer and use it in GitHub Desktop.
Save mosesrenegade/6c01cf881a310738312294a4446c48dc to your computer and use it in GitHub Desktop.
RC4 No Swap
#!/usr/bin/env python
debug = 0
def KSA(key):
keylength = len(key)
if debug == 1:
print("Current Keylength is " + str(keylength))
S = range(256)
j = 0
for i in range(256):
j = (j + S[i] + key[i % keylength]) % 256
if debug == 1:
print("Before Swap i is set to: " + str(i) + " and j is currently: " + str(j))
#
# Let's remove swapping as a test for the special no swap thing
#
#S[i], S[j] = S[j], S[i] # swap
if debug == 1:
print("Now swap, Si is: " + str(S[i]) + " and Sj is: " + str(S[j]))
if debug == 1:
print("Will return S which is: " + str(S))
return S
def PRGA(S):
i = 0
j = 0
while True:
if debug == 1:
print("Currently i is: " + str(i) + " and j is: " + str(j))
i = (i + 1) % 256
j = (j + S[i]) % 256
#
# Let's remove the swapping as a test for the special no swap thing
#S[i], S[j] = S[j], S[i] # swap
K = S[(S[i] + S[j]) % 256]
yield K
def RC4(key):
S = KSA(key)
return PRGA(S)
if __name__ == '__main__':
key = 'Key'
plaintext = 'Plaintext'
def convert_key(s):
if debug == 1:
print("The Key here is set to: " + str(key))
return [ord(c) for c in s]
key = convert_key(key)
if debug == 1:
print("We will now convert the key to oridinals of each value, key is now" + str(key))
keystream = RC4(key)
import sys
for c in plaintext:
sys.stdout.write("%02X" % (ord(c) ^ keystream.next()))
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment