Manipulate[Module[{chartOptions,plaintext,ciphertext,kwString,ptally,ctally},chartOptions=Sequence[AxesLabel{None,"frequency(%)"},PlotRange{Automatic,{0,14}},Axes{False,True},ImageSize{550,125},AspectRatio.20,LabelingFunction(Placed[Style[#3〚2,1〛,12,Bold],Center]&),Epilog{Red,Dashed,Line[{{0,3.85},{32,3.85}}]}];plaintext=StringReplace[ToLowerCase[message],CharacterRange["!","@"]..|" """];lpt=StringLength[plaintext];kwString=StringTake[NestWhile[#<>#&,ToLowerCase[Switch[cipher,1,StringTake[allChars,{offset+1}],2,keyword,3,keyword<>plaintext]],StringLength[#]≤lpt&],lpt];ciphertext=encrypt[plaintext,kwString];{ptally,ctally}=SortBy[#,-Last[#]&]&/@Tally/@Characters[{plaintext,ciphertext}];Column[{Panel[StringTake[message,75],"original message"],Panel[BarChart[100ptally〚All,2〛/lpt,ChartLabelsptally〚All,1〛,chartOptions]],Panel[StringTake[ciphertext,70],"encrypted message"],Panel[BarChart[100ctally〚All,2〛/lpt,ChartLabelsctally〚All,1〛,chartOptions]]}]],{{message,StringTake[ExampleData[{"Text","DeclarationOfIndependence"}],5000]},texts,PopupMenu},{{cipher,2},ciphers,PopupMenu},Row[{Control[{{keyword,"united"},keys,PopupMenu,Enabled(cipher>1)}],Spacer[10],Control[{{offset,13},0,25,1,Slider,ImageSizeSmall,Appearance"Labeled",Enabled(cipher==1)}]}],TrackedSymbolsTrue,SynchronousUpdatingFalse,Initialization(titles={"DeclarationOfIndependence","JFKInaugural","OriginOfSpecies","UNHumanRightsEnglish","DonQuixoteIEnglish"};texts=Join[Thread[StringTake[ExampleData[{"Text",#}],5000]&/@titlestitles]];allChars="abcdefghijklmnopqrstuvwxyz";keys={"x","yes","united","pelargoniums",allChars};ciphers=Thread[Range[3]->{"Caesar","De Vigenere","Autokey"}];encrypt[message_,keyword_]:=FromCharacterCode[Mod[ToCharacterCode[message]-97+ToCharacterCode[keyword]-97,26]+97])]
This Demonstration shows the effect of keyword length on the letter frequency of an encrypted message.
Letter frequency analysis is a common tool used for decryption. Ordered by their frequency, the letters in English text are: 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. The efficiency of a cipher can be rated by its ability to change this "normal" order.
Monoalphabetic ciphers (like the Caesar cipher) merely shift the order of the letters but do not change the frequency distribution.
Polyalphabetic ciphers (like the De Vigenere cipher or the Autokey cipher) use a keyword. The longer the keyword, the flatter the frequency distribution and the harder the decryption.