howmuch515 icon

rc4.py

howmuch515 | PRO | 10/25/19 07:28:57 AM UTC | 0 ⭐ | 327 👁️ | Never ⏰ | []
Python |

1.08 KB

|

None

|

0 👍

/

0 👎

#!/bin/env python3
from sys import argv
 
def info(msg):
    print(f"[+] {msg}")
 
def success(msg):
    print(f"[*] {msg}")
 
def KSA(key):
    S = list(range(256))
    j = 0
    for i in range(256):
        j = (j + S[i] + key[i % len(key)]) % 256
        S[i], S[j] = S[j], S[i]
    return S
 
def PRGA(sbox):
    i, j = 0, 0
    while True:
        i = (i+1) % 256
        j = (j + sbox[i]) % 256
        sbox[i], sbox[j] = sbox[j], sbox[i]
        K = sbox[(sbox[i] + sbox[j]) % 256]
        yield K
 
def rc4(data, key):
    S = KSA(key)
    gen = PRGA(S)
    result = "".join(chr(c ^ n) for c, n in zip(data, gen))
    return str(result)
 
def main():
    CIPHER_FILE = argv[1]
    KEY_FILE = argv[2]
 
    # load cipher
    cipher = b''
    with open(CIPHER_FILE, 'rb') as f:
        cipher = f.read()
 
    # load key
    key = b''
    with open(KEY_FILE, 'rb') as f:
        key = f.read()
 
    # execute RC4
    sbox = KSA(key)
 
    # output result
    result = rc4(cipher, key)
    success(result)
 
 
if __name__ == "__main__":
    main()

Comments