Skip to content

Instantly share code, notes, and snippets.

@lokkju
Last active October 7, 2018 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lokkju/9846ea4cd1941b1cd6a3c5c357785df3 to your computer and use it in GitHub Desktop.
Save lokkju/9846ea4cd1941b1cd6a3c5c357785df3 to your computer and use it in GitHub Desktop.
rotating vigenere decoder for adversary group
from itertools import count
from string import ascii_letters
_cidx = dict(zip(ascii_letters, count(1)))
def decipher(key,enc,rot):
"""
Deciphers Vigenere cipher with the given key and rotated alphabet
Parameters:
key (str): Encryption key
enc (str): Encrypted text
rot (int): how many steps to rotate the alphabet
Returns:
str: Decrypted text
"""
i = 0
ret = ''
_dc = lambda c : ascii_letters[(_cidx[c] - _cidx[key[i%len(key)]] + rot)%26]
_dc_case = lambda c : _dc(c) if c.islower() else _dc(c).upper()
_dc_any = lambda c : c if not c.isalpha() else _dc_case(c)
for c in enc:
ret += _dc_any(c)
if c.isalpha(): i += 1
return ret
if __name__ == "__main__":
encrypted = """
Tkz sedy icgjch uou omdw
hu mr jyzj bauerhgkhj tfdik'y xniddp uw eebu.
-Ymuggj Cxjwiqaa.
Qhat qpk pz ddacubd lp pt rgzgtfc uk olisg pwoxoezaks lpc.
Ofu euenf gvyktb dq jva.
Nexq hufw te qwzuittb mk iyw.
Mdsk dhh mfloz amqp.
Sagaa dzri urrx iw ounno xrlcb.
Kbqi iwfbwbz yhu, syovcbv ewv vte.
Jatlag ipl ez xrj dxin wiyy.
Ig uxkw nuh jt bxyg lyxy.
Szpzy og yty qcaw blvtjx.
Fe, Eys fei jhy hvpaq krpq?
"""
key = "AdversaryGroup"
for ln,encline in enumerate(encrypted.split("\n")):
print ln,decipher(key,encline,ln-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment