Added Lab2/Lab3 IS
This commit is contained in:
parent
011d5aded1
commit
0755dd4b76
6 changed files with 109 additions and 140 deletions
|
@ -8,23 +8,25 @@ def main():
|
|||
for a in range(1, 26):
|
||||
if gcd(a, 26) != 1:
|
||||
continue
|
||||
a_inv = mod_inverse(a, 26)
|
||||
if a_inv is None:
|
||||
continue
|
||||
|
||||
for b in range(26):
|
||||
# if key produces "ab" -> "GL"
|
||||
if (a * 0 + b) % 26 == 6 and (a * 1 + b) % 26 == 11:
|
||||
a_inv = mod_inverse(a, 26)
|
||||
decrypted = ""
|
||||
for char in ciphertext:
|
||||
if char.isalpha():
|
||||
y = ord(char.upper()) - ord('A')
|
||||
# check constraint: "ab" -> "GL" (a*0+b=6, a*1+b=11 mod 26)
|
||||
if (b % 26 == 6) and ((a + b) % 26 == 11):
|
||||
decrypted = []
|
||||
for ch in ciphertext:
|
||||
if ch.isalpha():
|
||||
y = ord(ch.upper()) - ord('A')
|
||||
x = (a_inv * (y - b)) % 26
|
||||
decrypted += chr(x + ord('A'))
|
||||
decrypted.append(chr(x + ord('A')))
|
||||
else:
|
||||
decrypted += char
|
||||
decrypted.append(ch)
|
||||
|
||||
print(f"Key found: a={a}, b={b}")
|
||||
print(f"Ciphertext: {ciphertext}")
|
||||
print(f"Decrypted: {decrypted}")
|
||||
print(f"Decrypted: {''.join(decrypted)}")
|
||||
return
|
||||
|
||||
def gcd(a, b):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue