2

(i'm just trying to find what am I missing...)

Assuming John have a clear text message , he can create a regular hash ( like md5 , or sha256) and then encrypt the message.

John can now send Paul the message + its (clear text)hash and Paul can know if the message was altered. ( decrypt and then compare hashes).

Even if an attacker can change the encrpyted data ( without decrypt) - - when paul will open the message - and recalc the hash - it wont generate the same hash as the one john sent him.

so why do we need hash by key ?

enter image description here

enter image description here

1
  • What do you mean by "hash by key"? In your example key to the hash function is the clear text message. Nov 10, 2012 at 8:08

3 Answers 3

1

It looks like you don't have to it is just a good idea to because by including the key in the hash it shows that the data was indeed encrypted with the original key - almost indefinitely. Obviously your example above would work, but I would say you can't be 100% certain that the message wasn't intelligently manipulated, or brute force trial-and-errored, to produce a decrypt on the other side that appears correct but doesn't trigger a hash check failure.

An HMAC function is used by the message sender to produce a value (the MAC) that is formed by condensing the secret key and the message input. The MAC is typically sent to the message receiver along with the message. The receiver computes the MAC on the received message using the same key and HMAC function as was used by the sender, and compares the result computed with the received MAC. If the two values match, the message has been correctly received, and the receiver is assured that the sender is a member of the community of users that share the key.

FIPS PUB 198
FEDERAL INFORMATION PROCESSING STANDARDS PUBLICATION
"The Keyed-Hash Message Authentication Code (HMAC)"

Using the above method means you have an extra check for safety. After you decrypt the message you then append the original key to the message and run your hashing function. You then compare the new hash with the one sent. This is a better check because you know that the attacker would have to know the key (or be extremely lucky) in order to generate something that passed the hash check. It's basically an attempt to try and avoid those attackers that might know hashing functions very well, and limit what alterations they can make.

2
  • hi can you go please to chat.stackoverflow.com/rooms/19369/…
    – Royi Namir
    Nov 10, 2012 at 8:49
  • @RoyiNamir Apologies, I was out for the rest of the day yesterday.. and am out again today. If you have questions though I should be able to answer them during the week however.
    – Pebbl
    Nov 11, 2012 at 9:57
0

If you ask why the keys are hashed, it allows a database or operating systems to store passwords in more secure format. The system can check the validity of the key by comparing the hash of the key to the stored hash of the key.

Moreover, a secure system not only hashes a key, but a key+known random pattern (=salt), which prevents people from generating dictionaries of hashes of the most often used passwords. Even if one uses password = password, the system first append it to "passwordAK(43mafk2;" and calculates the hash. That hash doesn't anymore match to anybody else's precomputed dictionary, but the attacker must concatenate his own password dictionary to "AK(43mafk2;" and calculate the hashes all over again for every password in the system.

2
  • IMHO - SALT arent related. they just exists to prevent rainbow tables and prevent that same data will yield same hash. my question is WHY DO WE NEED TO CALC HASH ALSO BY THE KEY ?
    – Royi Namir
    Nov 10, 2012 at 8:09
  • Can you elaborate, what you mean by "hash by key"? Nov 10, 2012 at 8:12
0

The reason to have a hash of the original un-encrypted text is for added security. The issue here is not if someone manipulates the encrypted data - that action will rarely decrypt into something meaningful, but rather prevent someone who has the key from decrypting text, altering it and re-encrypting it with the same key.

so basically, even if someone has the means to decrypt your text, if they do that, change your text, re-encrypt and pass the encrypted data to final destination, you can verify whether data was manipulated or not.

EXAMPLE: I have File #1 with text "Samuel" in it - which is a name of the spy mole in our organization. Lets assume I encrypt it into File #2 with text "qwerty". I pass along File #2 to Peter for delivery to Adam. However, Peter is a devious scumbag and a spy for Soviet Union. He previously stole my encryption/decryption protocols and he wants to mislead us by changing "Samuel" to "Justin". So, he decrypts "qwerty" back to "Samuel", changes "Samuel" to "Justin", encrypts it with the same rules into "asdfg" and passes along this file to Adam. Adam decrypts File #2 successfully and would assume "Justin" is the Soviet spy.... if he didn't hash "Justin" and called me to confirm whether our hashes matched. Surprise! They do not! Hence we know someone manipulated the data and that someone knows the decryption/encryption protocols! Data integrity saved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.