可以用私钥加密/用公钥解密吗?

发布于 2024-08-22 22:50:16 字数 674 浏览 3 评论 0原文

[免责声明:我知道,如果您对加密有任何了解,您可能会告诉我为什么我做错了 - 我已经做了足够多的谷歌搜索,知道这似乎是典型的反应。]

假设以下情况:您有一个想要为给定域发布登录 cookie 的中央机构。在此域中,您不一定信任每个人,但您有一些应该能够读取 cookie 的关键端点。我说了一些,但实际上,“值得信赖”的合作伙伴的数量可能很大。 cookie 不包含太多信息 - 用户名、时间戳、到期日、随机数。当然,出于性能原因,即使在加密之后(在合理范围内),它也应该保持很小。现在,存在两个安全问题:

1)我们不信任该域上的每个网络服务器都拥有用户数据。因此,读取 cookie 的能力应仅限于这些值得信赖的合作伙伴。 2) 虽然我们相信这些合作伙伴能够保护我们用户的数据,但我们仍然希望中心授权点不可伪造(同样,在合理范围内)。

现在,如果我们为权威机构生成一个 RSA 私钥并保密,并将公钥仅分发给“可信合作伙伴”,那么我们应该能够使用私钥进行加密,并让拥有公钥的任何人都可以读取它。我不清楚的是,是否仍然需要对消息进行签名,或者解密行为是否可以证明它是使用私钥生成的?与将对称密钥传播给所有相关方并使用它进行加密,而仅使用私钥进行签名相比,该方案是否会更好或更差?当然,请随意告诉我这是一个愚蠢的想法,但请记住,实际的论据可能比重复爱丽丝和鲍勃更有说服力。

哦,欢迎提供实现指针,尽管可以在 Google 上找到基础知识,如果涉及任何“陷阱”,那将很有用!

[Disclaimer: I know, if you know anything about crypto you're probably about to tell me why I'm doing it wrong - I've done enough Googling to know this seems to be the typical response.]

Suppose the following: you have a central authority that wants to issue login cookies for a given domain. On this domain, you don't necessarily trust everyone, but you have a few key end-points who should be able to read the cookie. I say a few, but in practice this number of "trusted" partners may be large. The cookie doesn't contain much information - a username, a timestamp, an expiry, a random number. It should remain small of course, for performance reasons, even after encryption (within reason). Now, there are two security issues:

1) We don't trust every webserver on this domain with user data. For this reason, the ability to read the cookie should be restricted to these trusted partners.
2) While we trust these partners to protect our user's data, we'd still like the central point of authority to be unforgeable (again, within reason).

Now, if we generate a private RSA key for the authority and keep it secret, and distribute the public key only to the "trusted partners", we should be able to encrypt with the private key and have it readable by anyone with the public key. What I'm unclear on is, would it still be necessary to sign the message, or would the act of decrypting be evidence that it was generated with the private key? Is this any way in which this scheme would be better or worse than disseminating a symmetric key to all parties involved and using that to encrypt, while using the private key merely to sign? And of course feel free to tell me all the ways this is a stupid idea, but bear in mind that practical arguments will probably be more convincing than rehashing Alice and Bob.

Oh, and implementation pointers would be welcome, though one can find the basics on Google, if there are any "gotchas" involved that would be useful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

十级心震 2024-08-29 22:50:16

Nate Lawson 解释了此处和< a href="http://rdist.root.org/2007/05/03/rsa-public-keys-are-not-private-implementation/" rel="noreferrer">这里为什么你可以'不要安全地使用公钥作为保密的解密密钥(这是一个微妙的点,并且在您之前很多其他人都犯过一个错误,所以不要感到难过!)。

只需使用您的公钥来签署真实性,并使用单独的对称密钥来保密。

我已经阅读了足够多关于针对公钥系统(尤其是 RSA)的有趣攻击的文章,我完全同意这个结论:

公钥密码系统和 RSA
特别是极其脆弱。做
不要以与它们不同的方式使用它们
被设计。

(这意味着:用公钥加密,用私钥签名,其他任何事情都是在玩火。)

