Horrible public key tricks

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Horrible public key tricks: 2026-11-08 — 2026-11-09

糟糕的公钥技巧:2026-11-08 — 2026-11-09

So, I’ve always been somewhat annoyed at SSL/TLS. It requires everyone to go through a complex “ACME” dance just to put up a public website. Worse, certificates expire every few months, so it’s not a one and done thing: even with automation, maintaining a certificate requires care to ensure the ACME client always has access to DNS records or the web server. … and the whole system doesn’t even work because certificates are issued over plaintext connections, and trusted certificate authorities use very secure keys like “4”.

一直以来,我对 SSL/TLS 都感到有些厌烦。它要求每个人都必须经历复杂的“ACME”流程才能搭建一个公共网站。更糟糕的是,证书每隔几个月就会过期,所以这并非一劳永逸的事情:即使有了自动化,维护证书也需要小心谨慎,以确保 ACME 客户端始终能够访问 DNS 记录或 Web 服务器。……而且整个系统甚至根本无法运作,因为证书是在明文连接上签发的,而受信任的证书颁发机构使用的却是像“4”这样非常“安全”的密钥。

There is prior art of protest SSL implementations, but I do actually want to encrypt web traffic. Passive eavesdropping is a real risk… but preventing it doesn’t require the whole certificate thing: therefore, the best choice is a protest certificate. The only part of the certificate I directly control is the public key. Therefore, the simplest way to obtain a bad certificate is use a vanity private key such as 313333333337 (one of the most eleet primes). However, while the resulting public key would be weak, no one would know unless they try to break it. A good protest should be obvious.

虽然已有抗议性质的 SSL 实现先例,但我确实希望加密 Web 流量。被动窃听是一个真正的风险……但防止它并不需要整套证书机制:因此,最好的选择是“抗议证书”。我唯一能直接控制的证书部分就是公钥。因此,获取一张“糟糕”证书的最简单方法是使用一个虚荣私钥,例如 313333333337(最“极客”的素数之一)。然而,虽然由此产生的公钥会很脆弱,但除非有人尝试破解它,否则没人会知道。一个好的抗议应该是显而易见的。

First, let’s look at mathematical framework of RSA. To generate a key, pick two large prime numbers (p and q) and multiply them to yield the modulus (n). Pick a small number, e as public exponent. The RSA public key is composed of the modulus (n) and the public exponent.

首先,让我们看看 RSA 的数学框架。要生成密钥,需选取两个大素数(p 和 q)并将它们相乘得出模数(n)。选取一个小数 e 作为公钥指数。RSA 公钥由模数(n)和公钥指数组成。

Ok, back to vanity keys. The standard value for e is 65537 (0x10001), because the small number of set bits makes encryption faster. In fact, the CA I used will refuse to sign keys with a non-standard public exponent, so I can’t do anything funny there. The modulus, n, is either 2,048 or 4,096 “user controlled” bits, so it’s the ideal place to hide a message.

好了,回到虚荣密钥上来。e 的标准值是 65537 (0x10001),因为设置的位数较少使得加密速度更快。事实上,我使用的证书颁发机构会拒绝签署非标准公钥指数的密钥,所以我无法在那里搞什么花样。模数 n 有 2048 或 4096 位“用户可控”空间,因此它是隐藏信息的理想场所。

However, to use the certificate, I’ll need to find two large primes that multiply out to the vanity modulus. My method starts by picking two random primes and calculating the resulting n value. Then, the bytes of n are modified to introduce message into the base64 encoding. Of course, the modified n is not the product of p and q, but this is fixable by setting q to n/p. The resulting q value is very unlikely to be a prime (or integer), so I round it and find the next prime after the ideal q: which usually only requires changing the value of q by a few hundred.

然而,要使用该证书,我需要找到两个相乘后等于虚荣模数的大素数。我的方法是先选取两个随机素数并计算出结果 n。然后,修改 n 的字节,将信息植入 Base64 编码中。当然,修改后的 n 不再是 p 和 q 的乘积,但这可以通过将 q 设置为 n/p 来修复。所得的 q 值极不可能是素数(或整数),所以我对其进行四舍五入,并找到理想 q 之后的下一个素数:这通常只需要将 q 的值改变几百即可。

Since p and q are similar in size, a small change q only affects the least-significant half of the product. As long as the message is embedded into the upper half of n, it’s likely to survive undamaged. The original p and newly selected q can now be used to construct a functional RSA private key in the usual manner. From here, it’s really just a matter of getting a certificate authority to sign the key.

由于 p 和 q 的大小相近,q 的微小变化只会影响乘积的低位部分。只要信息嵌入在 n 的高位部分,它就很可能完好无损地保留下来。现在可以使用原始的 p 和新选定的 q,以常规方式构建功能性的 RSA 私钥。从这里开始,剩下的就只是让证书颁发机构签署该密钥的问题了。

… although since base64 operates on groups of three bytes, it is possible for the message to be misaligned and not readable. However, since the layout of the certificate is static, it is possible to adjust the message’s placement until it works: this should take less than four attempts. The result is a func

……尽管由于 Base64 是按三个字节一组进行操作的,信息可能会出现错位而无法读取。然而,由于证书的布局是静态的,可以通过调整信息的位置直到其生效:这通常不需要超过四次尝试。结果是一个功能性的……