Hill 密码矩阵计算器

计算 Hill 密码的逆矩阵、行列式,验证密钥,以及更多功能。

明文
结果
0 characters
Size:
det=9
Padding Char
Format
continuousblocksgroups

Frequently Asked Questions About Hill Cipher

What is the Hill cipher?

The Hill cipher is a polygraphic substitution cipher that encrypts blocks of letters at once using matrix multiplication. Invented by mathematician Lester S. Hill in 1929, it was the first cipher to operate on more than three symbols simultaneously. It converts letters to numbers, multiplies them by a key matrix, and applies modulo 26 to produce ciphertext.

How does the Hill cipher use matrices?

The Hill cipher converts plaintext letters into numerical vectors, then multiplies each vector by a square key matrix modulo 26. For a 2x2 key matrix, pairs of letters are encrypted together; for a 3x3 matrix, groups of three letters are processed at once. The resulting numbers are converted back to letters to form the ciphertext.

What key matrix sizes does the Hill cipher support?

The Hill cipher supports any n x n square matrix as a key, but 2x2 and 3x3 matrices are the most commonly used. A 2x2 matrix encrypts two letters at a time, while a 3x3 matrix encrypts three. Larger matrices provide stronger encryption but are more difficult to manage by hand and require more complex inverse calculations for decryption.

How do you decrypt a Hill cipher?

Decryption requires computing the inverse of the key matrix modulo 26, then multiplying each ciphertext vector by that inverse matrix. First, find the determinant of the key matrix. Then compute its modular multiplicative inverse mod 26. Finally, multiply the adjugate matrix by this inverse determinant mod 26 to obtain the decryption key matrix.

Why must the Hill cipher key matrix be invertible?

The key matrix must be invertible modulo 26 so that the ciphertext can be decrypted back to the original plaintext. Specifically, the determinant of the matrix must be coprime with 26 (it cannot share factors of 2 or 13). If the determinant is not coprime with 26, no modular inverse exists and decryption becomes impossible.

Who invented the Hill cipher?

Lester S. Hill, an American mathematician, invented the Hill cipher in 1929. He published his work in the journal The American Mathematical Monthly. Hill was a professor at several universities and developed the cipher to demonstrate practical applications of linear algebra in cryptography. It was one of the first ciphers to use advanced mathematics systematically.

Is the Hill cipher secure?

No, the Hill cipher is not considered secure by modern standards. It is vulnerable to known-plaintext attacks, where an attacker who knows both plaintext and ciphertext can recover the key matrix by solving a system of linear equations. It also lacks diffusion across blocks. However, it remains an important educational tool for teaching matrix algebra and classical cryptography concepts.

什么是希尔密码?

希尔密码(Hill Cipher)(有时拼作"Hill cypher")是一种多字母替换密码,使用矩阵乘法同时加密多个字母块。由数学家 Lester S. Hill 于 1929 年发明,它是第一种完全基于线性代数而非机械装置或简单字母替换的实用密码。

希尔密码不是每次替换一个字母,而是将字母组转换为数值向量,用秘密密钥矩阵相乘,并应用模 26 运算。2x2 密钥矩阵每次加密两个字母,3x3 矩阵每次处理三个字母。这种分组方式比凯撒密码仿射密码等单字母替换密码提供了更强的扩散效果。

希尔密码的工作原理

加密公式

C = (K x P) mod 26

其中:

  • P 是明文向量(字母转换为数字:A=0, B=1, ... Z=25)
  • K 是方形密钥矩阵(2x2 或 3x3)
  • C 是结果密文向量

2x2 加密示例

使用密钥矩阵 K = [[3, 3], [2, 5]] 加密"HELP":

第一步: 转换为数字并组成长度为 2 的块:

  • 块 1:H=7, E=4 -> [7, 4]
  • 块 2:L=11, P=15 -> [11, 15]

第二步: 将每个块与密钥矩阵相乘:

块 1:[[3,3],[2,5]] x [7,4] = [3x7+3x4, 2x7+5x4] = [33, 34]

应用 mod 26:[33 mod 26, 34 mod 26] = [7, 8] = HI

