仿射密码计算器

计算密钥、模逆元,并生成仿射密码的替换表

明文
0 characters
密文
0 characters

Substitution Tablea = 5, b = 8

PlainABCDEFGHIJKLMNOPQRSTUVWXYZ
CipherINSXCHMRWBGLQVAFKPUZEJOTYD
Affine Cipher Formula
E(x) = (ax + b) mod 26Encryption
D(y) = a⁻¹(y - b) mod 26Decryption

Frequently Asked Questions About Affine Cipher

What is the Affine cipher?

The Affine cipher is a monoalphabetic substitution cipher that encrypts letters using a mathematical formula. It combines a multiplicative and an additive shift, transforming each letter through E(x) = (ax + b) mod 26, where 'a' and 'b' are integer keys. Unlike simple shift ciphers, the Affine cipher uses two keys to create a more complex letter mapping, making it a foundational example of algebraic cryptography.

How does the Affine cipher formula work?

The formula E(x) = (ax + b) mod 26 converts each letter to a number (A=0, B=1, ... Z=25), multiplies by key 'a', adds key 'b', then takes the remainder after dividing by 26. For decryption, the formula D(y) = a⁻¹(y - b) mod 26 reverses the process using the modular multiplicative inverse of 'a'. For example, with a=5 and b=8, the letter A (0) encrypts to (5*0+8) mod 26 = 8, which is I.

What are valid key values for the Affine cipher?

The multiplicative key 'a' must be coprime with 26, meaning gcd(a, 26) = 1. The 12 valid values for 'a' are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. The additive key 'b' can be any integer from 0 to 25. This gives a total of 12 x 26 = 312 unique key combinations. If 'a' is not coprime with 26, multiple plaintext letters map to the same ciphertext letter, making decryption impossible.

How do you decrypt an Affine cipher?

Decryption uses the formula D(y) = a⁻¹(y - b) mod 26, where a⁻¹ is the modular multiplicative inverse of 'a' modulo 26. First, subtract 'b' from the ciphertext letter's numerical value. Then multiply by a⁻¹ and take mod 26. For example, if a=7 then a⁻¹=15 because (7 x 15) = 105 and 105 mod 26 = 1. Without knowing the keys, you can attempt brute force over all 312 combinations or use frequency analysis.

What is modular arithmetic in the Affine cipher?

Modular arithmetic is clock-style math where numbers wrap around after reaching a set value. In the Affine cipher, mod 26 ensures results stay within the 26-letter alphabet. After computing (ax + b), the mod 26 operation returns the remainder when divided by 26, mapping the result back to a valid letter position (0-25). This wrapping behavior is what allows the cipher to cycle through the alphabet seamlessly.

How is the Affine cipher related to the Caesar cipher?

The Caesar cipher is a special case of the Affine cipher where a=1. With a=1, the Affine formula E(x) = (1*x + b) mod 26 simplifies to E(x) = (x + b) mod 26, which is exactly the Caesar shift formula. The Affine cipher generalizes the Caesar cipher by adding a multiplicative component, increasing the key space from 26 to 312 possible keys and creating a non-uniform letter mapping.

Is the Affine cipher secure?

No, the Affine cipher is not secure for modern use. Its key space of only 312 combinations can be brute-forced in milliseconds by a computer. As a monoalphabetic cipher, it is also vulnerable to frequency analysis because each plaintext letter always maps to the same ciphertext letter. However, it remains valuable as an educational tool for teaching modular arithmetic, number theory, and the mathematical foundations of modern cryptography.

What is the modular inverse in the Affine cipher?

The modular inverse (a⁻¹) is the number that, when multiplied by 'a', gives a remainder of 1 when divided by 26. It is essential for decryption. For example, the inverse of 5 mod 26 is 21 because (5 × 21) = 105 and 105 mod 26 = 1. The modular inverse exists only when 'a' is coprime with 26, which is why valid 'a' values are restricted to 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.

How many possible keys does the Affine cipher have?

The Affine cipher has exactly 312 possible key combinations. This comes from 12 valid values for the multiplicative key 'a' (numbers coprime with 26) multiplied by 26 possible values for the additive key 'b' (0 through 25). This small key space means a computer can test every combination in milliseconds, making brute-force attacks trivial.

Can you crack an Affine cipher without knowing the key?

Yes, the Affine cipher can be cracked without the key using two main methods. Brute force tests all 312 key combinations and scores each result using English letter frequency analysis. Alternatively, if you can identify two known plaintext-ciphertext letter pairs, you can solve two simultaneous equations to recover both keys algebraically. Our decoder tool automates the brute-force approach with intelligent scoring.

什么是仿射密码?

