gokan
gokan

Reputation: 1078

Java - Difference between javax.crypto.Mac and javax.crypto.Cipher?

I would like to understand the difference between javax.crypto.Mac and javax.crypto.Cipher. Those two classes looks very similar (they have similar methods but those two classes do not inherits from each another).

  1. What's the fundamental difference between those two classes ?
  2. When should I use (or not use) Mac ?
  3. When should I use (or not use) Cipher ?

Upvotes: 0

Views: 973

Answers (1)

Tom Leek
Tom Leek

Reputation: 856

A Message Authentication Code is for integrity. It computes, on some input message, a kind of "keyed checksum" that depends on the message and on the key. With knowledge of the key, the MAC can be verified to match a given message. Alterations are thus reliably detected.

A Symmetric encryption algorithm is for confidentiality. It transforms a message into an unreadable sequence of bits; the encryption is reversible provided that the decryption key is known.

MAC do not ensure confidentiality; the message is kept as is, plainly readable. Encryption does not ensure integrity; alterations may go undetected. In properly applied cryptography, you need both. (But mind that this "properly" term is big.)

Upvotes: 4

Related Questions