附录:

如果您有兴趣减少生成的 cookie 的大小,您应该考虑使用 ECDSA 而不是 RSA 来生成签名 - ECDSA 签名比同等安全系数的 RSA 签名小得多。

Nate Lawson explains here and here why you can't securely use the public key as a closely-held secret decryption key (it's a subtle point, and a mistake plenty of others have made before you, so don't feel bad!).

Just use your public key to sign for authenticity, and a separate symmetric key for the secrecy.

I've read enough on interesting attacks against public key systems, and RSA in particular, that I agree absolutely with this conclusion:

Public key cryptosystems and RSA in
particular are extremely fragile. Do
not use them differently than they
were designed.

(That means: Encrypt with the public key, sign with the private key, and anything else is playing with fire.)

Addendum:

If you're interesting in reducing the size of the resulting cookies, you should consider using ECDSA rather than RSA to produce the signatures - ECDSA signatures are considerably smaller than RSA signatures of an equivalent security factor.

爱要勇敢去追 2024-08-29 22:50:16

在密码学中,你就是你所知道的。在您的场景中,您有一个能够发布 cookie 的中央机构,并且您不希望其他实体能够执行相同的操作。所以中央当局必须“知道”一些私人数据。此外,您希望“受信任的 Web 服务器”能够访问 cookie 的内容,并且您不希望任何人都读取 cookie。因此,“可信网络服务器”也必须拥有自己的私有数据。

正常的方式是,权威机构对 cookie 应用数字签名,并使用可信 Web 服务器已知的密钥对 cookie 进行加密。您的想法如下所示:

  • 有一个 RSA 模数 n 和两个常见的 RSA 指数 de (例如 n >ed = 1 模 p-1q-1,其中 n=pq)。中央机构知道d,受信任的网络服务器知道e,模数n是公开的。
  • 中央机构通过将 cookie 填充为整数 cn 并计算 s = c^d mod 来处理 cookie n
  • 受信任的 Web 服务器通过计算 c = s^e mod n 来访问 cookie 数据。

尽管这样的方案可能可行,但我发现其中存在以下问题:

  • 对于基本安全性,e 必须很大。在通常的 RSA 描述中,e 是公共指数并且很小(例如 e = 3)。当它是公开的时,小指数没有问题,但由于您不希望第三方访问 cookie 内容,因此您必须使e足够大以抵抗穷举搜索。同时,受信任的 Web 服务器必须不知道 pq,而只能知道 n。这意味着受信任的网络服务器将需要计算具有大模数和大指数的事物,并且不知道模数因子。这似乎是一个小问题,但它使许多 RSA 实现库失去了资格。您将“靠自己”处理大整数(以及所有称为“侧通道泄漏”的实现问题)。
  • RSA 签名的抵抗力和 RSA 加密的抵抗力已经得到了很好的研究,但没有一起研究。碰巧填充是必不可少的,并且您不使用相同的填充方案进行加密和签名。在这里,您需要一个有利于签名和加密的填充方案。密码学家通常认为,当数百名训练有素的密码学家对该方案进行了几年的研究,并且没有发现明显的弱点(或修复了发现的任何弱点)时,就可以实现良好的安全性。自制的方案几乎总是无法实现安全。
  • 如果一个秘密被很多人知道,那么它就不再是秘密了。在这里,如果所有 Web 服务器都知道e,那么如果不选择新的e 并将新值传达给所有剩余的可信 Web 服务器,则无法撤销可信 Web 服务器权限。对于共享对称密钥,您也会遇到这个问题。

目前正在研究同类型中保证机密性和可验证完整性的问题。您可以查找signcryption。目前还没有既定的标准。

基本上,我认为您会对更经典的设计感到更满意,数字签名仅用于签名,并且(对称或非对称)加密用于机密部分。这将允许您使用现有的库,并使用尽可能少的自制代码。

