可交换密码识别正确的解密?
理论上,假设我正在使用交换对称密码来创建我自己的加密文件。我知道加密的 rar/zip 可以实现我的想法,但我希望了解幕后细节。如果我只是加密没有元数据的文件,那么我如何知道解密时它是否已正确解密?
我想到的一种方法是将使用的密钥放在文件的前面,然后将密钥与文件一起加密。当我解密时,我可以将解密密钥与文件的开头进行比较,并知道它是否有效,但我对将密钥实际放置在文件中感到不舒服。
另一个想法是将数据的静态部分放在文件的开头,但是当尝试暴力破解文件时,如果有人知道密钥(或实际密钥)中的冲突,则可以将其用作指示符。数据的静态部分,我不喜欢通过模糊来实现安全性。
我的最后一个想法是包含初始未加密文件的哈希值,但对于可能会减慢进程的大文件。使用这种方法,我必须对文件进行哈希和加密,这似乎效率很低。我希望有更好的技术。
验证使用可交换对称密码加密的文件是否已成功解密(无需与原始文件进行比较)的最佳方法是什么?
In theory, let's say I'm using a commutative symmetrical cipher to create my own kind of encrypted file. I know that an encrypted rar/zip would do what I'm thinking of, but I'm looking to understand the under the hood details. If I just encrypt the file with no meta data, then how can I know when I decrypt it that it's properly decrypted?
One approach I thought of was to place the key used at the front of the file and then encrypt the key along with the file. When I decrypt, I can compare the decryption key with the beginning of the file and know if it worked, but I'm uncomfortable with actually placing the key inside the file.
Another idea would be placing a static section of data at the beginning of the file, but that can be used as an indicator when trying to brute force the file to when a collision in keys (or the actual key) is discovered if anybody knows the static section of data and I don't like security through obscurity.
My last thought is to include the hash of the initial unencrypted file, but for large files that can slow down the process. With this approach, I have to hash and encrypt the file and that seems inefficient. I'm hoping there's a better technique.
What would be the best approach to verify that an file that was encrypted with a commutative symmetrical cipher was decrypted successfully (without having the original file to compare to)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用定义明确但随机格式的标头。一种标准方法是使用随机数据和加密哈希(伪代码如下):
这给出了 64 字节的高熵数据。无法使用 crib 来暴力破解加密。要验证您是否拥有正确的密钥,只需解密标头并检查以确保第二个 32 字节是第一个字节的有效 SHA256 哈希值。
Use a header with a well defined, but random format. One standard way to do this is with random data and cryptographic hashes (pseudo-code follows):
This gives 64 bytes of high entropy data. There is no way this can be used crib for brute-forcing the encryption. To validate you have the proper key, just decrypt the header and check to make sure that the second 32 bytes are a valid SHA256 hash of the first.
我仍然建议存储哈希值或校验和。如果将其放在加密数据的末尾,则可以在加密期间读取文件时生成校验和,因此不需要任何额外的文件传递。 (校验和会产生 CPU 开销,但这是最小的。为此目的,您不需要使用像 SHA 这样昂贵的东西;CRC32 就可以了。)
校验和将有助于检测传输中的错误。如果加密数据中的单个位被更改,则超过该点的解密数据可能会是垃圾。魔术头不会检测到这一点,但校验和会检测到。
I would still recommend storing a hash or checksum. If you put it at the end of the encrypted data, you can generate the checksum as you read the file during the encryption, so it doesn't require any extra passes through the file. (There will be CPU overhead for the checksum, but that'll be minimal. You don't need to use something as expensive as SHA for this purpose; CRC32 will do.)
The checksum will help detect errors in transit. If a single bit in the encrypted data is altered, the decrypted data past that point will probably be garbage. A magic header won't detect that, but a checksum will.
有像 CCM 这样的密码模式可以提供完整性。我不确定它们如何满足您对交换性的要求。
There are cipher modes like CCM that provide integrity. I'm not sure how they would fit with your requirement for commutativity.