第四章一般習題

page 108

4.1.1. 密文 YIFZMA 乃是由希爾密碼法加密而成，其加密矩陣為{9 &13\cr 2&3}。解密之。

M = {{9, 13}, {2, 3}};

abc = "abcdefghijklmnopqrstuvwxyz";
\(\(num = "0001020304050607080910111213141516171819202122232425";\)\)
digitalize = 
        Table[StringTake[abc, {i}] -> 
            StringTake[num, {2*i - 1, 2*i}], {i, 1, 26}];
alphabetize = 
        Table[StringTake[num, {2*i - 1, 2*i}] -> 
            StringTake[abc, {i}], {i, 1, 26}];
Q0[plaintext_] := 
        StringReplace[plaintext, digitalize];
A[digit_] := StringReplace[digit, alphabetize];

ca = "YIFZMA";
cl = ToLowerCase[ca];
cm = 
        Table[StringTake[cl, {i}], {i, 
            StringLength[cl]}];
d = Q0[cm] // ToExpression;
c = 
      Table[{d[[i]], d[[i + 1]]}, {i, 1, StringLength[cl], 
          2}]

Mod[c . PolynomialMod[Inverse[M], 26], 26]

mf = Flatten[%];
ms = Table[
            If[mf[[i]] < 10, "0" <> ToString[mf[[i]]], 
              ToString[mf[[i]]]], {i, StringLength[cl]}] // A // 
        StringJoin

第四章電腦習題

page 108

4.2.1. 密文 22, 09, 00, 12, 03, 01, 10, 03, 04, 08, 01, 17 乃是由希爾密碼法加密而成，其加密矩陣為{1&2&3\cr 4&5&6\cr 7&8&10}。試解密之。

M = {{1, 2, 3}, {4, 5, 6}, {7, 8, 10}};
c = {{22, 09, 00}, {12, 03, 01}, {10, 03, 04}, {08, 01, 
            17}};
m = 
        Mod[c . PolynomialMod[Inverse[M], 26], 
          26];
mf = Flatten[m]

abc = "abcdefghijklmnopqrstuvwxyz";
\(\(num = "0001020304050607080910111213141516171819202122232425";\)\)
digitalize = 
        Table[StringTake[abc, {i}] -> 
            StringTake[num, {2*i - 1, 2*i}], {i, 1, 26}];
alphabetize = 
        Table[StringTake[num, {2*i - 1, 2*i}] -> 
            StringTake[abc, {i}], {i, 1, 26}];
A[digit_] := StringReplace[digit, alphabetize];

ms = Table[
            If[mf[[i]] < 10, "0" <> ToString[mf[[i]]], 
              ToString[mf[[i]]]], {i, Length[mf]}] // A // 
        StringJoin

StringDrop[ms, -1]

4.2.2. 密文 ZIRKZWOPJJOPTFAPUHFHADRQ 乃是由希爾密碼法加密而成，其加密矩陣為{1&2&3&4\cr 4&3&2&1\cr 11&2&4&6\cr 2 &9&6&4}。試解密之。

abc = "abcdefghijklmnopqrstuvwxyz";
\(\(num = "0001020304050607080910111213141516171819202122232425";\)\)
digitalize = 
        Table[StringTake[abc, {i}] -> 
            StringTake[num, {2*i - 1, 2*i}], {i, 1, 26}];
alphabetize = 
        Table[StringTake[num, {2*i - 1, 2*i}] -> 
            StringTake[abc, {i}], {i, 1, 26}];
Q0[plaintext_] := 
        StringReplace[plaintext, digitalize];
A[digit_] := StringReplace[digit, alphabetize];

ca = "ZIRKZWOPJJOPTFAPUHFHADRQ";

cl = ToLowerCase[ca];
cm = 
        Table[StringTake[cl, {i}], {i, 
            StringLength[cl]}];
d = Q0[cm] // ToExpression;
cd = 
      Table[{d[[i]], d[[i + 1]], d[[i + 2]], d[[i + 3]]}, {i, 
          1, StringLength[ca], 4}]

P = {{1, 2, 3, 4}, {4, 3, 2, 1}, {11, 2, 4, 6}, {2, 9, 6, 
            4}};
mf = 
        Mod[cd . PolynomialMod[Inverse[P], 26], 26] // 
          Flatten;
ms = Table[
            If[mf[[i]] < 10, "0" <> ToString[mf[[i]]], 
              ToString[mf[[i]]]], {i, StringLength[ca]}] // A // 
        StringJoin

4.2.3. 下面密文乃是由代換密碼法加密而成。我們保留了字與字中間的空白，這對解密者當然是一大助益，甚至只要單一字母的頻率分析即可達成任務；因為字的結構是猜測的一大提示。試解密之。

cap1 = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
frequency1[txt_] := 
        Array[Function[
            i, {Count[
                Characters[txt], Characters[cap1][[i]]], Characters[
                  cap1][[i]]}], 26];
fip = "FIPSCSNUK FMYD CUXBK ZMSKYRK TBNU MBMUOQ PYCCUR FBM
SRLOIGSRP RYCYMSYIK GUZMUKKSYR UMB HBRA MYHHUM LOQGU
HBMMYW YF HYRRSU BRG LOQGU BRG LYMMULCSYRK YFFSLSBOK
KBQ CTUQ TBNU OSCCOU GYIHC CTUQOO LBCLT IZ WSCT KUNUR
SRDBCUK WTY UKLBZUG OBKC DYRCT BRG BMU BLLIKUG YF
ASOOSRP B ZYOSLUDBR";
StringLength[fip]

frequency1[fip] // Sort

fugitives from texas prisons have rarely gotten far
including notorious depression era bank robber clyde barrow
of bonnie and clyde and corrections officials say they have
little doubt theyll catch up with seven inmates who escaped
last month and are accused of killing a policeman