第四章古典密碼之旅(下)

pages 83 - 107

希爾密碼

M = {{1, 2, 3}, {4, 5, 6}, {0, 7, 11}}; 
    Mod[{0, 1, 2} . M, 26]

M' = PolynomialMod[Inverse[M], 26]

Mod[{4, 19, 2} . M', 26]

一個例子

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];

ma = "hihappybirthdaytoyou"; 
    mx = ma <> "x"
m0 = 
        Table[StringTake[Q0[mx], {i, i + 1}], {i, 1, 2  StringLength[mx], 
              2}] // ToExpression;
m = 
      Table[{m0[[i]], m0[[i + 1]], m0[[i + 2]]}, {i, 1, 
          StringLength[mx], 3}]

c = Mod[m . M, 26]

cf = Flatten[c];
cs = 
      Table[If[cf[[i]] < 10, "0" <> ToString[cf[[i]]], 
          ToString[cf[[i]]]], {i, StringLength[mx]}]

ca=A[cs] // StringJoin//ToUpperCase

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

% == c

mf = Mod[c . M', 26] // Flatten;
ms = Table[
            If[mf[[i]] < 10, "0" <> ToString[mf[[i]]], 
              ToString[mf[[i]]]], {i, StringLength[cl]}] // A // 
        StringJoin

StringDrop[ms, -1] == ma

PolynomialMod[Inverse[{{7, 14}, {20, 19}}], 26]

Mod[{{5, 10}, {18, 21}} . {{25, 22}, {15, 11}}, 
      26]

代換密碼

\(\(lwn = "LWNSOZBNWVWBAYBNVBSQWVWOHWDIZWRBBNPBPOOUWRPAWXAWPBWZWMYPOBNPBBNWJPAWWRZSLWZQJBNWIAXAWPBSALIBNXWABPIRYRPOIWRPQOWAIENBVBNPBPUSREBNWVWPAWOIHWOIQWABJPRZBNWFYAVYIBSHNPFFIRWVVBNPBBSVWXYAWBNWVWAIENBVESDWARUWRBVPAWIRVBIBYBWZPUSREUWRZWAIDIREBNWIATYVBFSLWAVHASUBNWXSRVWRBSHBNWESDWARWZBNPBLNWRWDWAPRJHSAUSHESDWARUWRBQWXSUWVZWVBAYXBIDWSHBNWVWWRZVIBIVBNWAIENBSHBNWFWSFOWBSPOBWASABSPQSOIVNIBPRZBSIRVBIBYBWRWLESDWARUWRBOPJIREIBVHSYRZPBISRSRVYXNFAIRXIFOWVPRZSAEPRIKIREIBVFSLWAVIRVYXNHSAUPVBSVWWUUSVBOICWOJBSWHHWXBBNWIAVPHWBJPRZNPFFIRWVV";\)\)

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

LFSR序列

lfsr[c_, k_, n_] := 
        Module[{z}, 
          z = k; 
Do[
            AppendTo[z, 
              Mod[Array[Function[i, z[[j - Length[k] - 1 + i]]], 
                    Length[k]] . c, 2]], {j, Length[k] + 1, n}]; 
          z];

c5 = {1, 0, 1, 0, 0}; k5 = {0, 1, 0, 0, 0};
s = {0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 
          0, 0, 1, 1, 0, 1, 1, 1, 0, 1,  0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 
          1, 0, 0, 1, 1, 1, 1, 1};
lfsr[c5, k5, Length[s]] == s

例題01

A = {{0, 1, 1, 0}, {1, 1, 0, 1}, {1, 0, 1, 0}, {0, 1, 0, 
            1}};
B = {{1}, {0}, {1}, {1}};
PolynomialMod[Inverse[A] . B, 2]

c4 = {1, 1, 0, 0}; k4 = {0, 1, 1, 0};
t = {0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 
          1, 1, 1, 1};
lfsr[c4, k4, Length[t]] == t

例題02

lfsrmatx[v_, m_] := 
      Array[Function[{i, j}, v[[i + j - 1]]], {m, 
          m}]
lfsrrhs[v_, m_] := 
      Array[Function[i, v[[i + m]]], m]
lfsrsoln[v_, m_] := 
      PolynomialMod[Inverse[lfsrmatx[v, m]] . lfsrrhs[v, m], 2]

{lfsrmatx[t, 4] == A, lfsrsoln[t, 4]}

lfsrlength[v_, m_] := 
      Table[Mod[Det[lfsrmatx[v, k]], 2], {k, 
          Min[m, 1 + Floor[Dimensions[v][[1]]/2]]}]

u = {1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 
          1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 
          0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 
          1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 
          0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0};

lfsrlength[u, 20]

lfsrsoln[u, 8]