def KSA(key): S = list(range(256)) j = 0 for i in range(256): j = (j + S[i] + ord(key[i % len(key)])) % 256 S[i], S[j] = S[j], S[i] return S def PRGA(S): i,j = 0, 0 while True: i = (i + 1) % 256 j = (j + S[i]) % 256 S[i], S[j] = S[j], S[i] K = S[(S[i] + S[j]) % 256] yield K def RC4(data, key): S = KSA(key) gen = PRGA(S) data = bytearray(data, encoding='utf-8') result = bytearray(c ^ n for c, n in zip(data, gen)) return str(result) key = 'this_is_a_key' message = 'this_is_a_message' chiphertext = RC4(message, key) print(chiphertext)
これでもいける
from Crypto.Cipher import ARC4 key = b'Very long and confidential key' cipher = ARC4.new(key) msg = cipher.encrypt(b'Open the pod bay doors, HAL') print(msg)