Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Vigenere Cipher by Hanna_Hofman
def find_shift(enc,dec):
# having a letter of encrypted message and of decrypted one, find shift
shift = ord(enc)-ord(dec)
if shift >= 0: return shift
return 26 + shift
def remove_shift(char,shift):
# having letter from encrypted message and shift, find decrypted letter
new_ord = ord(char)-shift
if new_ord >= ord('A'): return chr(new_ord)
return chr(26 + new_ord)
def make_seq(key, length):
# having a key make key-sequence of needed length
seq = key * (length//len(key) + 1)
return seq[:length]
def extract_key(seq):
# having a key-sequence find key
for i in range(1,len(seq)+1):
if make_seq(seq[:i], len(seq)) == seq: return seq[:i]
def decode_vigenere(old_decrypted, old_encrypted, new_encrypted):
# extract key-sequence from old messages
key_seq = [find_shift(old_encrypted[i], old_decrypted[i]) for i in range(len(old_encrypted))]
# extract key from sequence
key = extract_key(key_seq)
# make new key-sequence of proper length
new_seq = make_seq(key,len(new_encrypted))
# decrypt message
new_decrypted = ''.join([remove_shift(new_encrypted[i],new_seq[i]) for i in range(len(new_seq))])
return new_decrypted
Dec. 9, 2014
Comments: