Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save quandqn/3d4fb9553ba2ffc82cafc03feee84523 to your computer and use it in GitHub Desktop.
Save quandqn/3d4fb9553ba2ffc82cafc03feee84523 to your computer and use it in GitHub Desktop.
def KSA(key):
key_len = len(key)
S = range(256)
j = 0
for i in xrange(256):
j = (j + S[i] + key[i % key_len]) % 256
S[i], S[j] = S[j], S[i]
return S
def PRGA(S):
i = 0
j = 0
while True:
i = (i + 1) % 256
j = (j + 1) % 256
S[i], S[j] = S[j], S[i]
K = S[(S[i] + S[j]) % 256]
yield K
def RC4(key):
S = KSA(key)
return PRGA(S)
def enc(key, m):
key = [ord(c) for c in key]
key_stream = RC4(key)
cipher = [chr(ord(c) ^ key_stream.next()) for c in m]
return ''.join(cipher)
def dec(key, cipher):
key = [ord(c) for c in key]
key_stream = RC4(key)
plain = [chr(ord(c) ^ key_stream.next()) for c in cipher]
return ''.join(plain)
def main():
k1 = '.....'
k2 = '.....'
m = '......'
cipher = enc(k2, enc(k1, m)).encode('base64')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment