I want do detect illegal modification of a string by using HMAC-SHA256 or AES/CBC/PKCS5Padding, with low calculation cost. Which is the best answer?

I plan to save data in a DB with one of the following format

| METHOD                   | original STRING | VALIDATION BY  |  
| H:= HMAC-SHA256          | org-string      | H(org-string)  |  
| A:= AES/CBC/PKCS5Padding | org-string      | A(org-string)  |  

IF A(org-string) cannot be decrypted, the original STRING is modified.

FYI: This question well explained difference between SHA and AES.

  • 1
    $\begingroup$ Note that you probably rather want AES-CMAC than AES-CBC because only the former is meant to detect message manipulation. $\endgroup$ – SEJPM Dec 15 '16 at 14:25

With AES/CBC mode, an attacker can modify the string, and have the result decrypt without errors; the only thing that can cause a decryption failure is an illegal padding pattern after decryption, and:

  • If the plaintext string is at least 17 bytes long, then the attacker can modify the string without affecting the padding bytes (and thus leave a valid padding pattern)

  • Alternatively, changing the entire ciphertext randomly leaves a 1/256 chance that, after decryption, the last byte of the ciphertext is 01 (which is a valid padding pattern)

Hence, to answer your question, HMAC-SHA256 is the best answer, as AES doesn't do the job at all. If your crypto toolkit provides it, you might want to consider AES-CMAC instead (which is another right answer for this problem)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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