Contents

说明:
本文来自《Understanding Cryptography》习题

  • 密文

xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit

  • 字频分析
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$str = 'xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpitghlxiwiwtxgqadds';
$result = array_count_values(str_split($str));
asort($result, SORT_NUMERIC);
print_r(array_slice($result, -5));
/*
Array
(
[p] => 5
[l] => 5
[x] => 7
[i] => 9
[t] => 10
)
*/$shift = ord('e') - ord('t'); // 最高频字符 t 映射成 e
$begin = ord('a');
$end = ord('z');
$arr = [];
for ($i = 0; $i < strlen($str); $i++) {
$ord = ord($str[$i]) + $shift;
if ($ord < $begin) {
$ord += 26;
}
if ($ord > $end) {
$ord -= 26;
}
$arr[] = chr($ord);
}
echo implode('', $arr) . "\n";
  • 暴力破解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$str = 'xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpitghlxiwiwtxgqadds';
$begin = ord('a');
$end = ord('z');
for ($shift = 1; $shift < 26; $shift++) {
$arr = [];
for ($i = 0; $i < strlen($str); $i++) {
$ord = ord($str[$i]) - $shift;
if ($ord < $begin) {
$ord += 26;
}
if ($ord > $end) {
$ord -= 26;
}
$arr[] = chr($ord);
}
echo implode('', $arr) . "\n";
}

结果:

ifweallunitewewillcausetheriverstostainthegreatwaterswiththeirblood

使用 Google搜索 if we all unite we will cause the rivers to stain the great water switch their blood得到结果:

Tecumseh's Speech to the Osages (Winter 1811-12)

www.historyisaweapon.com/defcon1/tecumosages.html

Tecumseh的简介: https://en.wikipedia.org/wiki/Tecumseh

反抗侵略的北美土著英雄。

Contents