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 33 34 35 36 37 38
| import string import random
ALPHABET = string.ascii_uppercase
def generate_keystream(key, length): keystream = [] while len(keystream) < length: keystream.extend(key) key = key[1:] + key[:1] return keystream
def decrypt(ciphertext, key): keystream = generate_keystream(key, len(ciphertext)) decrypted = []
for i in range(len(ciphertext)): if ciphertext[i] in ALPHABET: decrypted_char_index = (ALPHABET.index(ciphertext[i]) - keystream[i]) % 26 decrypted_char = ALPHABET[decrypted_char_index] decrypted.append(decrypted_char) else: decrypted.append(ciphertext[i])
return "".join(decrypted)
with open("ciphertext.txt", "r") as f: ciphertext = f.read()
key = [12, 21, 8, 19, 8, 8, 19, 23, 15, 12, 25, 16, 12]
plaintext = decrypt(ciphertext, key)
print("Decrypted Message:") print(plaintext.lower())
|