I'm working on the MIT Opencourseware problem set #4, and the problem is to write a code that can break a multi-level Caesar shifted text. The text is shifted this way:
plaintext: a very short string
shift1: 3..................
shift2: 6...........
shift3: 4.....
ciphertext: dcyhuacaqx biefdvt
So the rules are the shift can begin only at the beginning of a word (so after a space if it's not the first word) and the shifts are layered, one on top of the other. I have tried for too many hours to solve that problem, I came up with a few solutions, but they never seem to work on a large encrypted text, only on a few words at a time. This one is my latest:
def decodeV2(text, wordlist):
final_sentence = ''
while len(text) > 0:
counter = 0
if len(text) > 15:
text_slice = text[-15:]
else:
text_slice = text [:]
print 'new text slice=', text_slice
while True:
if counter > 0:
text_slice = text_slice[1:]
print 'text_slice =', text_slice
part_shift = find_best_shift(wordlist, text_slice)
print 'text_slice shift =', part_shift
counter += 1
if part_shift != None:
break
print 'done', 'word is:', text_slice, 'shift is:', part_shift
print ' decoded word is:', apply_coder(text_slice, build_decoder(part_shift))
text = text[:-len(text_slice)]
print 'new text =', text
final_sentence = apply_coder(text_slice, build_decoder(part_shift)) + final_sentence
return final_sentence
My idea here was to find the right word at the end of the sentence, and then remove it from the text, and do it again with the new, shorter text until it's finished. But I get wrong shifts on long pieces of text so it's no good. find_best_shift() returns an integer - the shift of the Caesar cipher. apply_coder() simply applies a Caesar shift to a piece of text.
It is recommended in the problem to use recursion for this, I tried and also failed. Please help me out, I've been stuck on this for too long. Here's a link for the problem set, its question #4: problem_set.pdf Thanks!
Aucun commentaire:
Enregistrer un commentaire