fixed l1q5

This commit is contained in:
Student 2025-08-19 10:11:36 +05:30
parent badd502331
commit 2a4d95afe8

View file

@ -1,20 +1,15 @@
def main(): from string import ascii_uppercase as A
shift = (ord('C') - ord('y')) % 26
ctext = "XVIEWYWI" def idx(c): return A.index(c)
plaintext = [] def infer_shift(ct, pt):
return (idx(ct[0]) - idx(pt[0].upper())) % 26
for ch in ctext: def decrypt(ct, shift):
if ch.isalpha(): return ''.join(A[(idx(c) - shift) % 26] if c.isalpha() else c for c in ct)
base = ord('a')
shifted = (ord(ch.lower()) - base - shift) % 26 + base
plaintext.append(chr(shifted))
else:
plaintext.append(ch)
print(f"Attack type: Known plaintext attack") known_ct = "CIW"
print(f"Ciphertext: {ctext}") known_pt = "yes"
print(f"Decrypted: {''.join(plaintext)}") cipher = "XVIEWYWI"
if __name__ == '__main__': shift = infer_shift(known_ct, known_pt)
main() print(decrypt(cipher, shift))