(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 10.2' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 158, 7] NotebookDataLength[ 43895, 1189] NotebookOptionsPosition[ 39948, 1052] NotebookOutlinePosition[ 40306, 1068] CellTagsIndexPosition[ 40263, 1065] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Cryptanalysis of Affine ciphers", "Title", CellChangeTimes->{{3.65725755378201*^9, 3.6572575955064697`*^9}}], Cell[CellGroupData[{ Cell["Affine ciphers", "Section", CellChangeTimes->{{3.624033047288239*^9, 3.624033050439247*^9}, { 3.65676198981747*^9, 3.656762009483061*^9}}], Cell[TextData[{ "An affine cipher is an enchipherment scheme of the form ", Cell[BoxData[ FormBox[ RowBox[{ RowBox[{ SubscriptBox["E", RowBox[{"a", ",", "b"}]], "(", "x", ")"}], "=", RowBox[{"ax", "+", RowBox[{"b", " ", RowBox[{"(", RowBox[{"mod", " ", "26"}], ")"}]}]}]}], TraditionalForm]], FontWeight->"Bold"], " and ", Cell[BoxData[ FormBox[ StyleBox[ RowBox[{ RowBox[{ SubscriptBox["D", RowBox[{"a", ",", "b"}]], "(", "x", ")"}], "=", RowBox[{ RowBox[{ SuperscriptBox["a", RowBox[{"-", "1"}]], "(", RowBox[{"x", "-", "b"}], ")"}], " ", RowBox[{"(", RowBox[{"mod", " ", "26"}], ")"}]}]}], FontWeight->"Bold"], TraditionalForm]]], ", where a and b are integer numbers between 0 and 25, and a and 26 must be \ relatively prime. " }], "Text", CellChangeTimes->{{3.624033185868272*^9, 3.624033233076146*^9}, { 3.624033569106139*^9, 3.624033676196334*^9}, {3.624034024254312*^9, 3.624034059131054*^9}, 3.657257255555821*^9}], Cell["These are the admissible values for the first key (key a):", "Text", CellChangeTimes->{{3.624034354067368*^9, 3.6240343649438963`*^9}, { 3.624034404529327*^9, 3.624034417856174*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"GoodKeys", " ", "=", " ", RowBox[{"Select", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{"i", ",", RowBox[{"{", RowBox[{"i", ",", "25"}], "}"}]}], "]"}], ",", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"GCD", "[", RowBox[{"26", ",", "#"}], "]"}], "\[Equal]", "1"}], ")"}], "&"}]}], "]"}]}]], "Input", CellChangeTimes->{{3.624639947518792*^9, 3.624639951686612*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"{", RowBox[{ "1", ",", "3", ",", "5", ",", "7", ",", "9", ",", "11", ",", "15", ",", "17", ",", "19", ",", "21", ",", "23", ",", "25"}], "}"}]], "Output", CellChangeTimes->{3.657257240346572*^9}] }, Open ]], Cell["\<\ Note that there are exactly 12 such numbers (the Euler characteristic of 26 \ is (2 - 1) (13 - 1) = 12).\ \>", "Text", CellChangeTimes->{{3.657257265385688*^9, 3.657257337922612*^9}}], Cell[CellGroupData[{ Cell["Encoding and Decoding ", "Subsubsection", CellChangeTimes->{{3.6240347627587357`*^9, 3.6240347677907753`*^9}, { 3.62403810952796*^9, 3.6240381155590343`*^9}, {3.624199117497347*^9, 3.624199136920718*^9}, 3.624211353801586*^9}], Cell[BoxData[ RowBox[{"Clear", "[", RowBox[{"AffineEncode", ",", " ", "AffineDecode"}], "]"}]], "Input", CellChangeTimes->{{3.624038890013033*^9, 3.624038899343802*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[{ RowBox[{ RowBox[{"AffineEncode", "[", RowBox[{ RowBox[{"x_", " ", "?", "IntegerQ"}], ",", " ", "a_", ",", " ", "b_"}], "]"}], ":=", " ", RowBox[{"Mod", "[", RowBox[{ RowBox[{ RowBox[{"a", "*", "x"}], "+", "b"}], ",", "26"}], "]"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"AffineEncode", "[", RowBox[{ RowBox[{"x_", " ", "?", "StringQ"}], ",", " ", "a_", ",", " ", "b_"}], "]"}], ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"n", ",", "numerictext"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"numerictext", "=", RowBox[{ RowBox[{"ToCharacterCode", "[", RowBox[{"ToUpperCase", "[", "x", "]"}], "]"}], "-", "65"}]}], ";", "\[IndentingNewLine]", RowBox[{"numerictext", " ", "=", " ", RowBox[{"Select", "[", RowBox[{"numerictext", ",", " ", RowBox[{ RowBox[{"(", RowBox[{"0", "<=", "#", "\[LessEqual]", "25"}], ")"}], "&"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"n", " ", "=", " ", RowBox[{"Mod", "[", RowBox[{ RowBox[{ RowBox[{"a", "*", "numerictext"}], "+", "b"}], ",", "26"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"FromCharacterCode", "[", RowBox[{"n", "+", "65"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}]}], "Input", CellChangeTimes->{{3.6240257500371323`*^9, 3.6240257987200212`*^9}, { 3.624025831271524*^9, 3.624025842014024*^9}, 3.6240262221904793`*^9, { 3.624026317748263*^9, 3.6240263753323812`*^9}, {3.62403450344028*^9, 3.6240345550976458`*^9}, {3.624034679639542*^9, 3.624034696591422*^9}, { 3.6241982715951643`*^9, 3.6241982958523083`*^9}, {3.624198809385181*^9, 3.624198835769988*^9}, {3.624199059411195*^9, 3.6241990650590363`*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[{ RowBox[{ RowBox[{"AffineDecode", "[", RowBox[{ RowBox[{"x_", " ", "?", "IntegerQ"}], ",", " ", "a_", ",", " ", "b_"}], "]"}], ":=", " ", RowBox[{"Mod", "[", RowBox[{ RowBox[{ RowBox[{"PowerMod", "[", RowBox[{"a", ",", RowBox[{"-", "1"}], ",", "26"}], "]"}], "*", RowBox[{"(", RowBox[{"x", "-", "b"}], ")"}]}], ",", "26"}], "]"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"AffineDecode", "[", RowBox[{ RowBox[{"x_", " ", "?", "StringQ"}], ",", " ", "a_", ",", " ", "b_"}], "]"}], ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"n", ",", "numerictext"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"numerictext", " ", "=", " ", RowBox[{ RowBox[{"ToCharacterCode", "[", "x", "]"}], "-", "65"}]}], ";", "\[IndentingNewLine]", RowBox[{"n", " ", "=", " ", RowBox[{"Mod", "[", RowBox[{ RowBox[{ RowBox[{"PowerMod", "[", RowBox[{"a", ",", RowBox[{"-", "1"}], ",", "26"}], "]"}], "*", RowBox[{"(", RowBox[{"numerictext", "-", "b"}], ")"}]}], ",", "26"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"FromCharacterCode", "[", RowBox[{"n", "+", "65"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}]}], "Input", CellChangeTimes->{{3.624032008355279*^9, 3.624032090512413*^9}, { 3.6240386901368513`*^9, 3.6240387437183867`*^9}, {3.624038845063614*^9, 3.62403884847127*^9}, {3.6241982856280746`*^9, 3.6241982874110603`*^9}, { 3.624198840786376*^9, 3.624198869880609*^9}, {3.6241990332362328`*^9, 3.624199050444194*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"AffineEncode", "[", RowBox[{ "\"\\"", ",", " ", "17", ",", " ", "5"}], "]"}]], "Input", CellChangeTimes->{{3.6240387712289124`*^9, 3.624038805519191*^9}}, Background->GrayLevel[0.85]], Cell[BoxData["\<\"KJJTZKLTVFMMLSVNULAUVIZFIVAIVQQXVFZX\"\>"], "Output", CellChangeTimes->{3.657257364565743*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"AffineDecode", "[", RowBox[{ "\"\\"", ",", "17", ",", "5"}], "]"}]], "Input", CellChangeTimes->{{3.6240388111268253`*^9, 3.624038834583012*^9}}, Background->GrayLevel[0.85]], Cell[BoxData["\<\"LOOKSLIKEAFFINECHIPHERSAREPRETTYEASY\"\>"], "Output", CellChangeTimes->{3.657257366964732*^9}] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Associations", "Section", CellChangeTimes->{{3.6246403251253643`*^9, 3.624640327900708*^9}}], Cell[TextData[{ Cell[BoxData[ FormBox[ ButtonBox["Association", BaseStyle->"Hyperlink", ButtonData->{ URL["http://reference.wolfram.com/language/ref/Association.html"], None}, ButtonNote->"http://reference.wolfram.com/language/ref/Association.html"], TraditionalForm]], FontWeight->"Bold"], " (<|....|>) - Along with lists, ", StyleBox[ButtonBox["associations", BaseStyle->"Hyperlink", ButtonData->{ URL["http://reference.wolfram.com/language/guide/Associations.html"], None}, ButtonNote->"http://reference.wolfram.com/language/guide/Associations.html"], FontWeight->"Bold"], " are fundamental constructs in the Wolfram Language. They associate keys \ with values, allowing highly efficient lookup and updating, even with \ millions of elements. Associations provide generalizations of symbolically \ indexed lists, associative arrays, dictionaries, hashmaps, structs, and a \ variety of other powerful data structures. \n", StyleBox["Association[key->val]", FontWeight->"Bold"], " associates keys with values\n", StyleBox["Association[{k->v, b->y}]", FontWeight->"Bold"], " key k is associated with value v, key b with value y, etc. \nConvert a \ list of rules to an association using ", StyleBox["Association[List]", FontWeight->"Bold"], ". Convert an association back to list using ", StyleBox["Normal[Association]", FontWeight->"Bold"], "." }], "Text", CellChangeTimes->{{3.6242037476535273`*^9, 3.624203788862714*^9}, { 3.624203856476383*^9, 3.624203890147406*^9}, {3.6242116998201437`*^9, 3.624211705946965*^9}, {3.624211740893067*^9, 3.6242117875968733`*^9}, { 3.624212200059668*^9, 3.624212200060721*^9}, {3.624212242755248*^9, 3.6242122626648197`*^9}, {3.624212305649691*^9, 3.624212422809297*^9}}], Cell[CellGroupData[{ Cell["Numeric correspondents of alphabet letters:", "Subsubsection", CellChangeTimes->{{3.624641005365942*^9, 3.624641041109343*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"NumAlphabet", "=", RowBox[{"Association", " ", "@", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"FromCharacterCode", "[", "i", "]"}], "\[Rule]", RowBox[{"i", "-", "65"}]}], ",", RowBox[{"{", RowBox[{"i", ",", "65", ",", RowBox[{"65", "+", "25"}]}], "}"}]}], "]"}]}]}]], "Input", CellChangeTimes->{{3.624338451993761*^9, 3.62433847447567*^9}, { 3.624339024523342*^9, 3.624339046226468*^9}, {3.624641091319014*^9, 3.624641130246983*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"\[LeftAssociation]", RowBox[{ RowBox[{"\<\"A\"\>", "\[Rule]", "0"}], ",", RowBox[{"\<\"B\"\>", "\[Rule]", "1"}], ",", RowBox[{"\<\"C\"\>", "\[Rule]", "2"}], ",", RowBox[{"\<\"D\"\>", "\[Rule]", "3"}], ",", RowBox[{"\<\"E\"\>", "\[Rule]", "4"}], ",", RowBox[{"\<\"F\"\>", "\[Rule]", "5"}], ",", RowBox[{"\<\"G\"\>", "\[Rule]", "6"}], ",", RowBox[{"\<\"H\"\>", "\[Rule]", "7"}], ",", RowBox[{"\<\"I\"\>", "\[Rule]", "8"}], ",", RowBox[{"\<\"J\"\>", "\[Rule]", "9"}], ",", RowBox[{"\<\"K\"\>", "\[Rule]", "10"}], ",", RowBox[{"\<\"L\"\>", "\[Rule]", "11"}], ",", RowBox[{"\<\"M\"\>", "\[Rule]", "12"}], ",", RowBox[{"\<\"N\"\>", "\[Rule]", "13"}], ",", RowBox[{"\<\"O\"\>", "\[Rule]", "14"}], ",", RowBox[{"\<\"P\"\>", "\[Rule]", "15"}], ",", RowBox[{"\<\"Q\"\>", "\[Rule]", "16"}], ",", RowBox[{"\<\"R\"\>", "\[Rule]", "17"}], ",", RowBox[{"\<\"S\"\>", "\[Rule]", "18"}], ",", RowBox[{"\<\"T\"\>", "\[Rule]", "19"}], ",", RowBox[{"\<\"U\"\>", "\[Rule]", "20"}], ",", RowBox[{"\<\"V\"\>", "\[Rule]", "21"}], ",", RowBox[{"\<\"W\"\>", "\[Rule]", "22"}], ",", RowBox[{"\<\"X\"\>", "\[Rule]", "23"}], ",", RowBox[{"\<\"Y\"\>", "\[Rule]", "24"}], ",", RowBox[{"\<\"Z\"\>", "\[Rule]", "25"}]}], "\[RightAssociation]"}]], "Output", CellChangeTimes->{3.657256108155539*^9}] }, Open ]], Cell["\<\ If we want to obtain the frequency of letter A, we can now call TA[\"A\"] in \ place of TA[[1]].\ \>", "Text", CellChangeTimes->{{3.656323952734988*^9, 3.656324035229251*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"NumAlphabet", "[", "\"\\"", "]"}]], "Input", CellChangeTimes->{{3.624641921272399*^9, 3.62464193035847*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData["7"], "Output", CellChangeTimes->{3.657256115349862*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"NumAlphabet", "[", RowBox[{"[", "8", "]"}], "]"}]], "Input", CellChangeTimes->{{3.624641921272399*^9, 3.62464193035847*^9}, { 3.624641971254284*^9, 3.624641981547364*^9}, {3.657256132413239*^9, 3.657256133684272*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData["7"], "Output", CellChangeTimes->{3.657256134729197*^9}] }, Open ]], Cell[TextData[{ StyleBox["Keys[..]", FontWeight->"Bold"], " and ", StyleBox["Values[..]", FontWeight->"Bold"], " can be used to obtain the keys or the values of an Association." }], "Text", CellChangeTimes->{{3.656324173082464*^9, 3.6563242415305643`*^9}, 3.657256247677875*^9}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{"Keys", "[", "NumAlphabet", "]"}], "\[IndentingNewLine]", RowBox[{"Values", "[", "NumAlphabet", "]"}]}], "Input", CellChangeTimes->{{3.624212495385154*^9, 3.6242124990943623`*^9}, { 3.657256197892898*^9, 3.657256224869452*^9}}, Background->GrayLevel[0.85]], Cell[BoxData[ RowBox[{"{", RowBox[{"\<\"A\"\>", ",", "\<\"B\"\>", ",", "\<\"C\"\>", ",", "\<\"D\"\>", ",", "\<\"E\"\>", ",", "\<\"F\"\>", ",", "\<\"G\"\>", ",", "\<\"H\"\>", ",", "\<\"I\"\>", ",", "\<\"J\"\>", ",", "\<\"K\"\>", ",", "\<\"L\"\>", ",", "\<\"M\"\>", ",", "\<\"N\"\>", ",", "\<\"O\"\>", ",", "\<\"P\"\>", ",", "\<\"Q\"\>", ",", "\<\"R\"\>", ",", "\<\"S\"\>", ",", "\<\"T\"\>", ",", "\<\"U\"\>", ",", "\<\"V\"\>", ",", "\<\"W\"\>", ",", "\<\"X\"\>", ",", "\<\"Y\"\>", ",", "\<\"Z\"\>"}], "}"}]], "Output", CellChangeTimes->{ 3.657256150297235*^9, {3.65725621717733*^9, 3.657256230030677*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ "0", ",", "1", ",", "2", ",", "3", ",", "4", ",", "5", ",", "6", ",", "7", ",", "8", ",", "9", ",", "10", ",", "11", ",", "12", ",", "13", ",", "14", ",", "15", ",", "16", ",", "17", ",", "18", ",", "19", ",", "20", ",", "21", ",", "22", ",", "23", ",", "24", ",", "25"}], "}"}]], "Output", CellChangeTimes->{ 3.657256150297235*^9, {3.65725621717733*^9, 3.657256230033452*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Table of frequency of letters from the English alphabet\ \>", "Subsubsection", CellChangeTimes->{{3.6242057698996887`*^9, 3.624205787096738*^9}, { 3.62464065613348*^9, 3.6246406681728992`*^9}, 3.6572562549777317`*^9}], Cell[BoxData[ RowBox[{"T", "=", RowBox[{"{", RowBox[{ RowBox[{"\"\\"", "\[Rule]", " ", "0.0804"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", " ", "0.0154"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0306"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0399"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.1251"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0230"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0196"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0549"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0726"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0016"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0067"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0414"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0253"}], ",", RowBox[{"\"\\"", "\[Rule]", "0.0709"}], ",", RowBox[{"\"\\"", "\[Rule]", "0.0760"}], ",", RowBox[{"\"\\"", "\[Rule]", "0.0200"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0011"}], ",", RowBox[{"\"\\"", "\[Rule]", "0.0612"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0654"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0925"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0271"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0099"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0192"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0019"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0173"}], ",", " ", RowBox[{"\"\\"", "\[Rule]", "0.0009"}]}], "}"}]}]], "Input", CellChangeTimes->{{3.624205834253846*^9, 3.6242062567257833`*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"TA", "=", RowBox[{"Association", "[", "T", "]"}]}]], "Input", CellChangeTimes->{{3.6242062876797657`*^9, 3.624206296485211*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Sort", "[", "TA", "]"}]], "Input", CellChangeTimes->{{3.62421306028726*^9, 3.624213063107107*^9}}, Background->GrayLevel[0.85]], Cell[BoxData[ RowBox[{"\[LeftAssociation]", RowBox[{ RowBox[{"\<\"Z\"\>", "\[Rule]", "0.0009`"}], ",", RowBox[{"\<\"Q\"\>", "\[Rule]", "0.0011`"}], ",", RowBox[{"\<\"J\"\>", "\[Rule]", "0.0016`"}], ",", RowBox[{"\<\"X\"\>", "\[Rule]", "0.0019`"}], ",", RowBox[{"\<\"K\"\>", "\[Rule]", "0.0067`"}], ",", RowBox[{"\<\"V\"\>", "\[Rule]", "0.0099`"}], ",", RowBox[{"\<\"B\"\>", "\[Rule]", "0.0154`"}], ",", RowBox[{"\<\"Y\"\>", "\[Rule]", "0.0173`"}], ",", RowBox[{"\<\"W\"\>", "\[Rule]", "0.0192`"}], ",", RowBox[{"\<\"G\"\>", "\[Rule]", "0.0196`"}], ",", RowBox[{"\<\"P\"\>", "\[Rule]", "0.02`"}], ",", RowBox[{"\<\"F\"\>", "\[Rule]", "0.023`"}], ",", RowBox[{"\<\"M\"\>", "\[Rule]", "0.0253`"}], ",", RowBox[{"\<\"U\"\>", "\[Rule]", "0.0271`"}], ",", RowBox[{"\<\"C\"\>", "\[Rule]", "0.0306`"}], ",", RowBox[{"\<\"D\"\>", "\[Rule]", "0.0399`"}], ",", RowBox[{"\<\"L\"\>", "\[Rule]", "0.0414`"}], ",", RowBox[{"\<\"H\"\>", "\[Rule]", "0.0549`"}], ",", RowBox[{"\<\"R\"\>", "\[Rule]", "0.0612`"}], ",", RowBox[{"\<\"S\"\>", "\[Rule]", "0.0654`"}], ",", RowBox[{"\<\"N\"\>", "\[Rule]", "0.0709`"}], ",", RowBox[{"\<\"I\"\>", "\[Rule]", "0.0726`"}], ",", RowBox[{"\<\"O\"\>", "\[Rule]", "0.076`"}], ",", RowBox[{"\<\"A\"\>", "\[Rule]", "0.0804`"}], ",", RowBox[{"\<\"T\"\>", "\[Rule]", "0.0925`"}], ",", RowBox[{"\<\"E\"\>", "\[Rule]", "0.1251`"}]}], "\[RightAssociation]"}]], "Output", CellChangeTimes->{3.65725597052258*^9}] }, Open ]], Cell["\<\ The letters of the English alphabet, ordered from most frequent to least \ frequent :\ \>", "Text", CellChangeTimes->{{3.657256266910698*^9, 3.657256328375082*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"SortedLetters", "=", " ", RowBox[{"Keys", "[", RowBox[{"Sort", "[", RowBox[{"TA", ",", "Greater"}], "]"}], "]"}]}]], "Input", CellChangeTimes->{{3.6242130803081923`*^9, 3.624213086229519*^9}, { 3.624642470592166*^9, 3.624642513271162*^9}}, Background->GrayLevel[0.85]], Cell[BoxData[ RowBox[{"{", RowBox[{"\<\"E\"\>", ",", "\<\"T\"\>", ",", "\<\"A\"\>", ",", "\<\"O\"\>", ",", "\<\"I\"\>", ",", "\<\"N\"\>", ",", "\<\"S\"\>", ",", "\<\"R\"\>", ",", "\<\"H\"\>", ",", "\<\"L\"\>", ",", "\<\"D\"\>", ",", "\<\"C\"\>", ",", "\<\"U\"\>", ",", "\<\"M\"\>", ",", "\<\"F\"\>", ",", "\<\"P\"\>", ",", "\<\"G\"\>", ",", "\<\"W\"\>", ",", "\<\"Y\"\>", ",", "\<\"B\"\>", ",", "\<\"V\"\>", ",", "\<\"K\"\>", ",", "\<\"X\"\>", ",", "\<\"J\"\>", ",", "\<\"Q\"\>", ",", "\<\"Z\"\>"}], "}"}]], "Output", CellChangeTimes->{3.657255973386819*^9}] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Cryptanalysis of Monoalphabetic Ciphers", "Section", CellChangeTimes->{{3.624204989798499*^9, 3.6242050014934464`*^9}, { 3.624207996622704*^9, 3.624208000636476*^9}}], Cell["\<\ We call a monoalphabetic substitution cipher any cipher that does a \ permutation of the letters of the English alphabet. Unlike Caesar\ \[CloseCurlyQuote]s cipher or Affine ciphers, a general monoalphabetic \ substitution cipher does not have the drawback of a small key space. Indeed, \ the key space is 26! \[TildeEqual]4.03 10^26. However, a large key space does \ not guarantee that the system is secure! On the contrary, by simply counting \ the letter frequencies in the ciphertext and comparing these with the letter \ frequencies of the English alphabet, one very quickly finds the encodings of \ the most frequent letters in the plaintext. Indeed, the most frequent letter \ in the ciphertext will very likely be the encoding of one of the letters E, \ T, A, etc. After having found the encryptions of the most frequent letters in \ the plaintext, it is not difficult to fill in the rest. Of course, the longer \ the cipher text, the easier the cryptanalysis becomes.\ \>", "Text", CellChangeTimes->{{3.624204592744491*^9, 3.624204781273799*^9}, { 3.6242050113014917`*^9, 3.624205020997603*^9}, {3.657255751761545*^9, 3.6572559378834867`*^9}, {3.657255987235816*^9, 3.657256019955598*^9}}, TextJustification->1.], Cell[CellGroupData[{ Cell["Frequency analysis", "Subsubsection", CellChangeTimes->{{3.624205040953052*^9, 3.624205045417078*^9}, { 3.624205179221681*^9, 3.624205185605605*^9}}], Cell["\<\ The probability that letter \[OpenCurlyDoubleQuote]A\[CloseCurlyDoubleQuote] \ appears in an English text is approx. 0.0804. The probability that letter \ \[OpenCurlyDoubleQuote]E\[CloseCurlyDoubleQuote] appears in an English text \ is approx. 0.1251. The probability that letter \[OpenCurlyDoubleQuote]Z\ \[CloseCurlyDoubleQuote] appears in an English text is approx. 0.0009. \ Enciphering a text with a monoalphabetic cipher only permutes these \ frequencies around.\ \>", "Text", CellChangeTimes->{{3.624205059784507*^9, 3.6242050607912273`*^9}, { 3.624205121996207*^9, 3.624205235509014*^9}, {3.6242113699545603`*^9, 3.62421137496038*^9}, {3.624211484399848*^9, 3.624211485624501*^9}, { 3.624640685644902*^9, 3.624640767451808*^9}}, TextJustification->1.] }, Open ]], Cell[CellGroupData[{ Cell["\<\ A function to count the number of occurrences of letters in a text\ \>", "Subsubsection", CellChangeTimes->{{3.624640870265018*^9, 3.624640902371903*^9}, { 3.624641567461637*^9, 3.6246415743403378`*^9}, 3.70212039564213*^9}], Cell[TextData[{ "We use the ", StyleBox["Mathematica", FontSlant->"Italic"], " function ", StyleBox["StringCount[s, sub] ", FontWeight->"Bold"], "which", StyleBox[" ", FontWeight->"Bold"], "counts the occurrences of the substring sub in the string s" }], "Text", CellChangeTimes->{{3.62464158139892*^9, 3.62464161138838*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"LetterFrequencies", "[", "x_", "]"}], ":=", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"y", ",", " ", "L", ",", " ", "F"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"y", " ", "=", " ", RowBox[{"ToUpperCase", "[", "x", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"L", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"FromCharacterCode", "[", "i", "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", "65", ",", " ", RowBox[{"65", "+", "25"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"F", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", "i", "]"}], "]"}], "->", RowBox[{"StringCount", "[", RowBox[{"y", ",", RowBox[{"L", "[", RowBox[{"[", "i", "]"}], "]"}]}], "]"}]}], ",", " ", RowBox[{"{", RowBox[{"i", ",", "1", ",", "26"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Association", "[", "F", "]"}]}]}], "\[IndentingNewLine]", "]"}]}]], "Input", CellChangeTimes->{{3.624208644951823*^9, 3.624208654550441*^9}, { 3.624208749456106*^9, 3.624208775887925*^9}, {3.624208811454795*^9, 3.624208905915944*^9}, {3.6242089368054447`*^9, 3.6242089376712646`*^9}, { 3.62420905760949*^9, 3.624209071880652*^9}, {3.624209200722578*^9, 3.624209237492311*^9}, 3.6242092862939377`*^9, {3.6242093222486877`*^9, 3.624209328615868*^9}, {3.624209366958667*^9, 3.624209372195003*^9}, { 3.624209411867494*^9, 3.6242094243073063`*^9}, {3.6242099975900927`*^9, 3.624210035377469*^9}, {3.624210090836877*^9, 3.62421013286279*^9}, { 3.624210187201779*^9, 3.624210222685542*^9}, {3.62421026175854*^9, 3.624210265172633*^9}, {3.6242115979073353`*^9, 3.624211603236266*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"LetterFrequencies", "[", "\"\\"", "]"}]], "Input",\ Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"Ordered", "=", RowBox[{"Sort", "[", RowBox[{ RowBox[{"LetterFrequencies", "[", "\"\\"", "]"}], ",", " ", "Greater"}], "]"}]}]], "Input", CellChangeTimes->{{3.624208917210041*^9, 3.624208923577086*^9}, { 3.6242102307646503`*^9, 3.624210239190071*^9}, {3.624210368545308*^9, 3.62421038621911*^9}, {3.624211849919601*^9, 3.6242118653171587`*^9}, { 3.624211980137444*^9, 3.62421198479186*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"Keys", "[", "Ordered", "]"}]], "Input", CellChangeTimes->{{3.624212076798414*^9, 3.624212088638867*^9}, { 3.624212132629427*^9, 3.624212136332365*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Cryptanalysis of Affine Ciphers:", "Section", CellChangeTimes->{{3.624208044692566*^9, 3.624208054556316*^9}}], Cell[CellGroupData[{ Cell["\<\ a) By exhaustive key search, or brute-force approach (try all possible keys):\ \ \>", "Subsubsection", CellChangeTimes->{{3.624208063172361*^9, 3.624208090268035*^9}, { 3.656319060033389*^9, 3.6563190632587147`*^9}, {3.6563194536706877`*^9, 3.656319461934849*^9}, {3.6563250726923933`*^9, 3.656325073580098*^9}}], Cell["\<\ The following text \"KJJTZKLTVFMMLSVNULAUVIZFIVAIVQQXVFZX\" has been encoded \ using an Affine Cipher. Decode the text.\ \>", "Text", CellChangeTimes->{{3.6563194864647903`*^9, 3.656319492686431*^9}, 3.702120403114107*^9}] }, Open ]], Cell[CellGroupData[{ Cell["b) By the method of a known ciphertext:", "Subsubsection", CellChangeTimes->{{3.624208063172361*^9, 3.624208090268035*^9}, { 3.656763283043449*^9, 3.656763283777658*^9}}], Cell[TextData[{ StyleBox["Exercise: ", FontWeight->"Bold"], "Suppose that an affine cipher E(x) = ax+b (mod 26) enciphers \ \[OpenCurlyDoubleQuote]H\[CloseCurlyDoubleQuote] as \[OpenCurlyDoubleQuote]X\ \[CloseCurlyDoubleQuote] and \[OpenCurlyDoubleQuote]Q\[CloseCurlyDoubleQuote] \ as \[OpenCurlyDoubleQuote]Y\[CloseCurlyDoubleQuote]. Find the key. Use the \ key to decode the ciphertext \ \[OpenCurlyDoubleQuote]HXAEAEICJJOLCGPSQPVJCAPHOTHCHHCIG\ \[CloseCurlyDoubleQuote], which has been encoded using the same cipher. " }], "Text", CellChangeTimes->{{3.6242082478670588`*^9, 3.624208341271936*^9}, { 3.624208493532267*^9, 3.6242085043715477`*^9}, {3.624210781324059*^9, 3.624210791403756*^9}, {3.624211479746768*^9, 3.6242114804967012`*^9}}, TextJustification->1.], Cell[TextData[{ StyleBox["Hint: ", FontWeight->"Bold"], "Solve a system of two linear equations modulus 26." }], "Text", CellChangeTimes->{{3.6242113834663057`*^9, 3.624211433127977*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Solution : ", "Subsubsection", CellChangeTimes->{{3.624641716241548*^9, 3.624641731382698*^9}}], Cell[BoxData[{ RowBox[{"Clear", "[", RowBox[{"a", ",", "b"}], "]"}], "\[IndentingNewLine]", RowBox[{"sol", " ", "=", RowBox[{"Solve", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{"NumAlphabet", "[", "\"\\"", "]"}], "*", "a"}], "+", "b"}], " ", "\[Equal]", " ", RowBox[{"NumAlphabet", "[", "\"\\"", "]"}]}], ",", " ", RowBox[{ RowBox[{ RowBox[{ RowBox[{"NumAlphabet", "[", "\"\\"", "]"}], "*", "a"}], "+", "b"}], " ", "\[Equal]", " ", RowBox[{"NumAlphabet", "[", "\"\\"", "]"}]}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Modulus", "\[Rule]", "26"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input", CellChangeTimes->{{3.6246417492005043`*^9, 3.624641856187418*^9}, { 3.624641887899272*^9, 3.624641898835864*^9}, {3.624642035754902*^9, 3.624642061499239*^9}, {3.6246421502561083`*^9, 3.624642151678978*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[{ RowBox[{"ciphertext1", " ", "=", " ", "\"\\""}], "\[IndentingNewLine]", RowBox[{"AffineDecode", "[", RowBox[{"ciphertext1", ",", " ", RowBox[{"a", " ", "/.", " ", RowBox[{"Flatten", "[", "sol", "]"}]}], ",", " ", RowBox[{"b", " ", "/.", " ", RowBox[{"Flatten", "[", "sol", "]"}]}]}], "]"}]}], "Input", CellChangeTimes->{{3.624642102630653*^9, 3.624642146814795*^9}, { 3.6246422380468197`*^9, 3.6246422519498777`*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell[CellGroupData[{ Cell["c) By frequency analysis:", "Subsubsection", CellChangeTimes->{{3.624208081883882*^9, 3.6242080879403677`*^9}, { 3.624208930116665*^9, 3.6242089342847433`*^9}, {3.656763287447015*^9, 3.656763288238723*^9}}], Cell[TextData[{ StyleBox["Exercise:", FontWeight->"Bold"], " Suppose that you have intercepted the following ciphertext. You know that \ it has been encoded using an affine cipher, but you don\[CloseCurlyQuote] t \ know the key. Use frequency analysis to break the cipher. ", StyleBox["\[OpenCurlyDoubleQuote]\ QNRVKVITSTIJVLVFPUTHVONVPGNWZVTGWQTWWQVPFPWVZXPPVHDIVNGWQVHNGWITIFAFPXZUSFHNDG\ WXGJWQVSVWWVICIVBDVGHXVPXGWQVHXUQVIWVYWTGOHNZUTIXGJWQVPVRXWQWQVSVWWVICIVBDVGHX\ VPNCWQVVGJSXPQTSUQTAVWNGVKVIFBDXHLSFCXGOPWQVXZTJVPDGOVIWQVUVIZDWTWXNGUNCWQVZNP\ WCIVBDVGWSVWWVIPXGWQVUSTXGWVYW\[CloseCurlyDoubleQuote].", Background->RGBColor[1, 1, 0.85]], " " }], "Text", CellChangeTimes->{{3.624209715352704*^9, 3.624209789136374*^9}, { 3.6242128806216297`*^9, 3.624212893583449*^9}, {3.6246422811499767`*^9, 3.624642300726613*^9}, 3.6572556069860277`*^9}] }, Open ]], Cell[CellGroupData[{ Cell["Solution : ", "Subsubsection", CellChangeTimes->{{3.624641716241548*^9, 3.624641731382698*^9}}], Cell["\<\ First we need to find out the most frequent letters in the ciphertext. Since \ a monoalphabetic substitution cipher was used, we expect that the most \ frequent letters in the ciphertext are encodings of the most frequent letters \ in the English alphabet. So we can use this information to break the cipher!\ \>", "Text", CellChangeTimes->{{3.656331536593176*^9, 3.656331574891893*^9}, 3.6563316419788647`*^9, {3.656332774401116*^9, 3.656332790410664*^9}, { 3.657255581378117*^9, 3.6572555908831463`*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{ RowBox[{ "ciphertext3", " ", "=", " ", "\"\\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"S", "=", RowBox[{"Sort", "[", RowBox[{ RowBox[{"LetterFrequencies", "[", "ciphertext3", "]"}], ",", " ", "Greater"}], "]"}]}], "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{ RowBox[{"Letters", " ", "in", " ", "the", " ", "ciphertext"}], ",", " ", RowBox[{ "from", " ", "most", " ", "frequent", " ", "to", " ", "least", " ", "frequent"}]}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"CipherLetters", "=", RowBox[{"Keys", "[", "S", "]"}]}], "\[IndentingNewLine]", RowBox[{"(*", " ", RowBox[{ RowBox[{ "The", " ", "letters", " ", "of", " ", "the", " ", "English", " ", "alphabet"}], ",", RowBox[{ "ordered", " ", "from", " ", "most", " ", "frequent", " ", "to", " ", "least", " ", "frequent"}]}], " ", "*)"}]}], "\[IndentingNewLine]", RowBox[{"SortedLetters", " "}]}], "Input", CellChangeTimes->{{3.65725644839965*^9, 3.65725649336143*^9}, { 3.6572565880070868`*^9, 3.6572566232653847`*^9}, {3.657256673280219*^9, 3.657256776098247*^9}, 3.657257524354167*^9}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"\[LeftAssociation]", RowBox[{ RowBox[{"\<\"V\"\>", "\[Rule]", "46"}], ",", RowBox[{"\<\"W\"\>", "\[Rule]", "33"}], ",", RowBox[{"\<\"G\"\>", "\[Rule]", "19"}], ",", RowBox[{"\<\"Q\"\>", "\[Rule]", "17"}], ",", RowBox[{"\<\"X\"\>", "\[Rule]", "16"}], ",", RowBox[{"\<\"I\"\>", "\[Rule]", "16"}], ",", RowBox[{"\<\"P\"\>", "\[Rule]", "15"}], ",", RowBox[{"\<\"T\"\>", "\[Rule]", "13"}], ",", RowBox[{"\<\"N\"\>", "\[Rule]", "12"}], ",", RowBox[{"\<\"S\"\>", "\[Rule]", "9"}], ",", RowBox[{"\<\"H\"\>", "\[Rule]", "9"}], ",", RowBox[{"\<\"U\"\>", "\[Rule]", "8"}], ",", RowBox[{"\<\"D\"\>", "\[Rule]", "8"}], ",", RowBox[{"\<\"Z\"\>", "\[Rule]", "7"}], ",", RowBox[{"\<\"F\"\>", "\[Rule]", "7"}], ",", RowBox[{"\<\"C\"\>", "\[Rule]", "6"}], ",", RowBox[{"\<\"J\"\>", "\[Rule]", "5"}], ",", RowBox[{"\<\"O\"\>", "\[Rule]", "4"}], ",", RowBox[{"\<\"B\"\>", "\[Rule]", "4"}], ",", RowBox[{"\<\"Y\"\>", "\[Rule]", "2"}], ",", RowBox[{"\<\"R\"\>", "\[Rule]", "2"}], ",", RowBox[{"\<\"L\"\>", "\[Rule]", "2"}], ",", RowBox[{"\<\"K\"\>", "\[Rule]", "2"}], ",", RowBox[{"\<\"A\"\>", "\[Rule]", "2"}], ",", RowBox[{"\<\"M\"\>", "\[Rule]", "0"}], ",", RowBox[{"\<\"E\"\>", "\[Rule]", "0"}]}], "\[RightAssociation]"}]], "Output",\ CellChangeTimes->{ 3.657256522269596*^9, 3.657256606869822*^9, {3.657256750960198*^9, 3.6572567770204973`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"\<\"V\"\>", ",", "\<\"W\"\>", ",", "\<\"G\"\>", ",", "\<\"Q\"\>", ",", "\<\"X\"\>", ",", "\<\"I\"\>", ",", "\<\"P\"\>", ",", "\<\"T\"\>", ",", "\<\"N\"\>", ",", "\<\"S\"\>", ",", "\<\"H\"\>", ",", "\<\"U\"\>", ",", "\<\"D\"\>", ",", "\<\"Z\"\>", ",", "\<\"F\"\>", ",", "\<\"C\"\>", ",", "\<\"J\"\>", ",", "\<\"O\"\>", ",", "\<\"B\"\>", ",", "\<\"Y\"\>", ",", "\<\"R\"\>", ",", "\<\"L\"\>", ",", "\<\"K\"\>", ",", "\<\"A\"\>", ",", "\<\"M\"\>", ",", "\<\"E\"\>"}], "}"}]], "Output", CellChangeTimes->{ 3.657256522269596*^9, 3.657256606869822*^9, {3.657256750960198*^9, 3.657256777023834*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{"\<\"E\"\>", ",", "\<\"T\"\>", ",", "\<\"A\"\>", ",", "\<\"O\"\>", ",", "\<\"I\"\>", ",", "\<\"N\"\>", ",", "\<\"S\"\>", ",", "\<\"R\"\>", ",", "\<\"H\"\>", ",", "\<\"L\"\>", ",", "\<\"D\"\>", ",", "\<\"C\"\>", ",", "\<\"U\"\>", ",", "\<\"M\"\>", ",", "\<\"F\"\>", ",", "\<\"P\"\>", ",", "\<\"G\"\>", ",", "\<\"W\"\>", ",", "\<\"Y\"\>", ",", "\<\"B\"\>", ",", "\<\"V\"\>", ",", "\<\"K\"\>", ",", "\<\"X\"\>", ",", "\<\"J\"\>", ",", "\<\"Q\"\>", ",", "\<\"Z\"\>"}], "}"}]], "Output", CellChangeTimes->{ 3.657256522269596*^9, 3.657256606869822*^9, {3.657256750960198*^9, 3.657256777025679*^9}}] }, Open ]], Cell["\<\ From exercise b) we know that it is enough to decipher two ciphertext letters \ to break the entire Affine cipher. Using frequency analysis, we infer that \ most likely letter V is an encoding of letter E, and letter W is an encoding \ of letter T. We try to break the affine cipher using the method outlined in \ exercise b). If we get something that makes sense, it means that we have \ broken the cipher. Otherwise, we continue guessing, and assume that for \ instance, the ciphertext letter V is an encoding for E, and ciphertext \ letter W is an encoding for A. \ \>", "Text", CellChangeTimes->{{3.657256787493046*^9, 3.657256997677267*^9}, { 3.657257028805929*^9, 3.657257106607917*^9}}, TextJustification->1.], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Solve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{ RowBox[{"4", "a"}], "+", "b"}], "\[Equal]", "21"}], ",", " ", RowBox[{ RowBox[{ RowBox[{"19", "a"}], "+", "b"}], "\[Equal]", "22"}]}], "}"}], ",", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], ",", " ", RowBox[{"Modulus", "\[Rule]", "26"}]}], "]"}]], "Input", Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"{", RowBox[{"{", RowBox[{ RowBox[{"a", "\[Rule]", "7"}], ",", RowBox[{"b", "\[Rule]", "19"}]}], "}"}], "}"}]], "Output", CellChangeTimes->{3.624642573514408*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{ RowBox[{"Clear", "[", RowBox[{"a", ",", "b"}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"sol2", "=", " ", RowBox[{"Solve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{"NumAlphabet", "[", RowBox[{"SortedLetters", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}], "*", "a"}], "+", "b"}], "\[Equal]", RowBox[{"NumAlphabet", "[", RowBox[{"CipherLetters", "[", RowBox[{"[", "1", "]"}], "]"}], "]"}]}], ",", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ RowBox[{"NumAlphabet", "[", RowBox[{"SortedLetters", "[", RowBox[{"[", "2", "]"}], "]"}], "]"}], "*", "a"}], "+", "b"}], "\[Equal]", " ", RowBox[{"NumAlphabet", "[", RowBox[{"CipherLetters", "[", RowBox[{"[", "2", "]"}], "]"}], "]"}]}]}], "}"}], ",", RowBox[{"{", RowBox[{"a", ",", "b"}], "}"}], ",", " ", RowBox[{"Modulus", "\[Rule]", "26"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", ",", RowBox[{"Flatten", "[", "sol2", "]"}], ",", " ", "\"\< and the decoded message is:\>\""}], "]"}], "\[IndentingNewLine]", RowBox[{"AffineDecode", "[", RowBox[{"ciphertext3", ",", " ", RowBox[{"a", " ", "/.", " ", RowBox[{"Flatten", "[", "sol2", "]"}]}], ",", " ", RowBox[{"b", " ", "/.", " ", RowBox[{"Flatten", "[", "sol2", "]"}]}]}], "]"}]}], "Input", CellChangeTimes->{{3.624642576220388*^9, 3.624642675943264*^9}, { 3.624642709489616*^9, 3.6246427235237513`*^9}, {3.624642773481056*^9, 3.624642775784275*^9}, 3.657257191310392*^9, 3.6572572302301607`*^9, { 3.657257403775591*^9, 3.6572575020402327`*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ InterpretationBox[ RowBox[{"\<\"We have broken the cipher \"\>", "\[InvisibleSpace]", RowBox[{"{", RowBox[{ RowBox[{"a", "\[Rule]", "7"}], ",", RowBox[{"b", "\[Rule]", "19"}]}], "}"}], "\[InvisibleSpace]", "\<\" and the decoded message is:\"\>"}], SequenceForm[ "We have broken the cipher ", {$CellContext`a -> 7, $CellContext`b -> 19}, " and the decoded message is:"], Editable->False]], "Print", CellChangeTimes->{{3.657257439588427*^9, 3.657257468462037*^9}, { 3.657257498704856*^9, 3.65725750269165*^9}}], Cell[BoxData["\<\"\ HOWEVERALARGEKEYSPACEDOESNOTMEANTHATTHESYSTEMISSECUREONTHECONTRARYBYSIMPLYCOUN\ TINGTHELETTERFREQUENCIESINTHECIPHERTEXTANDCOMPARINGTHESEWITHTHELETTERFREQUENCI\ ESOFTHEENGLISHALPHABETONEVERYQUICKLYFINDSTHEIMAGESUNDERTHEPERMUTATIONPOFTHEMOS\ TFREQUENTLETTERSINTHEPLAINTEXT\"\>"], "Output", CellChangeTimes->{{3.6572573802748737`*^9, 3.657257405245089*^9}, { 3.657257439590252*^9, 3.6572574684641037`*^9}, {3.657257498707673*^9, 3.657257502694024*^9}}] }, Open ]] }, Open ]] }, Open ]] }, Open ]] }, WindowSize->{808, 571}, WindowMargins->{{73, Automatic}, {Automatic, 0}}, FrontEndVersion->"11.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (September \ 21, 2016)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[580, 22, 115, 1, 92, "Title"], Cell[CellGroupData[{ Cell[720, 27, 147, 2, 64, "Section"], Cell[870, 31, 1068, 34, 76, "Text"], Cell[1941, 67, 191, 2, 30, "Text"], Cell[CellGroupData[{ Cell[2157, 73, 492, 15, 48, "Input"], Cell[2652, 90, 229, 5, 32, "Output"] }, Open ]], Cell[2896, 98, 194, 4, 30, "Text"], Cell[CellGroupData[{ Cell[3115, 106, 240, 3, 35, "Subsubsection"], Cell[3358, 111, 212, 4, 48, "Input"], Cell[3573, 117, 1908, 49, 185, "Input"], Cell[5484, 168, 1724, 47, 154, "Input"], Cell[CellGroupData[{ Cell[7233, 219, 263, 6, 48, "Input"], Cell[7499, 227, 113, 1, 32, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[7649, 233, 246, 6, 48, "Input"], Cell[7898, 241, 113, 1, 32, "Output"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[8072, 249, 99, 1, 64, "Section"], Cell[8174, 252, 1784, 41, 165, "Text"], Cell[CellGroupData[{ Cell[9983, 297, 134, 1, 35, "Subsubsection"], Cell[CellGroupData[{ Cell[10142, 302, 578, 15, 50, "Input"], Cell[10723, 319, 1397, 30, 75, "Output"] }, Open ]], Cell[12135, 352, 186, 4, 30, "Text"], Cell[CellGroupData[{ Cell[12346, 360, 175, 3, 48, "Input"], Cell[12524, 365, 70, 1, 32, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[12631, 371, 288, 6, 48, "Input"], Cell[12922, 379, 70, 1, 32, "Output"] }, Open ]], Cell[13007, 383, 291, 9, 30, "Text"], Cell[CellGroupData[{ Cell[13323, 396, 286, 5, 70, "Input"], Cell[13612, 403, 635, 10, 32, "Output"], Cell[14250, 415, 434, 8, 54, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[14733, 429, 232, 5, 59, "Subsubsection"], Cell[14968, 436, 1701, 31, 133, "Input"], Cell[16672, 469, 194, 4, 48, "Input"], Cell[CellGroupData[{ Cell[16891, 477, 153, 3, 48, "Input"], Cell[17047, 482, 1532, 30, 96, "Output"] }, Open ]], Cell[18594, 515, 175, 4, 30, "Text"], Cell[CellGroupData[{ Cell[18794, 523, 308, 7, 48, "Input"], Cell[19105, 532, 587, 9, 32, "Output"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[19753, 548, 175, 2, 64, "Section"], Cell[19931, 552, 1239, 18, 182, "Text"], Cell[CellGroupData[{ Cell[21195, 574, 158, 2, 35, "Subsubsection"], Cell[21356, 578, 778, 13, 68, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[22171, 596, 238, 4, 35, "Subsubsection"], Cell[22412, 602, 339, 12, 49, "Text"], Cell[22754, 616, 1974, 44, 154, "Input"], Cell[24731, 662, 133, 3, 48, "Input"], Cell[24867, 667, 494, 10, 48, "Input"], Cell[25364, 679, 216, 4, 48, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[25629, 689, 117, 1, 64, "Section"], Cell[CellGroupData[{ Cell[25771, 694, 328, 6, 35, "Subsubsection"], Cell[26102, 702, 237, 5, 49, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[26376, 712, 179, 2, 35, "Subsubsection"], Cell[26558, 716, 778, 14, 68, "Text"], Cell[27339, 732, 192, 5, 30, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[27568, 742, 102, 1, 35, "Subsubsection"], Cell[27673, 745, 1132, 28, 175, "Input"], Cell[28808, 775, 536, 11, 70, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[29381, 791, 218, 3, 35, "Subsubsection"], Cell[29602, 796, 861, 16, 125, "Text"] }, Open ]], Cell[CellGroupData[{ Cell[30500, 817, 102, 1, 35, "Subsubsection"], Cell[30605, 820, 524, 8, 68, "Text"], Cell[CellGroupData[{ Cell[31154, 832, 1519, 35, 259, "Input"], Cell[32676, 869, 1466, 32, 54, "Output"], Cell[34145, 903, 662, 11, 32, "Output"], Cell[34810, 916, 662, 11, 32, "Output"] }, Open ]], Cell[35487, 930, 732, 12, 125, "Text"], Cell[CellGroupData[{ Cell[36244, 946, 443, 14, 48, "Input"], Cell[36690, 962, 202, 6, 32, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[36929, 973, 1924, 49, 175, "Input"], Cell[38856, 1024, 562, 13, 24, "Print"], Cell[39421, 1039, 475, 7, 96, "Output"] }, Open ]] }, Open ]] }, Open ]] }, Open ]] } ] *)