解密上一篇文章的亂碼

閱讀時間約 10 分鐘

上一篇文章裡,我設計了一份關於密碼學的遊戲,並邀請大家一起來破解那堆看似亂碼的內容,而在這篇文章,我要來很負責任地提供我自己解密的流程和方法,並且把所有我在過程中所做的決定的理由,都一五一十地交代出來(如果你已經忘記遊戲內容,或是還沒看過題目的話,請至少先去了解遊戲規則之後再回來喔)。

分析頻率

首先,因為題目就已經直接告訴我們,加密的方法為「將 26 個英文字母和空格做重排,並且把明文中的 a 替換成重排後的第一個字元,b 換成第二個,以此類推」,這就表示,在密文中的每一個字母和空格,都只能代表唯一一種字元,所以不會發生密文中的 a 被解成兩種字母這種事。

而因為在英文排版中,像逗號和句號這種標點符號的後面,都會接上一個空格,所以稍微觀察一下密文後,我們就可以很容易地猜出,裡面的 d 代表的是空格。

除了這個簡單的結論,我們還有一件事可以觀察:像題目這種加密法,最大的一個缺陷,就是它保持了字母出現的「頻率」,最常用的那幾個字母,被加密後,也應該要以密文的形式頻繁出現,而這正是我們破解它的最大關鍵。

有了這個想法後,我們就可以先利用終端機,來計算密文中的字元所出現的頻率,並且記錄出現最多的前幾名,來得到下面這個表格(如果你有驚人的注意力,也可以自己慢慢統計):

密文 m n p y z v i w g
頻率 11.02% 9.55% 8.29% 7.64% 7.34% 6.25% 6.16% 5.82% 4.90%

(這裡我忽略了代表空格的 d,它出現的頻率遠遠高過 m,想想也合理,畢竟在英文的每個字中間都有一個空格嘛!至於為什麼我只考慮這幾個,只是為了跟下面的表格數量一致而已。)

接著,我們到維基百科,找找英文字母的使用頻率表,並且寫下幾個最常用的字母:

字母 e t a o i n s h r
頻率 12.70% 9.06% 8.17% 7.51% 6.97% 6.75% 6.33% 6.09% 5.99%

(我這邊只寫到 r,只是因為它和後面一名的 d 有明顯的落差而已。)

找出特定單字

既然我們已經從前一步,掌握到空格的位置了,這也代表我們可以把那密密麻麻的密文,切割成一個一個的單字,比如最一開始的 gmxmvnoh 是一整個字,而接下來是 yvmysah,以此類推。

有了這個結論,再加上頻率分析表的幫助,我做的下一步,就是利用終端機,來找出在密文中,所有含有 mnpyzviwg 的單字,並列出出現最多的幾個,成為我們的目標單字(以防你好奇,這些是所有出現四次以上的單字,這個「四」沒什麼特別的,單純是覺得這樣的樣本數夠多而已),我們暫時還不知道它們代表什麼意思,就先用 · 來代替吧。

密文 nim ah ny tpw ys mcpa zn
明文 ··· ·· ·· ··· ·· ···· ··
密文 p yln syg nzxumn nizw hyl am apni
明文 ····· ··· ······ ···· ··· ·· ····
密文 poo ipf im tiy ipbm nzam tph
明文 ··· ··· ·· ··· ···· ···· ···

大膽假設

找到要鎖定的單字以後,接下來我們就可以開始大膽假設,然後慢慢用英文的知識來推論。

從前面兩個頻率分析表來看,現在我們可以很合理的懷疑,密文中的 m,其實代表的是 e(後面為了方便起見,我們用 m ↦ e 來表示這個意思,也就是 密文 ↦ 明文),而 n ↦ t 等等。當然,我們不能保證兩邊的名次完全吻合,但這是一個可以參考的依據,而這兩個字元,正是我所做的第一個假設(選兩個的原因,單純是覺得一個太少,三個又太多而已)。

有了這個猜測以後,我們就可以從目標單字中,找到又短、又同時有 mn 的,像是最左上角的 nim。根據猜測,我們有 nim ↦ t·e,那有什麼很常見的三個字母的單字,開頭是 t、尾巴是 e 的呢?顯然就是 the 嘛!所以我們應該要有 i ↦ h

同樣的道理,我們可以看到第三個單字,根據猜測,我們有 ny ↦ t·,那剩下的字母,想必就只能是母音,而且也只能是 o 了吧,也就是說,y ↦ o

(透過目前的推論,我們還可以進一步知道第三排的 im ↦ he,似乎還蠻合理的,對吧?)

小心推論

根據上一步的結論,我們可以來重新整理一下前面的目標單字表

密文 nim ah ny tpw ys mcpa zn
明文 the ·· to ··· e··· ·t
密文 p yln syg nzxumn nizw hyl am apni
明文 ··o·t ·o· t···et th·· ·o· ·e ··th
密文 poo ipf im tiy ipbm nzam tph
明文 ··· h·· he ·ho h··e t··e ···

接下來我看到的,是在中間的 nizw ↦ th·· 這個線索,而我能想到符合這個形式的常用單字是:that、this、they。

我首先就刪掉了 nizw ↦ they 這個可能性,因為我覺得 w ↦ y 不太可能,畢竟 w 有出現在密文頻率表中的前幾名,但是 y 的實際使用頻率卻還蠻低的。

但我們還是得決定是 nizw ↦ that 還是 nizw ↦ this,這兩個候選人含有的字母,都有出現在頻率表上,似乎讓人難以分辨,但是如果你仔細觀察,其實後者的 z ↦ i、w ↦ s,相較於前者的 z ↦ a、w ↦ t,還要更符合兩張頻率表(尤其是 w 的部分),因此目前,我選擇相信 z ↦ i、w ↦ s

尋找單獨的字母

在英文裡,我們還有一個線索可以利用,那就是用之前的想法,去尋找只有一個字母的單字,那它們大概就只能代表 a 或是 i 了。

透過終端機,我們可以發現在密文裡,大量出現了單獨的 zp,而因為在上一步,我們猜測 z ↦ i,所以剩下來的,就只有 p ↦ a 這個可能了。

繼續小心推論

有了前面的結果,我想是時候再更新我們的單字表了。

密文 nim ah ny tpw ys mcpa zn
明文 the ·· to ·as e·a· it
密文 p yln syg nzxumn nizw hyl am apni
明文 a·o·t ·o· ti··et this ·o· ·e ·ath
密文 poo ipf im tiy ipbm nzam tph
明文 a·· ha· he ·ho ha·e ti·e ·a·

接下來,我鎖定的單字是(位在右邊的)mcpa ↦ e·a·am ↦ ·eapni ↦ ·athnzam ↦ ti·e,因為它們都已經有其他線索,而且也都有 a 可以利用。

而稍微想一下,就不難發現,能符合這四個單字的字母,大概就只有 a ↦ m 這個可能了吧?

填完剩下的單字

到了這步,我覺得線索就已經夠多了,於是我就再請到終端機,把目前已知的密文給轉換成明文,剩下還不知道的,就繼續用 · 來代替:

·e·e·t··, o·e o· m· ·a·o·ite ·o··asts, hi··e· ··ai·, sta·te· a ·e· se·ies ·a··e· "m· ··s··· he·o." i· this sho·, the· i··ite ·iste·e·s to sha·e sto·ies a·o·t thei· ··s··· he·oes ·ho ha·e he··e· them i· thei· time o· ·ee·. it ·i·es them a ·ha··e to ·i·a··· sa· "tha·· ·o·" ·o· a ·i···ess the· ·e·e· ·o··ot.

·iste·i·· to these sto·ies ·emi··e· me ho· a sim··e a·t o· ·i···ess ·a· ha·e a ··o·o··· im·a·t o· someo·e's ·i·e. i·s·i·e· ·· this sho·, i'· ·i·e to sha·e the sto·· o· m· ··s··· he·o.

···i·· m· se·io· ·ea·, ·hi·e i ·as ··e·a·i·· ·o· the ··i·e·sit· e·t·a··e e·ams, math ·as ·asi·a··· m· e·ti·e ·i·e. it ·as m· to· ··io·it·, a·· i ·e·ote· ·ea··· a·· m· st··· time to it. i e·ha·ste· a·· the mate·ia·s a·ai·a··e, ·oi·· th·o··h e·e·· si···e ha··o·t a·· te·t·oo· i ·o··· ·i··. i ·as a···i·.