对于签名部分,您可能需要使用 DSA 或 ECDSA:它们产生更短的签名(对于安全性相当于 1024 位 RSA 签名的 DSA 签名,通常为 320 位)。从中央机构的角度来看,ECDSA 还可以提供更好的性能:在我的 PC 上,使用单核,OpenSSL 每秒处理超过 6500 个 ECDSA 签名(在 P-192 NIST 曲线中),并且“仅”每秒处理 1145 个 RSA 签名。第二个(带有 1024 位密钥)。 ECDSA 签名由两个 192 位整数组成,即需要编码 384 位,而 RSA 签名则为 1024 位长。 P-192 中的 ECDSA 被认为至少与 RSA-1024 一样强大,甚至可能更强。

In cryptography you are what you know. In your scenario, you have a central authority which is able to issue your cookies, and you want no other entity to be able to do the same. So the central authority must "know" some private data. Also, you want the "trusted web servers" to be able to access the contents of the cookies, and you do not want just anybody do read the cookies. Thus, the "trusted web servers" must also have their own private data.

The normal way would be that the authority applies a digital signature on the cookies, and that the cookies are encrypted with a key known to the trusted web servers. What your are thinking about looks like this:

  • There is a RSA modulus n and the two usual RSA exponents d and e (such that ed = 1 modulo p-1 and q-1 where n=pq). The central authority knows d, the trusted web servers know e, the modulus n is public.
  • The cookie is processed by the central authority by padding it into an integer c modulo n, and computing s = c^d mod n.
  • The trusted web servers access the cookie data by computing c = s^e mod n.

Although such a scheme may work, I see the following problems in it:

  • For basic security, e must be large. In usual RSA descriptions, e is the public exponent and is small (like e = 3). A small exponent is no problem when it is public, but since you do not want cookie contents to be accessible by third parties, you must make e big enough to resist exhaustive search. At the same time, trusted web servers must not know p and q, only n. This means that trusted web servers will need to compute things with a big modulus and a big exponent, and without knowing the modulus factors. This seems a minor point but it disqualifies many RSA implementation libraries. You will be "on your own", with big integers (and all the implementation issues known as "side-channel leaks").
  • Resistance of RSA signatures, and resistance of RSA encryption, have been well studied, but not together. It so happens that the padding is essential, and you do not use the same padding scheme for encryption and for signatures. Here, you want a padding scheme which will be good for both signature and encryption. Cryptographers usually consider that good security is achieved when hundreds of trained cryptographers have looked at the scheme for a few years, and found no blatant weakness (or fixed whatever weaknesses were found). Home-cooked schemes almost always fail to achieve security.
  • If many people know a secret, then it is not a secret anymore. Here, if all web servers know e, then you cannot revoke a trusted web server privileges without choosing a new e and communicating the new value to all remaining trusted web servers. You would have that problem with a shared symmetric key too.

The problem of ensuring confidentiality and verifiable integrity in the same type is currently being studied. You may look up signcryption. There is no established standard yet.

Basically I think you will be happier with a more classical design, with digital signatures used only for signing, and (symmetric or asymmetric) encryption for the confidentiality part. This will allow you to use existing libraries, with the least possible homemade code.

For the signature part, you may want to use DSA or ECDSA: they yield much shorter signatures (typically 320 bits for a DSA signature of security equivalent to a 1024-bit RSA signature). From the central authority point of view, ECDSA also allows better performance: on my PC, using a single core, OpenSSL crunches out more than 6500 ECDSA signatures per second (in the P-192 NIST curve) and "only" 1145 RSA signatures per second (with a 1024-bit key). The ECDSA signatures consist in two 192-bit integers, i.e. 384 bits to encode, while the RSA signatures are 1024-bit long. ECDSA in P-192 is considered at least as strong, and probably stronger, than RSA-1024.

内心旳酸楚 2024-08-29 22:50:16

您应该使用某种数字签名方案或其他某种机制来解决您的场景的完整性问题。

加密本身还不够。
你怎么知道解密的消息就是它应该是什么?
解密使用正确密钥加密的 cookie 肯定会提供“有效”cookie,但是当您解密使用错误密钥加密的 cookie 时会发生什么?或者只是一些无意义的数据?好吧,你可能会得到一个看起来有效的 cookie! (时间戳在您认为有效的范围内,用户名合法,随机数是......呃......一个数字,等等)。

在我所知道的大多数非对称加密算法中,没有内置验证。这意味着使用错误的密钥解密消息不会“失败” - 它只会给您一个错误的明文,必须将其与有效的明文区分开来。这就是完整性发挥作用的地方,最常见的是使用数字签名。

顺便说一句,RSA 经过长期研究,并且有几个“陷阱”,因此,如果您计划从头开始实现它,您最好提前阅读如何避免创建“相对容易破解”的密钥。

You should use a digital sigantures scheme of some sort, or some other mechanism that is aimed to solve the integrity problem of your scenario.

The encryption itself isn't enough.
How would you know that the decrypted messege is what it should be?
Decrypting a cookie encrypted with the right key will surely provide a "valid" cookie, but what happens when you decrypt a cookie encrypted with the wrong key? or just some meaningless data? well, you might just get a cookie that looks valid! (the timestamps are in the range you consider valid, the username is legal, the random number is... uh... a number, etc.).

In most asymmetric encryption algorithms I know off, there is no built-in validation. That means that decrypting a message with the wrong key will not "fail" - it will only give you a wrong plaintext, which you must distinguish from a valid plaintext. This is where integrity comes to play, most commonly - using digital signatures.

BTW, RSA is long studied and has several "gotchas", so if you plan to implement it from scratch you better read ahead on how to avoid creating "relatively easy to break" keys.

岁吢 2024-08-29 22:50:16

根据定义,公钥是公开的。如果您使用私钥加密并使用公钥解密,那么就无法避免窥探。它都说:“这些数据来自持有私钥 X 的人 X”,任何人都可以验证这一点,因为密钥的另一半是公开的。

如何阻止你不知道的人不信任将公钥 X 放在您不信任的服务器上?

如果您希望在两个服务器之间建立安全的通信线路,则需要让所有这些受信任的服务器都有自己的公钥/私钥对,对于一台这样的服务器,我们将使用密钥对 Y。

然后,服务器 X 可以使用私钥 X 和公钥 Y 加密消息。这表示“服务器 X 发送了一条只有 Y 可以读取的消息,并且 Y 可以验证它来自 X”。

(该消息应包含一个短期对称密钥,因为公钥加密非常耗时。)

这就是 SSL 的作用。它使用公钥加密来设置会话密钥。

话虽这么说,使用图书馆。这东西很容易搞砸。

Public keys are by definition, public. If you're encrypting with a private key and decrypting with a public key, that's not safe from prying eyes. All it says: "this data is coming from person X who holds private key X" and anyone can verify that, because the other half of the key is public.

What's to stop someone you don't trust putting public key X on a server you don't trust?

If you want a secure line of communication between two servers, you need to have all of those trusted servers have their own public/private key pairs, we'll say key pair Y for one such server.

Server X can then encrypt a message with private key X and public key Y. This says "server X sent a message that only Y could read, and Y could verify it was from X."

(And that message should contain a short-lived symmetric key, because public key crypto is very time-consuming.)

This is what SSL does. It uses public key crypto to set up a session key.

That being said, use a library. This stuff is easy to screw up.

我一直都在从未离去 2024-08-29 22:50:16

对于您的问题的答案“解密行为是否可以证明它是使用私钥生成的”,如果接收者可以对数据进行简单的验证,答案是肯定的。假设您有“用户名:John,时间戳:,到期日:dd/mm/yyyy”。现在,如果使用错误的公钥进行解密,您将有可能得到“用户名:<一些字母>,时间戳:<仅数字>,过期时间:??/??/????”为零。您可以使用正则表达式 (regex) 进行验证,例如“用户名:[a-zA-Z]+,时间戳:[0-9]+,过期时间:...”,然后删除验证失败。您甚至可以检查日期是否在 1 到 31 之间,月份是否在 1 到 12 之间,但您无法使用它,因为如果使用错误的公钥,正则表达式通常会在“用户名:”处失败。如果验证成功,您仍然需要检查时间戳并确保没有重放攻击。