块 2:[[3,3],[2,5]] x [11,15] = [3x11+3x15, 2x11+5x15] = [78, 97]

应用 mod 26:[78 mod 26, 97 mod 26] = [0, 19] = AT

结果: HELP 变为 HIAT

解密公式

P = (K^-1 x C) mod 26

解密需要密钥矩阵的模逆矩阵。该逆矩阵使用行列式、其模 26 逆元以及伴随矩阵计算。我们的希尔密码解码器自动完成此过程。

密钥矩阵要求

并非每个矩阵都能作为有效的希尔密码密钥。必须满足三个条件:

要求说明
方形矩阵必须是 n x n(2x2 或 3x3)
行列式不为零det(K) 不能等于 0
行列式与 26 互质gcd(det(K), 26) = 1 -- 不能被 2 或 13 整除

mod 26 下的有效行列式值:1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25

2x2 与 3x3 矩阵的比较

特性2x2 矩阵3x3 矩阵
每块字母数23
密钥元素数49
扩散效果中等
手工计算可管理复杂
已知明文攻击所需字符数49
最适合学习更高安全性

安全性分析

希尔密码通过消除直接频率分析,提供了比单字母替换密码更强的安全性——不同位置的相同字母会产生不同的密文。然而,它存在一个根本性弱点。

已知明文攻击

由于加密是纯粹线性的,如果攻击者获得 n² 个匹配的明文-密文字符(2x2 需要 4 个,3x3 需要 9 个),就可以建立线性方程组并求解完整的密钥矩阵。这使得希尔密码不适用于任何现实世界的安全应用。

尽管存在此漏洞,该密码仍是一个出色的教学工具。其矩阵乘法原理直接影响了现代算法——值得注意的是,AES(当前全球加密标准)在其 MixColumns 步骤中使用了矩阵乘法。

希尔密码与其他古典密码的比较

特性希尔密码仿射密码普莱费尔密码凯撒密码
类型多字母单字母双字母单字母
数学基础矩阵乘法线性公式 (ax+b)基于方格的规则模 26 加法
块大小2 或 3 个字母1 个字母2 个字母1 个字母
密钥空间312 种密钥~600 万亿26 种密钥
抵抗频率分析部分部分

常见问题

如何手工计算希尔密码?

将每个字母转换为数字(A=0 到 Z=25),按矩阵大小将文本分割成块,用密钥矩阵乘以每个块向量,并对每个结果应用 mod 26。对于使用密钥 [[3,3],[2,5]] 的 2x2 矩阵和明文"HI":计算 [3x7+3x8, 2x7+5x8] = [45, 54],然后 mod 26 得到 [19, 2] = "TC"。更多示例请访问我们的示例页面

谁发明了希尔密码?

美国数学家和教授 Lester S. Hill 于 1929 年在《美国数学月刊》上发表了该密码。它是第一种完全基于数学原理而非机械替换装置的多字母密码。

今天还在使用希尔密码吗?

不用于实际加密,因为它容易受到已知明文攻击。然而,其基于矩阵的方法深刻影响了现代分组密码。AES 在其 MixColumns 操作中使用了矩阵乘法——这直接源自 Hill 的思想。该密码在大学密码学和线性代数课程中仍被广泛教授。

什么使密钥矩阵有效?

矩阵必须是方形的,其行列式必须与 26 互质(不被 2 或 13 整除)。如果 gcd(det(K), 26) 不等于 1,则不存在模逆元,解密将变得不可能。有效的行列式值为:1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25。

希尔密码 2x2 与 3x3——应该使用哪个?

学习时,从 2x2 矩阵开始——可以手工计算,且概念可直接迁移。对于课堂作业或更好的安全性演示,3x3 矩阵提供了更强的扩散效果(每块混合更多字母),并且需要更多已知明文才能攻击(9 个字符而非 4 个)。

希尔密码能处理空格和标点吗?

标准希尔密码仅对 26 个英语字母字母表进行运算。空格、数字和标点通常在加密前被去除。某些变体将模数扩展以包括额外字符(例如 mod 29 以添加空格、句点和逗号),但传统实现只处理 A-Z。

相关工具与资源