·ast ·o··a·· to e·am ·a·. a·te· ·i·ishi·· m· ····h, i ·e·t to the ·ath·oom ·ee·i·· a ·it si·· to m· stoma·h. that ·as a·o·t ·· mi··tes ·e·o·e the ·e·t e·am. ·o· ··esse· it·math. ·at··a···, i too· a·· o· m· st··· a·o·· ·ith me. ·he· the e·am ·as a·o·t to sta·t, i ·e·t the ·ath·oom a·· ·o··o·e· the ··o·· to m· e·am ha··. a·o·· the ·a·, i ·ea·he· i·to m· ·o··et ·o· o·e ·ast ·he··: ·e··i·, e·ase·...

"·ait a mi··te... ·he·e's m· a·missio· ti··et? ·he·e the he·· is m· a·missio· ti··et?"

·es, i ha· ·ost it.

·itho·t m· ti··et, the·e ·as ·o ·a· i ·o··· ta·e the e·am. a·· the ·e·t s···e·t ·as math, the o·e i'· ··t e·e··thi·· i·to! i ··she· ·a·· to the ·ath·oom, sea··hi·· a·o·· the ·a·, ··t i ·o··· ·othi··. i ·o····'t e·e· ·emem·e· ho·, ·he·, o· ·he·e i mi·ht ha·e ··o··e· it. i ·as sta·ti·· to ·a·i·. a mi··io· tho··hts ·e·e ·oi·· th·o··h m· mi··: i ha· s·e·t tho·sa··s o· ho··s ··a·ti·i·· m· math s·i··s, a·· ·o·, tha··s to m· o·· ···msi·ess, it ·o··· a·· ·e a ·aste o· time. m· ha·· ·o·· ·as a·o·t to ·e ··i·e·.

··st as i ·as a·o·t to ·i·e ··, a st·a··e· ·a··e· ·· to me. he ·oo·e· a·o·t m· a·e, ··o·a··· a·othe· ·e··o· st··e·t. ho··i·· o·t a ·ie·e o· ·a·e·, he as·e·, "is this ·o·· a·missio· ti··et?"

·es, he ha· ·o··· it some·he·e. a·· ··st at the ·i·ht mome·t, he sa· a ··mm· ·ho ·as ··ea··· ·oo·i·· ·o· somethi·· a·· ·as ·i··i·· to ·i·e him (me) a ha··. i too· the ti··et, ·a·e him a ··i·· tha··s, a·· ··she· ·a·· to m· e·am ha··. he a·so hea·e· o·· to his o·· e·am, a·· ·isa··ea·e·.

i· this ·ost, i ·a·t to e···ess m· si··e·e ··atit··e to that st··e·t. i ·i··'t ·et the ·ha··e to ·o this ··o·e··· at the time. i· ·o· ha··'t ·o··· m· ti··et a·· ·o·e o·t o· ·o·· ·a· to he·· me, i ·o··· ha·e ·e·e· ·o··i·e· m·se·· ·o· the ·est o· m· ·i·e, m··h ·ess ma·e it th·o··h the e·am.

i ·o·'t ··o· ·ho ·o· a·e, o· e·e· ·o·· ·ame. i· ·a·t, i'·· ··o·a··· ·e·e· ··o·. ··t i sti·· ·a·t to sa·: tha·· ·o·.

(··st i· ·ase ·o·'·e ·o··e·i··, ·i· i ·o ·e·· o· the math e·am? the a·s·e· is... o· ·o··se ·ot. a·te· s··h a ·ea···isaste·, ·ho ·o··· ta·e the e·am ·i·e ·othi·· ha· ha··e·e·, ·i·ht? ···s, the math e·am ·as ·a·ti···a··· ·ha··e··i·· that ·ea·. so i e··e· ·· ·ith a ··, ·hi·h ·as ·a· ··om ·hat i ha· ·o··e· ·o·.)

最後,就是發揮英文想象力的時候了,比如說,我看到第一行有 se·ies 這個字,那看來只能是 series 了,而稍微對應一下密文就會發現,這告訴我們 g ↦ r。又或者是第二行的 someo·e's,就只能是 someone's 了,而這也說明 v ↦ n

用這個方法,我們很快就能解出剩下來的密文,也就可以順利完成這個遊戲了(想看完整解密的結果可以點這)。

加密鑰匙

為了完整起見,我在最後附上題目用來加密的鑰匙。

(溫馨提醒:這是鑰匙,所以寫的是把明文加密的方向,和我們剛剛解密的方向相反。)

明文 a b c d e f g h i
密文 p x f m s j i z
明文 j k l m n o p q r
密文 r u o a v y q k g
明文 s t u v w x y z
密文 w n l b t c h e d