1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import re
def rc4_decrypt(ciphertext, key): S = list(range(256)) j = 0 decrypted = bytearray()
for i in range(256): j = (j + S[i] + key[i % len(key)]) % 256 S[i], S[j] = S[j], S[i]
i = j = 0 for byte in ciphertext: i = (i + 1) % 256 j = (j + S[i]) % 256 S[i], S[j] = S[j], S[i] k = S[(S[i] + S[j]) % 256] decrypted.append(byte ^ k)
return decrypted
ciphertext_str = "1Bh, 0CAh, 0AEh, 0EFh, 1Eh, 95h, 4Bh, 0C2h, 0D5h, 0E3h, 33h, 76h, 4Fh, 0F9h, 4Fh, 0D2h, 0FCh, 60h" ciphertext_bytes = bytearray(int(match.group(1), 16) for match in re.finditer(r"([0-9A-Fa-f]+)h", ciphertext_str))
key_str = "52h, 43h, 34h, 6Bh, 65h, 79h" key_bytes = bytearray(int(match.group(1), 16) for match in re.finditer(r"([0-9A-Fa-f]+)h", key_str))
plaintext = rc4_decrypt(ciphertext_bytes, key_bytes) print("解密后的明文:", plaintext.decode('latin-1'))
|