• 沒有找到結果。

傳統密碼之旅

N/A
N/A
Protected

Share "傳統密碼之旅"

Copied!
22
0
0

(1)

傳統密碼之旅 ( 下 )

八 . 希爾密碼(Hill Ciphers)

M =

1 2 3 4 5 6 0 7 11

(0, 1, 2)M = (0, 1, 2)

1 2 3 4 5 6 0 7 11

≡ (4, 19, 2) (mod 26),

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

Out[52]= {4,19,2}

1 9

13 −1 −3

−44 11 6 28 −7 −3

55

(2)

M = M−1 =

13 23 17 24 7 18 6 5 17

In[53]:= M=PolynomialMod[Inverse[M],26]

Out[53]= {{13,23,17},{24,7,18},{6,5,17}}

(4, 19, 2)M = (4, 19, 2)

13 23 17 24 7 18 6 5 17

≡ (0, 1, 2) (mod 26)。

In[54]:= Mod[{4,19,2}.M,26]

Out[54]= {0,1,2}

hihappybirthdaytoyou

In[55]:= 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];

In[60]:= ma="hihappybirthdaytoyou"; mx=ma<>"x";

m0=Table[StringTake[Q0[mx],{i,i+1}],{i,1,2StringLength[mx],2}]//ToExpression;

m=Table[{m0[[i]],m0[[i+1]],m0[[i+2]]},{i,1,StringLength[mx],3}]

Out[60]= hihappybirthdaytoyoux

Out[62]= {{7,8,7}, {0,15,15}, {24,1,8}, {17,19,7},{3,0,24},{19,14,24},{14,20,23}}hihappybirthdaytoyoux In[63]:= c=Mod[m.M,26]

Out[63]= {{13,25,16},{8,24,21},{2,5,10},{15,22,8},{3,18,13},{23,16,15},{16,3,25}}

(3)

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

Out[64]= {13,25,16,08,24,21,02,05,10,15,22,08,03,18,13,23,16,15,16,03,25}

In[65]:= ca=A[cs]//StringJoin//ToUpperCase Out[65]= NZQIYVCFKPWIDSNXQPQDZ

In[66]:= 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}]

Out[67]= {{13,25,16},{8,24,21},{2,5,10},{15,22,8},{3,18,13},{23,16,15},{16,3,25}}

In[68]:= %==c Out[68]= True

In[69]:= mf=Mod[c.M’,26]//Flatten;

ms=Table[If[mf[[i]]<10,"0"<>ToString[mf[[i]]],ToString[mf[[i]]]],{i,StringLength[cl]}]//A//StringJoin Out[70]= hihappybirthdaytoyoux

In[71]:= StringDrop[ms,-1]==ma Out[71]= True

= (1, 11, 14, . . .) 改成 clock = (2, 11, 14, . . .), 則密文中前三個字母由 TZP 變為 UBS。

blo = (1, 11, 14) 7−→ (1, 11, 14)

1 2 3 4 5 6 0 7 11

= (19, 25, 15) = TZP

clo = (2, 11, 14) 7−→ (2, 11, 14)

1 2 3 4 5 6 0 7 11

= (20, 1, 18) = UBS

1. 密文攻擊法:用密文攻擊法要破解希爾密碼是困難的, 但希爾密碼卻俯首稱臣於其他幾個攻 擊法之下。

2. 已知明文攻擊法:如果我們不知道 n, 那麼就試幾個不同的 n 值, 直等到找到正確的為止。

7 14 22 0 17 4 24 14 20 19 14 3 0 24

(4)

25 22 18 4 13 8 20 18 15 11 9 21 4 20 前兩個區塊得到矩陣方程式

7 14 22 0

! a b c d

!

≡ 25 22 18 4

!

(mod 26)。

!

7 14 20 19

! a b c d

!

≡ 25 22 15 11

!

(mod 26)。

!

7 14 20 19

!−1

≡ 5 10 18 21

!

(mod 26)。

In[72]:= PolynomialMod[Inverse[{{7,14},{20,19}}],26]

Out[72]= {{5,10},{18,21}}

M ≡ 5 10 18 21

! 25 22 15 11

!

≡ 15 12 11 3

!

(mod 26)。

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

Out[73]= {{15,12},{11,3}}

3. 選擇明文攻擊法: 選擇明文攻擊法可採取相同的策略來進行, 不過會快一些。 再一次地, 如 果你不知道 n, 試幾個不同的 n 值, 直到行的通為止。 所以假設 n 為已知。 選擇明文的第 一區塊為 baaa · · · = 1000 · · · , 第二區塊為 abaa · · · = 0100 · · · , 如此繼續至第 n 區塊 為 · · · aaab = · · · 0001。 密文區塊就是加密矩陣 M 的列向量。

4. 選擇密文攻擊法: 至於選擇密文攻擊法, 則採用跟選擇明文攻擊法完全一樣的策略來進行, 但明文與密文的角色對調。 如此所得到的明文區塊將會是加密矩陣 M 之反元素 (即解密矩 陣) 的列向量。

(5)

九 . 代換密碼(Substitution Ciphers)

In[74]:= lwn="LWNSOZBNWVWBAYBNVBSQWVWOHWDIZWRBBNPBPOOUWRPAWXAWPBWZWMYPOBNPBBNWJPAWWRZSLWZQJBNWIAXAWPBSALIBNXWA\

BPIRYRPOIWRPQOWAIENBVBNPBPUSREBNWVWPAWOIHWOIQWABJPRZBNWFYAVYIBSHNPFFIRWVVBNPBBSVWXYAWBNWVWAIENBV\

ESDWARUWRBVPAWIRVBIBYBWZPUSREUWRZWAIDIREBNWIATYVBFSLWAVHASUBNWXSRVWRBSHBNWESDWARWZBNPBLNWRWDWAPR\

JHSAUSHESDWARUWRBQWXSUWVZWVBAYXBIDWSHBNWVWWRZVIBIVBNWAIENBSHBNWFWSFOWBSPOBWASABSPQSOIVNIBPRZBSIR\

VBIBYBWRWLESDWARUWRBOPJIREIBVHSYRZPBISRSRVYXNFAIRXIFOWVPRZSAEPRIKIREIBVFSLWAVIRVYXNHSAUPVBSVWWUU\

SVBOICWOJBSWHHWXBBNWIAVPHWBJPRZNPFFIRWVV";

W B R S I V A P N O · · · 76 64 39 36 36 35 34 32 30 16 · · ·

In[75]:= cap="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

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

StringLength[lwn]

frequency[lwn]//Sort Out[77]= 520

Out[78]= {{0,G},{1,C},{1,K},{1,M},{1,T},{6,Q},{7,J},{7,L},{8,D},{11,F},{11,X},{13,E},{13,U},{13,Y},{14,H}, {15,Z},{16,O},{30,N},{32,P},{34,A},{35,V},{36,I},{36,S},{39,R},{64,B},{76,W}}

8傑弗遜

9富蘭克林

,

(6)

e t a o i n s h r

.127 .091 .082 .075 .070 .067 .063 .061 .060

W B R S I V A P N W 3 4 12 2 4 10 14 3 1

B 4 4 0 11 5 5 2 4 20 R 5 5 0 1 1 5 0 3 0

S 1 0 5 0 1 3 5 2 0 I 1 8 10 1 0 2 3 0 0 V 8 10 0 0 2 2 0 3 1 A 7 3 4 2 5 4 0 1 0 P 0 8 6 0 1 1 4 0 0 N 14 3 0 1 1 1 0 7 0

• th he in er an re ed on es st en at to nt ha

(7)

{R, S, I, V, P} = {a, o, i, n, s}。

{S, I, P} = {a, o, i} 與 {R, V} = {n, s}。

W = e, B = t, R = n, S = o, I = i, V = s, A = r, P = a, N = h。

LehoOZ these trYths to QeseOHeDiZent that aOOUen

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty, and the pursuit of Happiness.

(8)

That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed.

That whenever any Form of Government becomes destructive of these ends, it is the Right of the People to alter or to abolish it, and to institute new Government, laying its foundation on such principles and organizing its powers in such form, as to seem most likely to effect their Safety and Happiness.

10凡爾納

,

(9)

(10)

(11)

T, A, O, I, N, S, H, R, D, L;

． M ． ERE ．． ESLNE ．。

AMHEREA． ESLANEY。

(12)

AMHEREABESLANEY。(我來了, 阿貝．斯蘭尼。)

A． ELRI ． ES。

ELSIE． RE ． ARETOMEETTHYGO ．。

(13)

十一 . 二進位數與ASCII

(14)

0 NUL 1 SOH 2 STX 3 ETX 4 EOT 5 ENQ 6 ACK 7 BEL 8 BS 9 HT 10 NL 11 VT 12 NP 13 CR 14 SO 15 SI 16 DLE 17 DC1 18 DC2 19 DC3 20 DC4 21 NAK 22 SYN 23 ETB 24 CAN 25 EM 26 SUB 27 ESC 28 FS 29 GS 30 RS 31 US 32 SP 33 ! 34 " 35 # 36 \$ 37 % 38 & 39 ’ 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 91 [ 92 \ 93 ] 94 ^ 95 _ 96 ‘ 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 125 } 126 ~ 127 DEL

十二 . 單次鑰匙簿密碼(One -Time Pads)

(明文) 0 0 1 0 0 1 0 1 0 1 (鑰匙) 1 0 0 1 0 0 1 0 1 0 (密文) 1 0 1 1 0 1 1 1 1 1 解密動作與加密完全一樣, 用同一個鑰匙加到密文即可, 如下:

(密文) 1 0 1 1 0 1 1 1 1 1 (鑰匙) 1 0 0 1 0 0 1 0 1 0 (明文) 0 0 1 0 0 1 0 1 0 1

(15)

『熱線 (Hot Line)』 就是採用單次鑰匙簿密碼系統來互通信息。

十三 . 線性回饋位移暫存器序列

010000100101100111110001101110101000010010110011111 可由其起始值 k = {0, 1, 0, 0, 0}

x1 = 0, x2 = 1, x3 = 0, x4 = 0, x5 = 0 及下面的線性遞迴關係式得到:

xn+5 = xn+ xn+2 (mod 2)。

(16)

xn+m ≡ c0xn+ c1xn+1+ c2xn+2+ · · · + cm−1xn+m−1 (mod 2), 此處係數 c = {c0, c1, . . . , cm−1} 為 0 或 1。 若我們指定起始值為

k= {x1, x2, x3, · · · , xm}

• lfsr[c,k,n] 將係數為 c = {c0, c1, . . . , cm−1} 之遞迴關係式, 在起始值 k = {x1, x2, x3, . . . , xm} 之下所生成的序列, 輸出其前面 n 項。

In[79]:=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];

In[80]:= 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 Out[82]= True

(明文) 1 0 1 1 0 0 1 1 1 0 0 0 1 1 1 1 (鑰匙) 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 1 0 1 0 1 1 0 解密則與加密完全一樣, 將鑰匙序列加在密文上即可完成。

xn+31= xn+ xn+3

(17)

.

...................................................................................................................

xm+2 xm+1 .................................................................................................................... xm ....................................................................................................................

L

.

...................................................................................................................密文

.. . .. .. . .. . .. .. . .. .. .. . .. . .. .. . .. .. . .. .. . .. . .. .. .. . .. . .. ..... .. .. . .. . .. .. .. .

.. .. . .. . .. .. .. . .. .. . .. . .. .. .. . .. .. . .. . .. .. .. . .. .. . .. .. .. . .. .. .. .... .. .. . .. .. .. .. . .

.. .. . .. . .. .. .. . .. .. . .. . .. .. .. . .. .. . .. . .. .. .. . .. .. . .. .. .. . .. .. .. .... .. .. . .. .. .. .. . .

L

.. .. . .. .. . .. .. .. . .. . .. .. . .. .. .. . .. . .. .. . .. .. .. . .. . .. .. .. . .. . .. .. .. . .. .............

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .............. . .............. .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ............... ...............

0 1 1 1

! c0

c1

!

= 1 0

!

(18)

0 1 1 1 1 0 1 0 1

 c0

c1

c2

=

 0 1 0

 0 1 1 0 1 1 0 1 1 0 1 0 0 1 0 1

 c0

c1

c2

c3

=

 1 0 1 1

 ,

In[83]:= 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]

Out[84]= {{1}, {1}, {0}, {0}}

In[85]:= 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 Out[86]= True

x1 x2 · · · xm x2 x3 · · · xm+1

... ... ... ...

xm xm+1· · · x2m−1

 c0

c1

...

cm−1

=

 xm+1

xm+2

...

x2m

In[87]:= 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]

(19)

In[90]:= {lfsrmatx[t, 4] == A, lfsrsoln[t, 4]}

Out[90]= {True, {1, 1, 0, 0}}

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

In[92]:=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};

In[93]:= lfsrlength[u, 20]

Out[93]= {1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0}

In[94]:= lfsrsoln[u, 8]

Out[94]= {1, 1, 0, 0, 1, 0, 0, 0}

xn+8 = xn+ xn+1+ xn+4

(20)

xn= xn+1+ xn+4+ xn+8

x16= x17+ x20+ x24= 1 + 1 + 1 = 1 如此繼續下去, 我們可依序決定 x15, x14,· · · , x1。

M =

x1 x2 · · · xm

x2 x3 · · · xm+1

... ... . .. ...

xm xm+1 · · · x2m−1

2022 年即將到來，以往的除夕夜都是在農曆的 12 月 30 號，但是新的一年卻沒有 這一天，由於除夕意指歲末的最後 1 天，只要是正月初一的前一晚都是除夕，因此

 不過以上所提的內容幾乎都會被現在的智慧型手機取 代，因此我們覺得這些功能能夠運用在一個沒有網路