简体   繁体   English

MD5 Hashing给出了C#的关键

[英]MD5 Hashing Given a Key in C#

I've been looking for a way to hash a given string in C# that uses a predetermined key. 我一直在寻找一种方法来在使用预定密钥的C#中散列给定字符串。

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. 在我通过互联网尝试找到一个例子的冒险中,我看到了许多MD5CryptoServiceProvider示例,这些示例似乎使用了机器的默认密钥,但没有一个应用特定密钥。 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. 我给它们一个散列字符串和一个ID号,然后用它们分析数据并向我返回一个类似的集合。 So is there anyway to get md5 to hash via a specific key that would be consistent to both. 所以无论如何都要通过一个与两者一致的特定键来使md5散列。

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? 我希望这可以在C#中完成,但是如果用库不可能你可以使用像php或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. 编辑2:正如我在下面的评论所说,这是“盐腌”这个词我没有注意到。 Oh the joys of getting thrown into something new with no directions. 噢,没有方向就被抛入新事物的乐趣。

MD5 is not encryption - it's a hash. MD5不是加密 - 它是一个哈希。 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. 如果您正在将数据传输到另一个人的服务器,那么最好使用像gpg这样的东西来使用您通过电话同意的对称密钥加密文件, 或者使用某些公钥加密。 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. MD5是一个无键的哈希函数 - 根本没有使用密钥。 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. 然后,您将MD5字符串或文件,并将您计算的哈希值与它们发送的哈希值进行比较。 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. 并不意味着没有人篡改他们在途中发送给你的东西,因为MD5对它没有任何“暗示”。 I can md5 anything I want and send it to you. 我可以md5任何我想要的东西并发送给你。

A HMAC is a keyed hash function . HMAC是键控哈希函数 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 . 如果他们向您发送长字符串或文件以及HMAC,您可以自己计算HMAC,比较您的HMAC和他们的HMAC,如果它们匹配, 则数据在传输过程中没有损坏,数据也没有被篡改

MD5 is a hash function and, strictly speaking, is not used to "encrypt" a string. MD5是一个哈希函数,严格来说,它不用于“加密”字符串。 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. 它产生一个128位的“消息摘要”(因此名称中的MD),用作输入字符串的一种指纹。

Tom's right: MD5 is just a one-way hash, you can't decrypt it. 汤姆是对的:MD5只是一个单向哈希,你不能解密它。 Try these links: 试试这些链接:

You can use AES from C# to do the type of encryption you are looking for. 您可以使用C#中的AES来执行您要查找的加密类型。 Here's an article on how. 是一篇关于如何做的文章

You should use one of the classes inherited from SymmetricAlgorithm , for instance : 您应该使用从SymmetricAlgorithm继承的其中一个类,例如:

  • AesCryptoServiceProvider AesCryptoServiceProvider
  • DESCryptoServiceProvider DESCryptoServiceProvider
  • RC2CryptoServiceProvider RC2CryptoServiceProvider
  • TripleDESCryptoServiceProvider TripleDESCryptoServiceProvider

So, why does the following test fail if both input strings are identical? 那么,如果两个输入字符串相同,为什么以下测试失败?

    [TestMethod]
    public void MD5HashTest()
    {
        var hash1 = (new MD5CryptoServiceProvider()).ComputeHash(new System.Text.ASCIIEncoding().GetBytes("now is the time for all good men."));
        var hash2 = (new MD5CryptoServiceProvider()).ComputeHash(new System.Text.ASCIIEncoding().GetBytes("now is the time for all good men."));

        Assert.AreEqual(hash1, hash2);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM