第三章一般習題

page 79

3.1.1. 凱撒要安排與安東尼的一個秘密會議，地點在 the Timber (the river) 或 the Coliseum (the arena)。他送出密文 EVIRE。然而，安東尼不知道解密鑰匙，所以他試所有的可能性。請問他該在何處與凱撒碰面？

E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A   B   C   D
V   W   X   Y   Z   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   
I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A   B   C   D   E   F   G   H  
R   S   T   U   V   W   X   Y   Z   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   
E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W   X   Y   Z   A   B   C   D

3.1.2. 經位移密碼器加密後得 KDDKMU, 試所有的可能性來解密之。

K     L     M     N     O     P     Q     R     S     T     U     V     W     X     Y     Z     A     B     C     D     E     F     G     H     I     J
D     E     F     G     H     I     J     K     L     M     N     O     P     Q     R     S     T     U     V     W     X     Y     Z     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     A     B     C
K     L     M     N     O     P     Q     R     S     T     U     V     W     X     Y     Z     A     B     C     D     E     F     G     H     I     J
M     N     O     P     Q     R     S     T     U     V     W     X     Y     Z     A     B     C     D     E     F     G     H     I     J     K     L     
U     V     W     X     Y     Z     A     B     C     D     E     F     G     H     I     J     K     L     M     N     O     P     Q     R     S     T

3.1.3. 使用仿射函數 5x+7 mod{26} 來加密信息 i love you more than i can say, 解密函數為何呢？試驗證之。

abc = "abcdefghijklmnopqrstuvwxyz";
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
affencryptkey[m_, n_] := 
        Table[StringTake[abc, {i}] -> 
            StringTake[cap, {Mod[m*(i - 1) + n, 26] + 1}], {i, 
            26}];
affencrypt[plaintext_, m_, n_] := 
        StringReplace[plaintext, affencryptkey[m, n]];
affdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[abc, {Mod[m*(i - 1) + n, 26] + 1}], {i, 
            26}];
affdecrypt[plaintext_, m_, n_] := 
        StringReplace[plaintext, affdecryptkey[m, n]];

affencrypt["iloveyoumorethanicansay", 5, 7]

affdecrypt[%, 21, 9]

3.1.4. 考慮在模 26 之下的仿射密碼。你用選擇明文攻擊法來破解，明文 hahaha 所對應的密文為 NONONO。試決定其加密函數。

\(7\ alp + bet = 13, \ 
    0\ alp + bet = \(14\ mod\ 26\ 解之得\ bet = 
        14\), \ 故\ alp = \(7^{\(-1\)}*\((\(-1\))\) = \(\(-15\) = 
          11\ mod\ 26\)\)\)
\(加密函數為\ y = \(\(11  
          x\)\(+\)\(14\)\(\ \)\)\)

3.1.5. 密文 CRWWZ 是由模 26 之下的仿射密碼所加密而得。若明文以 ha 起頭，試破解之。

\(2\ alp' + bet' = 7, \ 
    17\ alp' + 
        bet' = \(0\ mod\ 26\ 解之得\ 15 
           alp' = \(-7\)\), \ 故\ alp' = \(15^{\(-1\)}*\((\(-7\))\) = \(\(-49\) = \(3\ 因而\ bet' = 
            1\ mod\ 26\)\)\)\)
\(解密函數為\ x = \(\(3  
          y\)\(+\)\(1\)\(\ \)\)\)

abc = "abcdefghijklmnopqrstuvwxyz";
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
affdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[abc, {Mod[m*(i - 1) + n, 26] + 1}], {i, 
            26}];
affdecrypt[plaintext_, m_, n_] := 
        StringReplace[plaintext, affdecryptkey[m, n]];

affdecrypt["CRWWZ", 3, 1]

3.1.8. 假設你擁有一種僅僅具三個字母 a, b, c 的語言，而其出現的頻率分別為 0.7, 0.2, 0.1。下面密文是由維吉內爾密碼器加密所得(位移數當然是在模 3 之下進行，而非模
26)：CAAABBCACBCABACAABCCCACA。試證有可能鑰匙長度為2, 並決定最有可能的鑰匙。

cipher = "CAAABBCACBCABACAABCCCACA";
keymax = 4; 
    cipherlength = StringLength[cipher];
coinci[j_] := 
        Count[Table[
            If[StringTake[cipher, {i}] == StringTake[cipher, {i + j}], 
              t, {}], {i, cipherlength - j}], t];
coinci = Table[coinci[j], {j, keymax}];
keylength = Position[coinci, 
            Max[coinci]][[1]][[1]]

cap1 = "ABC";
frequency[txt_] := 
        Array[Function[
            i, {Characters[cap1][[i]], 
              Count[Characters[txt], Characters[cap1][[i]]]}], 
          3];
choose[txt_, m_, n_] := 
        StringJoin[
          Module[{z}, 
            z = {}; 
Do[
              z = Insert[z, StringTake[txt, {m*(i - 1) + n}], i], {i, 1, 
                Floor[StringLength[txt]/m]}]; z]];
alfreq1 = { .7,  .2,  .1};
s[a_, b_, n_] := 
        Sum[a[[i]]*b[[Mod[i - n - 1, 3] + 1]], {i, 3}];
corr[a_] := Table[s[a, alfreq1, i], {i, 0, 2}];
v[j_] := 
        Table[frequency[
                choose[cipher, keylength, j]][[i]][[2]], {i, 
            3}];
key = 
      Table[Position[corr[v[j]], 
                Max[corr[v[j]]]][[1]][[1]], {j, keylength}] - 
        1

abc1 = "abc";
cap1 = "ABC";
vigdecryptkey1[m_, n_] := 
        Table[StringTake[cap1, {i}] -> 
            StringTake[
              abc1, {Mod[i - 1 + m[[Mod[n - 1, Length[m]] + 1]], 3] + 
                  1}], {i, 1, 3}];
vigdecrypt1[plaintext_, v_] := 
        StringJoin[
          Module[{z}, z = {}; 
            Do[z = Insert[z, 
                  StringReplace[Characters[plaintext][[i]], 
                    vigdecryptkey1[v, Mod[i - 1, Length[v]] + 1]], i], {i, 
                StringLength[plaintext]}]; z]];
done = vigdecrypt1[cipher, -key]

第三章電腦習題

pages 74 - 75

3.2.1.下面密文是由位移密碼器所得：LCLLEWLJAZLNNZMVYIYLHRMHZA
試用頻率分析來猜測此密文所用的鑰匙，並用 Mathematica 來測試你的猜測。請問解密之後的明文為何？

cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
frequency[txt_] := 
        Array[Function[
            i, {Characters[cap][[i]], 
              Count[Characters[txt], Characters[cap][[i]]]}], 
          26];

lcl = "LCLLEWLJAZLNNZMVYIYLHRMHZA";

frequency[lcl]

Guess L=e-- > key = L - e = 11 - 4 = 7

abc = "abcdefghijklmnopqrstuvwxyz";
shiftdecryptkey[n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[abc, {Mod[i - 1 + n, 26] + 1}], {i, 1, 
            26}];
shiftdecrypt[plaintext_, n_] := 
        StringReplace[plaintext, shiftdecryptkey[n]];

shiftdecrypt[lcl, -7]

3.2.2. 下面密文是由仿射密碼器所得 (此密文以 ed 之名儲藏於 In[13]) ：
EDSGICKXHUKLZVEQZVKXWKZUKCVUH 。已知明文的前兩個字為 if, 試破解之。

\(4  alp' + bet' = 8, \ 3  alp' + bet' = \(\(5\ --\) > \ alp' = 3\), \ 
    bet' = \(\(-4\) = \(22\ 解密函數為\ x = 
          3  y + 22\)\)\)

affdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[abc, {Mod[m*(i - 1) + n, 26] + 1}], {i, 
            26}];
affdecrypt[plaintext_, m_, n_] := 
        StringReplace[plaintext, 
          affdecryptkey[m, n]];
ed = "EDSGICKXHUKLZVEQZVKXWKZUKCVUH";

affdecrypt[ed, 3, 22]

3.2.3. 下面密文是由仿射函數 3x+b 加密所得，此處 b 為某一整數：
TCABTIQMFHEQQMRMVMTMAQ, 試破解之(此密文以 tc 之名儲藏於 In[13])。

tc = "TCABTIQMFHEQQMRMVMTMAQ";

Table[affdecrypt[tc, 9, c], {c, 25}]

3.2.4.
維吉內爾密碼破解演算法

\(TraditionalForm\`0. \ 輸入密文並給予名稱\)

\(\(cipher = "YIHXZCWKYNTKSMOYAELAZLUTUVUZYUGNLRAULBANHBYIEZOOVMBVYJOVMCAKUTIGYJTHTQHHPUUCMBPRJPNTBVRXAYIGXQKKIKTNRJAOBAYCXVPWLCGOVNBAUGGSLUXHNXLCZXUGKKEYNUYTVWEXUEKLNOTARJPNIZLRGACQOCYCHRBXMGOUGEAYGNLRBAUGTHTQHHBXHNGGUGOVNAHWBTJEQOYQGUDAHXRJPCIMYQIHNTHHTKUDCKYJKHRMFYGUUAOKYNZIABMFRLPETWISZOABPUECLHIOYPUTEBHXRJPCIMYNVVRBBIAUMTPTNSOLLLTMNLPNIELRYAIVZJYGJENHLGNVSMPBBNLRMZUIKAHMBLYOCEAMBNZAHIMHNZPOVFCTNALQOYVZPSIENBMLTPXLSOATQGANTKPZHJRXAHIMQRYOOCEXQUAHQLVHZPNIEUEMLRAXHFKDEKTHAUADMWCPGAEEXWNTUOBVIAYLCZTNRCLCIGHBZOATEIJZOIAZLBAUDBAYOXHVMFYARPVQGANTKDMTXJNVSBKOTMSELAYEKOADXWBTZEKKUGKKIBYUEGIODXIHXWOWKJBCLRBHUQJVRLXNEGJTBAYJUYLLPCYRSIBMFRTVTMGIERVNOKYZKTBMKQUGAWMLULNLRMUOGOACIGHRBLRNHLTKAWPTNGNLYLBXUKYEQMCFLVRCLNUKSIDBHTXHTPXLGUIELXXVIHTMWBRXLTWMBRAUFQGCFNLDEHLXCOIKANUKFWPHZBANHBAYEKOADXNUAZFIKMBTVBTRUQBHNKXXVZPSZTNUKYFWKOFZVBMAYEKKELBWNZLDBHNUKNRMTNGGZKZXGNOUIVZVRLVRMNMGNHTNKIZZOEAXBBTVRMWXRGKWMMUXKPNKKYNYLDLXPBZPOVMIGNHTKTOFKMOZPBVIOTPXSTGCEBAYYGZTNNFYSLAANLRUMDMOIGOVNBAUGCLHMKYUONHTRLRYVLDXNUGATPXMRJLALLBNRSNWMBNBLDQXXVTCAQGNUGATPBMAGAIWGOAJLROHXFNHLTAUIKHNMPVVXAHWYZEKLDWFUAJAHIMABBLRVFYAZVFBAYCKVPTXVLZOEXXICRLFWKNUKWEWIFRYOATEHBZWEZBMULYOUMBRKHRBA";\)\)

\(TraditionalForm\`1. \ 求出鑰匙長度\ keylength\ 假設其最大值\ keymax\ 為某一整數如\ 9\)

keymax = 9; 
    cipherlength = StringLength[cipher];
coincidence[j_] := 
        Count[Table[
            If[StringTake[cipher, {i}] == StringTake[cipher, {i + j}], 
              t, {}], {i, 1, cipherlength - j}], 
          t];
coincidence = 
        Table[coincidence[j], {j, keymax}];
keylength = Position[coincidence, 
            Max[coincidence]][[1]][[1]]

2. 輸入 frequency,choose 並定義 corr,v 來算出鑰匙 key

cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
frequency[txt_] := 
        Array[Function[
            i, {Characters[cap][[i]], 
              Count[Characters[txt], Characters[cap][[i]]]}], 
          26];
choose[txt_, m_, n_] := 
        StringJoin[
          Module[{z}, 
            z = {}; 
Do[
              z = Insert[z, StringTake[txt, {m*(i - 1) + n}], i], {i, 1, 
                Floor[StringLength[txt]/m]}]; 
            z]];
alfreq = { .082,  .015,  .028,  .043,  .127,  .022,  .020,  .061,  .070,  .002,  .008,  .040,  .024,  .067,  .075,  .019,  .001,  .060,  .063,  .091,  .028,  .010,  .023,  .001,  .020,  .001};
s[a_, b_, n_] := 
        Sum[a[[i]]*b[[Mod[i - n - 1, 26] + 1]], {i, 26}];
corr[a_] := 
        Table[s[a, alfreq, i], {i, 0, 25}];
v[j_] := 
        Table[frequency[
                choose[cipher, keylength, j]][[i]][[2]], {i, 
            26}];
key = 
      Table[Position[corr[v[j]], 
                Max[corr[v[j]]]][[1]][[1]], {j, keylength}] - 
        1

3.輸入 vigdecrypt 並破解之

abc = "abcdefghijklmnopqrstuvwxyz";
vigdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[
              abc, {Mod[i - 1 + m[[Mod[n - 1, Length[m]] + 1]], 26] + 
                  1}], {i, 1, 26}];
vigdecrypt[plaintext_, v_] := 
        StringJoin[
          Module[{z}, z = {}; 
            Do[z = Insert[z, 
                  StringReplace[Characters[plaintext][[i]], 
                    vigdecryptkey[v, Mod[i - 1, Length[v]] + 1]], i], {i, 
                StringLength[plaintext]}]; z]];
vigdecrypt[cipher, -key]

3.2.5. 下面密文是由維吉內爾密碼器加密所得，試破解之。
VVHQWVVRHMUSGJGTHKIHTSSEJCHLSFCBGVWCRLRYQTFSVGAHWK
CUHWAUGLQHNSLRLJSHBLTSPISPRDXLJSVEEGHLQWKASSKUWEPW
QTWVSPGOELKCQYFNSVWLJSNIQKGNRGYBWLWGOVIOKHKAZKQKXZ
GYHCECMEIUJOQKWFWVEFQHKIJRCLRLKBIENQFRJLJSDHGRHLSF
QTWLAUQRHWDMWLGUSGIKKFLRYVCWVSPGPMLKASSJVOQXEGGVEY
GGZMLJCXXLJSVPAIVWIKVRDRYGFRJLJSLVEGGVEYGGEIAPUUIS
FPBTGNWWMUCZRVTWGLRWUGUMNCZVILE

Exit[]

\(\(cipher = "VVHQWVVRHMUSGJGTHKIHTSSEJCHLSFCBGVWCRLRYQTFSVGAHWKCUHWAUGLQHNSLRLJSHBLTSPISPRDXLJSVEEGHLQWKASSKUWEPWQTWVSPGOELKCQYFNSVWLJSNIQKGNRGYBWLWGOVIOKHKAZKQKXZGYHCECMEIUJOQKWFWVEFQHKIJRCLRLKBIENQFRJLJSDHGRHLSFQTWLAUQRHWDMWLGUSGIKKFLRYVCWVSPGPMLKASSJVOQXEGGVEYGGZMLJCXXLJSVPAIVWIKVRDRYGFRJLJSLVEGGVEYGGEIAPUUISFPBTGNWWMUCZRVTWGLRWUGUMNCZVILE";\)\)

keymax = 9; 
    cipherlength = StringLength[cipher];
coinci[j_] := 
        Count[Table[
            If[StringTake[cipher, {i}] == StringTake[cipher, {i + j}], 
              t, {}], {i, cipherlength - j}], t];
coinci = Table[coinci[j], {j, keymax}];
keylength = Position[coinci, 
            Max[coinci]][[1]][[1]]
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
frequency[txt_] := 
        Array[Function[
            i, {Characters[cap][[i]], 
              Count[Characters[txt], Characters[cap][[i]]]}], 
          26];
choose[txt_, m_, n_] := 
        StringJoin[
          Module[{z}, 
            z = {}; 
Do[
              z = Insert[z, StringTake[txt, {m*(i - 1) + n}], i], {i, 1, 
                Floor[StringLength[txt]/m]}]; z]];
alfreq = { .082,  .015,  .028,  .043,  .127,  .022,  .020,  .061,  .070,  .002,  .008,  .040,  .024,  .067,  .075,  .019,  .001,  .060,  .063,  .091,  .028,  .010,  .023,  .001,  .020,  .001};
s[a_, b_, n_] := 
        Sum[a[[i]]*b[[Mod[i - n - 1, 26] + 1]], {i, 26}];
corr[a_] := Table[s[a, alfreq, i], {i, 0, 25}];
v[j_] := 
        Table[frequency[
                choose[cipher, keylength, j]][[i]][[2]], {i, 
            26}];
key = 
      Table[Position[corr[v[j]], 
                Max[corr[v[j]]]][[1]][[1]], {j, keylength}] - 
        1
abc = "abcdefghijklmnopqrstuvwxyz";
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vigdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[
              abc, {Mod[i - 1 + m[[Mod[n - 1, Length[m]] + 1]], 26] + 
                  1}], {i, 1, 26}];
vigdecrypt[plaintext_, v_] := 
        StringJoin[
          Module[{z}, z = {}; 
            Do[z = Insert[z, 
                  StringReplace[Characters[plaintext][[i]], 
                    vigdecryptkey[v, Mod[i - 1, Length[v]] + 1]], i], {i, 
                StringLength[plaintext]}]; z]];
vigdecrypt[cipher, -key]

3.2.6. 下面密文是由維吉內爾密碼器加密所得，試破解之。
OCWYIKOOONIWUGPMXWKTZDWGTSSAYJZWYEMDLBNQAAAVSUWDVB
RFLAUPLOOUBFGQHGCSCMGZLATOEDCSDEIDPBHTMUOVPIEKIFPI
MFNOAMVLPQFXEJSMXMPGKCCAYKWFZPYUAVTELWHRHMWKBBVGTG
UVTEFJLODFEFKVPXSGRSORVGTAJBSAUHZRZALKWUOWHGEDEFNS
WMRCIWCPAAAVOGPDNFPKTDBALSISURLNPSJYEATCUCEESOHHDA
RKHWOTIKBROQRDFMZGHGUCEBVGWCDQXGPBGQWLPBDAYLOOQDMU
HBDQGMYWEUIK

Exit[]

\(\(cipher = "OCWYIKOOONIWUGPMXWKTZDWGTSSAYJZWYEMDLBNQAAAVSUWDVBRFLAUPLOOUBFGQHGCSCMGZLATOEDCSDEIDPBHTMUOVPIEKIFPIMFNOAMVLPQFXEJSMXMPGKCCAYKWFZPYUAVTELWHRHMWKBBVGTGUVTEFJLODFEFKVPXSGRSORVGTAJBSAUHZRZALKWUOWHGEDEFNSWMRCIWCPAAAVOGPDNFPKTDBALSISURLNPSJYEATCUCEESOHHDARKHWOTIKBROQRDFMZGHGUCEBVGWCDQXGPBGQWLPBDAYLOOQDMUHBDQGMYWEUIK";\)\)

keymax = 9; 
    cipherlength = StringLength[cipher];
coinci[j_] := 
        Count[Table[
            If[StringTake[cipher, {i}] == StringTake[cipher, {i + j}], 
              t, {}], {i, cipherlength - j}], t];
coinci = Table[coinci[j], {j, keymax}];
keylength = Position[coinci, 
            Max[coinci]][[1]][[1]]
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
frequency[txt_] := 
        Array[Function[
            i, {Characters[cap][[i]], 
              Count[Characters[txt], Characters[cap][[i]]]}], 
          26];
choose[txt_, m_, n_] := 
        StringJoin[
          Module[{z}, 
            z = {}; 
Do[
              z = Insert[z, StringTake[txt, {m*(i - 1) + n}], i], {i, 1, 
                Floor[StringLength[txt]/m]}]; z]];
alfreq = { .082,  .015,  .028,  .043,  .127,  .022,  .020,  .061,  .070,  .002,  .008,  .040,  .024,  .067,  .075,  .019,  .001,  .060,  .063,  .091,  .028,  .010,  .023,  .001,  .020,  .001};
s[a_, b_, n_] := 
        Sum[a[[i]]*b[[Mod[i - n - 1, 26] + 1]], {i, 26}];
corr[a_] := Table[s[a, alfreq, i], {i, 0, 25}];
v[j_] := 
        Table[frequency[
                choose[cipher, keylength, j]][[i]][[2]], {i, 
            26}];
key = 
      Table[Position[corr[v[j]], 
                Max[corr[v[j]]]][[1]][[1]], {j, keylength}] - 
        1
abc = "abcdefghijklmnopqrstuvwxyz";
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vigdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[
              abc, {Mod[i - 1 + m[[Mod[n - 1, Length[m]] + 1]], 26] + 
                  1}], {i, 1, 26}];
vigdecrypt[plaintext_, v_] := 
        StringJoin[
          Module[{z}, z = {}; 
            Do[z = Insert[z, 
                  StringReplace[Characters[plaintext][[i]], 
                    vigdecryptkey[v, Mod[i - 1, Length[v]] + 1]], i], {i, 
                StringLength[plaintext]}]; z]];
vigdecrypt[cipher, -key]

3.2.7. 下面密文是由維吉內爾密碼器加密所得，試破解之。
 XKJUROWMLLPXWZNPIMBVBQJCNOWXPCCHHVVFVSLLFVXHAZITYX
 OHULXQOJAXELXZXMYJAQFSTSRULHHUCDSKBXKNJQIDALLPQSLL
 UHIAQFPBPCIDSVCIHWHWEWTHBTXRLJNRSNCIHUVFFUXVOUKJLJ
 SWMAQFVJWJSDYLJOGJXDBOXAJULTUCPZMPLIWMLUBZXVOODYBA
 FDSKXGQFADSHXNXEHSARUOJAQFPFKNDHSAAFVULLUWTAQFRUPW
 JRSZXGPFUTJQIYNRXNYNTWMHCUKJFBIRZSMEHHSJSHYONDDZZN
 TZMPLILRWNMWMLVURYONTHUHABWNVW

Quit[]

\(\(cipher = "XKJUROWMLLPXWZNPIMBVBQJCNOWXPCCHHVVFVSLLFVXHAZITYXOHULXQOJAXELXZXMYJAQFSTSRULHHUCDSKBXKNJQIDALLPQSLLUHIAQFPBPCIDSVCIHWHWEWTHBTXRLJNRSNCIHUVFFUXVOUKJLJSWMAQFVJWJSDYLJOGJXDBOXAJULTUCPZMPLIWMLUBZXVOODYBAFDSKXGQFADSHXNXEHSARUOJAQFPFKNDHSAAFVULLUWTAQFRUPWJRSZXGPFUTJQIYNRXNYNTWMHCUKJFBIRZSMEHHSJSHYONDDZZNTZMPLILRWNMWMLVURYONTHUHABWNVW";\)\)

keymax = 9; 
    cipherlength = StringLength[cipher];
coinci[j_] := 
        Count[Table[
            If[StringTake[cipher, {i}] == StringTake[cipher, {i + j}], 
              t, {}], {i, cipherlength - j}], t];
coinci = Table[coinci[j], {j, keymax}];
keylength = Position[coinci, 
            Max[coinci]][[1]][[1]]
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
frequency[txt_] := 
        Array[Function[
            i, {Characters[cap][[i]], 
              Count[Characters[txt], Characters[cap][[i]]]}], 
          26];
choose[txt_, m_, n_] := 
        StringJoin[
          Module[{z}, 
            z = {}; 
Do[
              z = Insert[z, StringTake[txt, {m*(i - 1) + n}], i], {i, 1, 
                Floor[StringLength[txt]/m]}]; z]];
alfreq = { .082,  .015,  .028,  .043,  .127,  .022,  .020,  .061,  .070,  .002,  .008,  .040,  .024,  .067,  .075,  .019,  .001,  .060,  .063,  .091,  .028,  .010,  .023,  .001,  .020,  .001};
s[a_, b_, n_] := 
        Sum[a[[i]]*b[[Mod[i - n - 1, 26] + 1]], {i, 26}];
corr[a_] := Table[s[a, alfreq, i], {i, 0, 25}];
v[j_] := 
        Table[frequency[
                choose[cipher, keylength, j]][[i]][[2]], {i, 
            26}];
key = 
      Table[Position[corr[v[j]], 
                Max[corr[v[j]]]][[1]][[1]], {j, keylength}] - 
        1
abc = "abcdefghijklmnopqrstuvwxyz";
cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
vigdecryptkey[m_, n_] := 
        Table[StringTake[cap, {i}] -> 
            StringTake[
              abc, {Mod[i - 1 + m[[Mod[n - 1, Length[m]] + 1]], 26] + 
                  1}], {i, 1, 26}];
vigdecrypt[plaintext_, v_] := 
        StringJoin[
          Module[{z}, z = {}; 
            Do[z = Insert[z, 
                  StringReplace[Characters[plaintext][[i]], 
                    vigdecryptkey[v, Mod[i - 1, Length[v]] + 1]], i], {i, 
                StringLength[plaintext]}]; z]];
vigdecrypt[cipher, -key]