使用公钥加密大文件
我需要使用公钥加密 100KB 文件。我读过一些帖子,声称使用公钥直接加密大文件是不切实际的,首选方法是使用对称密钥加密文件,然后使用公钥加密该对称密钥。 似乎一种简单的解决方案是将大文件分解成碎片并使用相同的公钥对每个文件进行加密。我的问题是这个解决方案是否以及为什么是错误的?
I need to encrypt a 100KB file using a public key. I've been reading some posts claiming that it is not practical to directly encrypt large files using a public key, and that the preferred method is to encrypt the file using a symmetric key and then encrypt this symmetric key using the public key.
It seems that a naive solution would be to break the large file to pieces and encrypt each one of them using the same public key. My question is whether and why this solution is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您提到的混合方法(生成随机对称密钥,使用它来加密数据,并仅非对称地加密密钥)具有巨大的性能优势。
您也可以“将大文件分解成碎片并使用相同的公钥对每个文件进行加密”,这没有任何问题,但速度要慢得多。
The hybrid approach you mention (generate a random symmetric key, use this to encrypt the data, and encrypt only the key asymmetrically) has a massive performance advantage.
You could "break the large file to pieces and encrypt each one of them using the same public key" as well, there is nothing wrong with that, but it is much slower.
如果我理解正确,您想使用其他人的公钥加密文件,然后使用他们的私钥解密?
使用对称加密并且仅对(对称)密钥使用公钥加密的优点是性能:对称加密在计算上的资源密集程度要低得多(权衡:您必须保守密钥的秘密 - 这就是第二个,不对称步骤是)。
分解文件会增加管理开销(您如何确定会有多少块?您已将它们全部传输?)并且不会增加任何安全性。相反。
If I understand you right, you want to encrypt the file with someone else's public key, to be decrypted by their private key?
The advantage of using symmetric encryption and only using public key cryptography for the (symmetric) key is performance: symmetric cryptography is computationally much less resource-intensive (trade-off: you have to keep the key secret -- and that's what the second, asymmetric step is for).
Breaking up the file adds management overhead (how can you be sure how many chunks there will be? that you have transmitted them all?) and doesn't add any security. On the contrary.
非对称加密太慢,最常用的方法是使用非对称加密随机对称密钥,然后使用该对称密钥加密数据。
而且,最好的方法是使用众所周知的协议/标准(例如 OpenPGP)。
Asymmetric cryptography is too slow, the most used approach is to encrypt random symmetric key with asymmetric, and encrypt your data with that symmetric key.
And, as well, the best way is to use well-known protocol/standard for that purpose (OpenPGP for instance).
将文件分割成更小的部分,然后使用一些非对称密码对其进行加密与加密过程的运行时成本无关。最佳实践是使用相对较强的密钥使用良好的对称密码来加密数据,并使用非对称密码(使用您的公钥)对对称加密中使用的秘密密钥进行加密。
Splitting file into smaller pieces and then encrypting them with some asymmetric cipher has nothing to do with the runtime cost of the encryption process. Best practice is encrypting the data with a good symmetric cipher using a relatively strong key and encrypting the secret key you used in symmetric encryption with an asymmetric cipher(using your public key).
除了对称密钥加密的速度提升之外,还有另一个可能的好处:通过首先使用随机安全生成的对称密钥加密消息,然后您可以为多个收件人加密对称密钥,一次使用每个收件人自己的公共非对称密钥,无需重新加密整个消息。
Aside from the speed-boost of symmetric key encryption, there's another possible benefit: By first encrypting the message with a random securely-generated symmetric key, you can then encrypt the symmetric key for multiple recipients, once in each recipient's own public asymmetric key, without having to re-encrypt the entire message.