C# 中给定密钥的 MD5 哈希
我一直在寻找一种在 C# 中使用预定键对给定字符串进行哈希处理的方法。
在我通过互联网尝试寻找示例的过程中,我看到了很多 MD5CryptoServiceProvider 示例,这些示例似乎使用机器的默认密钥,但没有一个应用特定密钥。 我需要有一个特定的密钥来编码数据,以便将其同步到其他人的服务器。 我给他们一个散列字符串和一个 ID 号,他们用它来分析数据并向我返回一组类似的数据。 那么有没有办法通过一个与两者都一致的特定密钥来让 md5 进行哈希处理。
我更喜欢用 C# 来完成此操作,但如果使用库无法完成此操作,您可以使用某些 Web 语言(如 php 或 asp)来完成此操作吗?
编辑:误解了我陷入的场景,经过一番坐下来思考为什么他们会让我使用密钥,看来他们希望将密钥附加到字符串末尾并进行散列。 这样,服务器可以将其拥有的密钥与传递的数据一起附加,以确保其是有效的访问计算机。 无论如何...谢谢大家^_^
Edit2:正如我下面的评论所说,这是我忘记的术语“加盐”。 哦,在没有方向的情况下投入新事物的乐趣。
I've been looking for a way to hash a given string in C# that uses a predetermined key.
On my adventures through the internet trying to find an example i have seen lots of MD5CryptoServiceProvider examples which seem to use a default key for the machine, but none of them that apply a specific key. I need to have a specific key to encode data as to synchronize it to someone else's server. I hand them a hashed string and an ID number and they use that analyze the data and return a similar set to me. So is there anyway to get md5 to hash via a specific key that would be consistent to both.
I would prefer this to be done in C#, but if its not possible with the libraries can you do so with some web languages like php or asp?
Edit: Misunderstood the scenario I was thrown into and after a little sitting and thinking about why they would have me use a key it appears they want a key appended to the end of the string and hashed. That way the server can appended the key it has along with the data passed to ensure its a valid accessing computer. Anyways... thanks all ^_^
Edit2: As my comment below says, it was the term 'salting' I was oblivious to. Oh the joys of getting thrown into something new with no directions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
MD5 不是加密——它是哈希值。 它不允许字符串被解密。
您正在寻找一种对称加密算法。 它使用相同的密钥来加密和解密。 尝试使用加密函数而不了解它们是危险的。 即使你认为你理解它们,你也可能会犯错误。
如果您要将数据传输到另一个人的服务器,您可能最好使用 gpg 之类的东西来使用你们通过电话达成一致的对称密钥,或者也许是一些公钥加密。 这样,您就不需要编写任何加密代码,并且更安全(请注意,不是完全安全,但更安全)。
编辑:我仍在尝试解读您的要求。
MD5 是一种无密钥哈希函数 - 根本没有使用密钥。 假设服务器向您发送一个巨大的字符串或文件及其哈希值。 然后,您将对字符串或文件进行 MD5,并将您计算的哈希值与它们发送的哈希值进行比较。 如果它们匹配 - 数据在传输过程中未损坏。 这并不意味着没有人在传输过程中篡改他们发送给您的内容,因为 MD5 没有“秘密武器”。 我可以 md5 我想要的任何内容并将其发送给您。
HMAC 是一个密钥哈希函数。 它有一个只有您和您正在通信的团队才应该知道的秘密成分 - 秘密密钥。 如果他们向您发送长字符串或文件以及 HMAC,您可以自己计算 HMAC,将您的 HMAC 与他们的进行比较,如果匹配,则数据在传输过程中没有损坏,数据也没有被篡改< /强>。
MD5 is not encryption - it's a hash. It doesn't allow a string to be decrypted.
You're looking for a symmetric encryption algorithm. It uses the same key to encrypt and decrypt. Trying to use encryption functions without understanding them is dangerous. Even if you think you understand them, you can make a mistake.
If you're transferring data to another person's server, you may be better off using something like gpg to encrypt the file using a symmetric key you both agree on over the phone, or perhaps some public-key crypto. This way, you don't write any crypto code, and it's safer (not completely secure, mind you, but safer).
Edit: I'm still trying to decipher your requirements.
MD5 is an unkeyed hash function - there is not key in use at all. So let's say the server sends you a giant string, or a file, and a hash of it. You would then MD5 the string or file, and compare the hash you computed with the hash they sent. If they match - the data was not corrupted in transit. That doesn't mean no one tampered with what they sent you in transit, because MD5 has no "secret sauce" to it. I can md5 anything I want and send it to you.
A HMAC is a keyed hash function. It has a secret ingredient that only you and the group you're communicating with should know - the secret key. If they send you a long string or file, and a HMAC, you can compute the HMAC yourself, compare your HMAC and theirs, and if they match, the data was not corrupted in transit, nor was the data tampered with.
MD5 是一个哈希函数,严格来说,不用于“加密”字符串。 它生成一个 128 位“消息摘要”(因此名称中包含 MD),用作输入字符串的一种指纹。
MD5 is a hash function and, strictly speaking, is not used to "encrypt" a string. It produces a 128-bit "Message Digest" (hence the MD in the name) that is used as a kind of fingerprint for the input string.
Tom 是对的:MD5 只是一种单向哈希,您无法解密它。 尝试以下链接:
Tom's right: MD5 is just a one-way hash, you can't decrypt it. Try these links:
您可以使用 C# 中的 AES 来执行您正在寻找的加密类型。 这是关于如何操作的一篇文章。
You can use AES from C# to do the type of encryption you are looking for. Here's an article on how.
您应该使用从
SymmetricAlgorithm
继承的类之一,例如:You should use one of the classes inherited from
SymmetricAlgorithm
, for instance :那么,如果两个输入字符串相同,为什么以下测试会失败?
So, why does the following test fail if both input strings are identical?