fixed l1q5
This commit is contained in:
parent
badd502331
commit
2a4d95afe8
1 changed files with 11 additions and 16 deletions
|
@ -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))
|
Loading…
Add table
Add a link
Reference in a new issue