From 0a8d248c149a8499fc9b92e8ea7a5f4780be4d76 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 11 Oct 2013 18:37:13 +0100 Subject: [PATCH] Can now break Caesar ciphers --- 2013/.1a.ciphertext.kate-swp | Bin 0 -> 74 bytes 2013/.1b.ciphertext.kate-swp | Bin 0 -> 90 bytes 2013/1a.plaintext | 13 +++++++++++++ 2013/1b.plaintext | 3 +++ cipher.py | 29 +++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 2013/.1a.ciphertext.kate-swp create mode 100644 2013/.1b.ciphertext.kate-swp create mode 100644 2013/1a.plaintext create mode 100644 2013/1b.plaintext diff --git a/2013/.1a.ciphertext.kate-swp b/2013/.1a.ciphertext.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..9a96010fbf38b3a6d03c9c1eaa5eeb1b15d18195 GIT binary patch literal 74 zcmZQzU=Z?7EJ;-eE>A2_aLdd|RWQ;sU|?WSNHI6DFfcMROtZ8wN;FJOOa$`M4AYVm Wjf@S_3=$24J%OsYffxiB4P60-h!2YZ literal 0 HcmV?d00001 diff --git a/2013/.1b.ciphertext.kate-swp b/2013/.1b.ciphertext.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..06a78db547689452de6cd31863f32e18bc2072f2 GIT binary patch literal 90 zcmZQzU=Z?7EJ;-eE>A2_aLdd|RWQ;sU|?WSFiA5`OEgM0H8f03HnK2FOg1r1HB3n} hGfpy1O-eK~4fX`8Vq#!mTmqySlU?Ebr67KyD*(W565;>= literal 0 HcmV?d00001 diff --git a/2013/1a.plaintext b/2013/1a.plaintext new file mode 100644 index 0000000..da08fbf --- /dev/null +++ b/2013/1a.plaintext @@ -0,0 +1,13 @@ +DEAR PHIL, + +HOW COULD I PASS UP THE OPPORTUNITY TO WORK ON THIS? I HAD A CRACK AT ANOTHER OF THE NOTES YOU FOUND IN MONTMARTRE. IT LOOKS LIKE THE OLDEST ITEM IN THE PACKET AND I THOUGHT IT MIGHT BE A GOOD PLACE TO START. IT MAKES SOME SENSE OF THE BUCHENWALD REFERENCE IN THE LATER NOTE YOU SENT. + +IT DIDN'T EXPLAIN THE PARIS LINK SO I SENT A TEAM INTO THAT NEIGHBOURHOOD TO GATHER INTEL BUT THEY DIDN'T COME UP WITH VERY MUCH. WE DID GET ONE REPORT THAT THE HOUSE HAD BEEN OWNED BY A GERMAN FAMILY BEFORE THE WAR, BUT THAT IT HAD BEEN TAKEN OVER BY AN SS OFFICER IN NINETEEN FORTY ONE. SURETE RECORDS SUGGEST THAT THE FAMILY CAME FROM WEIMAR IN AUGUST NINETEEN THIRTY SEVEN, WHICH IS SUGGESTIVE GIVEN THE TIMING AND THE GEOGRAPHY, SO I HAVE SENT THE TEAM TO BUCHENWALD TO SEE WHAT THEY CAN FIND. + +I AM JUMPING TO CONCLUSIONS HERE, BUT THE PORTRAIT SARAH MENTIONS HAS TO BE THE MONA LISA. I HAVE BEEN TRYING TO GET ACCESS TO IT, BUT THE FRENCH AUTHORITIES ARE SPOOKED. THE THEFT OF THE PAINTING BY PERUGGIA BACK IN NINETEEN ELEVEN HAS MADE THEM VERY SENSITIVE. THE NAME CHAUDRON WAS MENTIONED MORE THAN ONCE, AND I NEED SOME TIME TO LOOK INTO THE HISTORY. + +IF YOU HAVE ANY INFLUENCE AT ALL AT THE MUSEUM I THINK WE NEED A PROPER EXAMINATION OF THE PAINTING, AND I NEED TO KNOW WHAT HAPPENED TO IT DURING THE WAR. + +ALL THE BEST, + +HARRY \ No newline at end of file diff --git a/2013/1b.plaintext b/2013/1b.plaintext new file mode 100644 index 0000000..1e22a0f --- /dev/null +++ b/2013/1b.plaintext @@ -0,0 +1,3 @@ +KINDNESS IS TO BE FOUND EVERYWHERE, EVEN IN THIS STINKING PLACE. THE GUARDS TAKE AWAY ANYTHING THEY CAN THAT MIGHT BRING US SOME JOY, BUT THEY CAN'T TAKE EVERYTHING. SOME OF THE GIRLS SING, OTHERS TELL STORIES TO KEEP THE FEAR AT THE EDGE OF OUR HEARTS - IT NEVER COMPLETELY LEAVES US - AND MY PICTURES ARE ALL I HAVE TO OFFER IN RETURN. PAPER IS PRECIOUS AND MOST OF MY PAINTINGS AND SKETCHES ARE HIDDEN ON THE ROUGH WOODEN SLATS OF OUR BUNKS UNDER THE THIN MATTRESSES. DETAIL IS IMPOSSIBLE, BUT WOULD BE HARD EVEN ON FINE CANVAS GIVEN THE PIGMENTS I CAN MAKE FROM THE BRICK DUST, SOIL AND SCRUBBY WEEDS IN THE CAMP. I AM THANKFUL EVERYDAY FOR MY GRANDFATHER'S INSISTENCE THAT AN ARTIST SHOULD BE ABLE TO MAKE THEIR OWN COLOURS. WHEN I DO FIND PAPER, I WRITE. THIS GREASY SCRAP WAS THE WRAPPING ON A GUARD'S SANDWICH AND THE GREASE WOULD MAKE IT IMPOSSIBLE TO PAINT ON EVEN IF I WANTED TO, BUT CHARCOAL FALLEN FROM A GUARD'S BRAZIER SEEMS TO WORK FINE. + +THE OTHERS IN MY DORMITORY TELL ME TO RECORD AS MUCH AS I CAN. NONE OF US KNOW IF WE WILL LIVE TO TELL THE WORLD OF THE TERRIBLE THINGS THAT HAPPEN HERE, SO I DRAW AND, WHEN I CAN, I WRITE, ALWAYS WITH ONE EAR LISTENING FOR THE CRUNCH OF A BOOT ON THE GRAVEL OUTSIDE. THE STONES ARE SHARP AND THE SOLDIERS DON'T GIVE US FOOTWEAR. IT IS ANOTHER WAY TO HUMILIATE US AND WEAR US DOWN, BUT THEY DO NOT REALISE THAT THIS MEANS THAT WE CAN ALWAYS TELL WHEN THEY ARE COMING. KINDNESS IS TO BE FOUND EVERYWHERE HERE, EVEN IN THEIR STUPID CRUELTY. \ No newline at end of file diff --git a/cipher.py b/cipher.py index b3ab488..b07cd25 100644 --- a/cipher.py +++ b/cipher.py @@ -17,6 +17,21 @@ def letter_frequencies(message): frequencies[letter]+=1 return frequencies +def scale_freq(frequencies): + total= sum(frequencies.values()) + scaled_frequencies = collections.defaultdict(int) + for letter in frequencies.keys(): + scaled_frequencies[letter] = frequencies[letter] / total + return scaled_frequencies + +def value_diff(frequencies1, frequencies2): + total= 0 + for letter in frequencies1.keys(): + total += abs(frequencies1[letter]-frequencies2[letter]) + return total + + + def caesar_cipher_letter(letter, shift): if letter in string.ascii_letters: if letter in string.ascii_lowercase: @@ -37,3 +52,17 @@ def caesar_cipher_message(message, shift): def caesar_decipher_message(message, shift): return caesar_cipher_message(message, -shift) + +def caesar_break(message): + best_key = 0 + best_fit = float("inf") + for shift in range(26): + plaintxt = caesar_decipher_message(message, shift) + lettertxt = letter_frequencies(plaintxt) + total1 = scale_freq(lettertxt) + total2 = scale_freq(english_counts) + fit = value_diff(total2, total1) + if fit < best_fit: + best_key = shift + best_fit = fit + return best_key -- 2.34.1