In[19]:=
breaktext[m_String, key_]:= Module[ {k=StringLength[key] },
text=ToNumericForm[m];
n=Length[text];
plaintext={};
While[ n>=k,
AppendTo[plaintext,Take[text,k]];
text=Drop[text,k];
n=n-k;];
If[ 0<n<k, AppendTo[plaintext,
Flatten[AppendTo[text,Table[Random[Integer,26],
{i,1,k-n}]]]]];
Return[plaintext]]
In[20]:=
breaktext[ " abc def ghijklmop", "abcd"]
Out[20]
In[21]:=
In[22]:=
Out[22]
The cryptanalysis of the Vigenere cipher also requires a frequency analysis
of
In[23]:=
Exercise: Modify the program to count the frequency of trigrams. Trigrams are
sequences
Up to Procedural Programming
In the Vigenere cipher, we take a plaintext M and add a key word K as in
the
following example.
Suppose M= I came I saw Iconquered and the key is Egypt,
the we add the two strings
IcameIsawIconquenred
EgyptEgyptEgyptEgypt
by doing arithmetic modulo 26. For this we generate the character codes of the
two strings
and then add them using the Map command.
vigenere[M_String, K_String]:=
Module[ { l},
l=breaktext[M,K];
l=Mod[Map[ #+ToNumericForm[K] &,l],26];
l=Flatten[l]+97;
Return[FromCharacterCode[l]]]
vigenere[ "I came Isaw I conquered", "Egypt"]
digrams and trigrams, two and three letter strings of the English alphabet.
The program is
similar to the frequency analysis of the letters of the alphabet that was
given above.
The program finds all the two letter sequences in the text and counts their
frequency. The second
argument is an integer a that can be set to return only those digrams that
occur at least
a times.
digrams[cipher_,a_]:= Module[ {k=1,str,l,i,p, s={}},
i=1;
l=StringLength[cipher];
While[i< l-4 ,
str=StringTake[cipher,{i,i+1}];
p=Length[StringPosition[cipher,str]];
If[p>a,
AppendTo[s, {str,p}]];
i++];
Return[Union[s]]]
Exercise: Rewrite the program without using a While statement.
of three letters. The frequency analysis of trigrams is very useful because
"THE" is the
most common trigram in the English language.