2026-07-12 20:22:15 +01:00

28 lines
773 B
Python

import string
ALPHA = string.ascii_uppercase
def caesar_decrypt(text: str, shift: int) -> str:
out = []
for ch in text:
if ch.isalpha():
is_lower = ch.islower()
base = ch.upper()
idx = ALPHA.index(base)
new_idx = (idx - shift) % 26
new_ch = ALPHA[new_idx]
out.append(new_ch.lower() if is_lower else new_ch)
else:
out.append(ch)
return ''.join(out)
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
print('Usage: solve.py ciphertext.txt')
sys.exit(1)
with open(sys.argv[1], 'r') as f:
ct = f.read().strip()
for s in range(26):
candidate = caesar_decrypt(ct, s)
print(f"Shift={s}: {candidate}")