0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

古典暗号ツールを作った(カイ二乗頻度解析でシーザーを自動復号)

0
Posted at

作ったもの

Caesar Cipherhttps://sen.ltd/portfolio/caesar-cipher/

スクリーンショット

  • 5 種の古典暗号: Caesar / ROT13 / Vigenère / Atbash / Rail Fence
  • シーザー総当たり(26 通り全部表示)
  • 文字頻度棒グラフ
  • カイ二乗スコアで最尤シフトを自動検出
  • エンコード + デコード

vanilla JS、ゼロ依存、ビルド不要node --test で 46 ケース。

自動復号の中心: カイ二乗スコア

英語の文字頻度は既知(E=12.7%, T=9.1%, A=8.2%, ...)。観測頻度と期待頻度の差を カイ二乗距離で測る:

for (const [letter, expected] of Object.entries(ENGLISH_FREQ)) {
  const observed = counts[letter] || 0;
  const expectedCount = (expected / 100) * total;
  chiSquared += (observed - expectedCount) ** 2 / expectedCount;
}

小さいほど英語らしい。26 通りの復号結果をスコアリングして最小を選べば、鍵なしでシーザー暗号が解ける。

ROT13 と Atbash は自己逆関数

atbash(atbash(x)) === x
rot13(rot13(x)) === x

2 回適用で元に戻る。ROT13 は 13 × 2 = 26、Atbash は対称反転だから。

Atbash のワンライナー

219 - c.charCodeAt(0)

219 = 'a' + 'z' = 97 + 122。a ↔ z、b ↔ y、... の対応を一発で実現。

シリーズ

100+ 公開ポートフォリオ シリーズの #82 です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?