仿射密码(Affine Cipher)是一种单表替换密码,使用数学函数对消息进行加密和解密。与更简单的凯撒密码不同,仿射密码将乘法和加法运算结合在一起,在保持相对易于理解的同时提供了更高的安全性。

仿射密码是在移位密码的基础上发展而来的,通过增加乘法成分来扩大密钥空间。这种经典加密方法在密码学教育中被广泛使用,用于讲授模运算和数论的基本概念。

仿射密码公式

仿射密码使用两个密钥:a(乘法密钥)和 b(加法密钥)。加密和解密公式如下:

加密: E(x) = (ax + b) mod 26

解密: D(y) = a⁻¹(y - b) mod 26

其中:

  • x 是明文字母的数值(A=0,B=1,……Z=25)
  • y 是密文字母的数值
  • a 必须与26互质(有效值:1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25)
  • b 可以是0到25之间的任意整数
  • a⁻¹ 是 a 的模乘逆元

如何使用本仿射密码工具

我们的在线仿射密码编码器让加密和解密变得简单:

  1. 在输入框中输入文本
  2. 从下拉菜单选择密钥 A(只显示有效的互质值)
  3. 使用滑块调整密钥 B(0-25)
  4. 选择模式:加密或解密
  5. 一键复制结果

工具会自动验证您的密钥选择,并提供实时转换。您还可以查看所选密钥生成的完整替换字母表。

本仿射密码工具的功能特点

  • 实时加密和解密 — 边输入边看结果
  • 智能密钥验证 — 只有有效的 A 值(与26互质)可供选择
  • 替换表显示 — 查看完整的字母映射关系
  • 复制到剪贴板 — 一键复制结果
  • 保留大小写和间距 — 非字母字符直接传递不变
  • 移动端适配 — 在所有设备上均可使用

如需在不知道密钥的情况下自动解密,请使用我们的仿射密码解码器。如需了解有效密钥背后的数学原理,请访问我们的密钥计算器

常见问题

仿射密码有什么用途?

仿射密码主要用于教育目的,讲授密码学基础知识,包括模运算、乘法逆元和密码分析技术。虽然不适合现代应用的安全需求,但它是数学加密入门的绝佳工具。

仿射密码有多少种可能的密钥?

仿射密码有312种可能的密钥组合(A 的12个有效值乘以 B 的26个值)。这比凯撒密码的26个密钥要多,但数量仍然少到容易受到暴力破解攻击。

为什么密钥 A 必须与26互质?

密钥 A 必须与26互质(即 gcd(A, 26) = 1),以确保每个明文字母映射到唯一的密文字母。如果 A 与26有公因数,多个字母会加密为相同的结果,使解密变为不可能。

密钥 A 的有效值有哪些?

密钥 A 的有效值为:1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 和 25。这些是小于26且与26互质的所有整数。

如何在不知道密钥的情况下解密仿射密码?

您可以使用频率分析或暴力破解方法来解密没有已知密钥的仿射密码。我们的解码工具会自动测试所有312种密钥组合,并根据英文字母频率规律对结果进行可能性排序。

仿射密码安全吗?

不,仿射密码不适合现代使用。由于只有312种可能的密钥,它很容易被暴力破解或频率分析攻破。它最适合用于教育目的和理解密码学概念。

如何破解仿射密码

破解仿射密码(有时也写作"affine cypher")有两种主要方法:

暴力破解法

由于仿射密码只有312种可能的密钥组合,您可以尝试所有组合并检查哪个能产生可读的英文。我们的仿射密码解码器通过频率分析评分自动完成这一过程。

已知明文攻击

如果您知道(或能猜出)两对明文-密文字母对,可以建立两个联立方程并用代数方法求解两个密钥。例如,如果您知道明文"e"对应密文"L",明文"t"对应密文"H",可以写出:

  • (a × 4 + b) mod 26 = 11
  • (a × 19 + b) mod 26 = 7

求解该方程组即可得到 a 和 b 的值。

仿射密码与凯撒密码的比较

凯撒密码实际上是仿射密码的特例,其中乘法密钥 a = 1。这意味着凯撒密码公式 E(x) = (x + b) mod 26 只是仿射公式 E(x) = (ax + b) mod 26 的简化版本。

特性凯撒密码仿射密码
公式E(x) = (x + b) mod 26E(x) = (ax + b) mod 26
密钥空间26个密钥312个密钥
密钥类型单一移位值两个值(a, b)
字母映射均匀移位非均匀映射
安全性极易破解仍然较弱,但更难

两种密码都是单表替换密码,都容易受到频率分析攻击,但仿射密码更大的密钥空间使暴力破解需要稍多一些的工作。若要更强的加密,请考虑多表密码,如维吉尼亚密码

相关工具与资源