但是,请考虑以下事项:

  1. RSA 公钥加密不是为结构化数据的批量加密而设计的,因为它可能被攻击者利用。公钥加密通常以两种方式使用:1)用于安全传输将在批量加密中使用的对称密钥(根据定义,其没有结构);这是通过使用公钥加密对称密钥来完成的,2)通过使用私钥加密文档的哈希值(同样没有结构)而不是文档来对文档进行数字签名。在你的情况下,你正在加密具有明确结构的cookie。
  2. 您依赖于公钥不会落入错误的人或实体的手中。
  3. 公钥加密比对称密钥加密慢大约 1000 倍。这可能不是您的情况的一个因素。

如果您仍然想使用这种方法,并且能够将公钥分发给“受信任的合作伙伴”,您应该生成一个随机会话密钥(即对称密钥),并使用私钥对其进行加密密钥并发送给所有拥有公钥的接收者。然后,您使用会话密钥加密 cookie。您可以定期更改会话密钥。更罕见的是,您还可以更改公钥:您可以生成新的公钥/私钥对,并使用会话密钥对公钥进行加密,然后将其发送给所有收件人。

The answer to your question "would the act of decrypting be evidence that it was generated with the private key", the answer is yes if the recipient can do simple validation of the data. Say you have "User name : John, timestamp : <number>, expiry : dd/mm/yyyy". Now if a wrong public key is used to decrypt, the probability that you will get "User name : <some letters>, timestamp : <only numbers>, expiry : ??/??/????" is zero. You can validate using a regular expression (regex) like "User name : [a-zA-Z]+, timestamp : [0-9]+, expiry : .... " and drop it validation fails. You can even check if the day is between 1 and 31, month is between 1 and 12 but you won't get to use it as regex will typically fail at "User name : " if wrong public key is used. If validation succeeds you still have to check the timestamp and ensure that you don't have a replay attack.

However, consider the following:

  1. RSA public key crypto is not designed for bulk encryption of structured data as it can be exploited by attacker. public key crypto is typically used in 2 ways: 1) for securely transporting the symmetric key (which by definition has no structure) which will be used in bulk encryption; this is done by encrypting the symmetric key using the public key and 2) digitally signing a document by encrypting not the document but the hash of the document (again something which has no structure) using the private key. In your case you are encrypting the cookie which has a definite structure.
  2. You are depending on the public key not getting into the hands of the wrong person or entity.
  3. public key encryption is about 1000 times slower that symmetric key encryption. This may not be a factor in you case.

If you still want to use this approach, and are able to distribute the public key only to the "trusted partners", you should generate a random session key (i.e. symmetric key), encrypt it using the private key and send to all recipients who have the public key. You then encrypt the cookie using the session key. You can change the session key periodically. More infrequently you can change the public key also: you can generate a new public/private key pair and encrypt public key with the session key and send it to all recipients.

我早已燃尽 2024-08-29 22:50:16

我假设您信任“可信合作伙伴”来解密和验证 cookie,但不希望他们能够生成自己的 cookie?如果这不是问题,您可以使用更简单的系统:将密钥分发给所有各方,并使用它来加密 cookie 并为其生成 HMAC。不需要公钥加密,不需要多个密钥。

I presume you trust the 'trusted partners' to decrypt and verify the cookie, but don't want them to be able to generate their own cookies? If that's not a problem, you can use a much simpler system: Distribute a secret key to all parties, and use that to both encrypt the cookie and generate an HMAC for it. No need for public key crypto, no need for multiple keys.

与他有关 2024-08-29 22:50:16

作为密钥分发方法的替代方法(可能适合也可能不适合您的应用程序),请考虑使用 Kerberos,它使用对称密钥加密、控制所有密钥材料的单一高度保护的堡垒服务器以及一组巧妙的协议(请参阅李约瑟协议)

As an alternative to your key distribution approach, which may or may not be suitable in your application, consider using Kerberos, which uses symmetric key encryption, a single highly protected bastion server that controls all the keying material, and a clever set of protocols (See the Needham-Schroder protocol)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文