Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Vigenere Cipher by jcg
def decode_vigenere(old_decrypted, old_encrypted, new_encrypted):
#find a candidate key with length>=length(key)
# "checkiocheckiocheck"
candidate_key = [chr(ord('A') +
(ord(old_encrypted[i]) - ord(old_decrypted[i])) % 26)
for i in range(len((old_decrypted)))]
# and now something completely different
# find a repeat in candidate key : "checkiocheck"->"checkio"
length = len(candidate_key)
for s in range(1, length):
if candidate_key[: length - s] == candidate_key[s: ]:
length = s
break
key = ''.join(candidate_key[: length])
# and with the key mod len(key), repeat the begin
return ''.join(chr(ord('A') +
(ord(new_encrypted[i]) - ord(key[i % length])) % 26)
for i in range(len((new_encrypted))))
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert decode_vigenere('DONTWORRYBEHAPPY',
'FVRVGWFTFFGRIDRF',
'DLLCZXMFVRVGWFTF') == "BEHAPPYDONTWORRY", "CHECKIO"
assert decode_vigenere('HELLO', 'OIWWC', 'ICP') == "BYE", "HELLO"
assert decode_vigenere('LOREMIPSUM',
'OCCSDQJEXA',
'OCCSDQJEXA') == "LOREMIPSUM", "DOLORIUM"
Sept. 25, 2014
Comments: