added lab2 q1

This commit is contained in:
Student 2025-08-05 09:25:24 +05:30
parent 933a52b3a8
commit 857ebf4a77
3 changed files with 70 additions and 6 deletions

View file

@ -2,21 +2,16 @@ def main():
# Affine cipher: E(x) = (ax + b) mod 26
# Given: "ab" -> "GL"
# a=0, b=1 -> G=6, L=11
# So: 6 = (a*0 + b) mod 26 -> b = 6
# And: 11 = (a*1 + b) mod 26 -> 11 = (a + 6) mod 26 -> a = 5
ciphertext = "XPALASXYFGFUKPXUSOGEUTKCDGEXANMGNVS"
# Try all possible values of a and b for affine cipher
for a in range(1, 26):
# a must be coprime to 26
if gcd(a, 26) != 1:
continue
for b in range(26):
# Check if this key produces "ab" -> "GL"
# if key produces "ab" -> "GL"
if (a * 0 + b) % 26 == 6 and (a * 1 + b) % 26 == 11:
# Found the key, now decrypt the message
a_inv = mod_inverse(a, 26)
decrypted = ""
for char in ciphertext: