C#AES Rijndael - 检测无效密码 [英] C# AES Rijndael - detecting invalid passwords

查看:248
本文介绍了C#AES Rijndael - 检测无效密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当用户输入错误的密码时,大部分时间都是 CryptographicException 抛出消息填充无效,无法删除。



然而,CryptStream的概率非常小不会使用错误的密码抛出异常,而是返回错误的解密流。换句话说,它解密为垃圾。



任何想法如何检测/防止这种情况?我可以想到的最简单的方法是在加密时在消息开头放置一个魔术数字,并检查它是否仍然在解密之后。



但是如果有一个更简单的方法,我很乐意听到它!

解决方案

HMAC是你需要的。正是为了这个目的。它结合了密钥和消息(在这种情况下,这将是您的密码),并且以确保内容的真实性和完整性的方式进行散列,只要使用的散列函数是安全的。您可以将HMAC附加到加密数据,并且可以稍后验证解密是否正确。




I'm using Rijndael to encrypt some sensitive data in my program.

When the user enters an incorrect password, most of the time a CryptographicException is thrown with the message "Padding is invalid and cannot be removed.".

However, with very small probability, the CryptStream does not throw an exception with the wrong password, but instead gives back an incorrectly decrypted stream. In other words, it decrypts to garbage.

Any idea how to detect/prevent this? The simplest way I can think of would be to put a "magic number" at the start of the message when encrypting, and check if it's still there after decrypting.

But if there's an easier way, I'd love to hear it!

解决方案

HMAC is what you need. It is exactly made for this purpose. It combines the key and the message (which in this case, will be your password) and hashes them in a way that it will ensure the authenticity and integrity of the content, as long as the hash function used is secure. You can attach the HMAC to the encrypted data, and it can be used later to validate if the decryption was made correctly.

这篇关于C#AES Rijndael - 检测无效密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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