Vigenere

Can you decrypt this message? Decrypt this message using this key "CYLAB".

starting the challenge we were told about vigenere cipher so i started learning about vigenere cipher so its similar to caesar cipher but the difference is that we are gonna use a key that will repeat until we make the cipher text hope you understand what im saying

so we just need to map all of the possibilities then decrypt using that

import string
instr = ""
with open('cipher.txt', 'r') as a:
    instr = a.readline().strip()

key = "CYLAB"
outstr = ""
offset_lower = ord('a')
offset_upper = ord('A')
lower = string.ascii_lowercase
upper = string.ascii_uppercase

dicts_l={}

for k in key:
    key_loc = ord(k)-offset_upper
    dicts_l[k] = {}
    for l in lower:
        cipher = chr((ord(l) - offset_lower + key_loc) % 26 + offset_lower)
        dicts_l[k][cipher] = l

dicts_u = {}

for k in key:
    key_loc = ord(k) - offset_upper
    dicts_u[k] = {}
    for u in upper:
        dicts_u[k][chr((ord(u) - offset_upper + key_loc) % 26 + offset_upper)] = u

count=0
for c in instr:
    if c.isalpha():
        if c.islower():
            outstr += dicts_l[key[count % len(key)]][c]
        elif c.isupper():
            outstr += dicts_u[key[count % len(key)]][c]
        count += 1
    else:
        outstr += c
print(outstr)

Last updated