AES中的密钥验证 [英] Key verification in AES

查看:311
本文介绍了AES中的密钥验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户输入错误的密钥进行AES解密,则会生成一些垃圾数据。我要验证给定的解密密钥,如果密钥不正确,则会引发错误。如何验证用户输入的密钥?

If the user enters a wrong key for AES decryption, some garbage data is generated. I want to verify the given decryption key and throw an error if the key is incorrect. How can I verify the key entered by the user?

推荐答案

使用 HMAC 。基本前提是您通过HMAC运行纯文本,将结果添加到纯文本,然后进行加密。然后在解密时执行相反的操作。如果明文和HMAC结果匹配,那么您就知道您拥有正确的密钥。

Use an HMAC. The basic premise is that you run the plaintext through an HMAC, add the result to the plaintext and then encrypt. Then do the opposite when decrypting. If the plaintext and HMAC result match, then you know you've got the correct key.

或者,如果要在解密之前知道,请使用提供的密钥材料由用户导出另外两个密钥(例如使用PBKDF2)。一种用于加密,另一种用于HMAC。在这种情况下,请先加密,然后使用第二个密钥然后应用HMAC。这样,您可以计算HMAC并检查它是否与之前匹配。

OR, if you want to know prior to decryption, use the key material provided by the user to derive two further keys (using, say PBKDF2). Use one for encryption and another for an HMAC. In this case, encrypt first and then apply the HMAC using the second key. This way you can compute the HMAC and check if it matches before you decrypt.

这篇关于AES中的密钥验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