4

I am trying to build an AES file encrypt/decrypt. I have used some neat tutorials and guides and I am wondering what the difference is between Key and SecretKey. For context I used the the following posts, with the first one using SecretKey (Duncan's reply) and the second one using Key (Shankar's reply) with some keyValue byte array.

My guess is that harcoding some a value with using just Key (shankar's answer) isn't as safe as randomly doing this with SecretKey (Duncan's answer).

Question: What is the difference between Key and Secret Key, and what is the reasoning for having keyValue within the second post (Key)? Can Key not be randomly generated Like Duncan did with SecretKey?

Community
  • 1
  • 1
Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    Beware of trying to learn cryptography by studying source code. Not all source code is cryptographically secure, and there are a lot of examples of bad code, even on SO. Some theoretical knowledge is required. – Maarten Bodewes Oct 28 '14 at 19:03
  • 1
    I understand, I am in a Network security class right now for the theoretical aspect, they just never went over the differences of Java's `SecretKey` and `Key`. – Austin Oct 28 '14 at 22:13

1 Answers1

5

An AES key should consist of bytes that are indistinguishable from random to an attacker. Some keys, such as DES and triple DES keys, are not fully random and therefore need to be generated by a SecretKeyFactory or KeyGenerator (although, for DES, many implementations simply ignore the parity bits and allow random values as well). Random symmetric keys can however also be created by directly using SecretKeySpec and using it as a SecretKey. This is a useful shortcut, but beware that it may not be compatible with key generation or storage in hardware (HSM, smart card).

If you create keys randomly on the spot or if you use static keys depends on your key management scheme. Key management is one of the most important hardest things to do correctly for any encryption scheme. "Hard coding" the key in the application sources is of course less safe than generating or storing the key securely. It is usually only performed if there is no other way, or for testing / demonstration purposes. How you handle key management is completely up to the use case; there is no "best" without knowing how and for what the keys are utilized. For instance, Duncan's random key generation is of little use if you cannot retrieve the key value later on.

As for Key and SecretKey; Key is the base interface of Secretkey, PublicKey and PrivateKey. SecretKey generally consists of random bytes as indicated above. PublicKey and PrivateKey are always part of an asymmetric key pair. Generally these kind of keys are based on number theory and they consist of multiple components. For instance an RSAPublicKey consists of both the modulus and the public exponent (as BigInteger values).

As for which you need: choose the one that makes most sense at a particular location, using the highest level interface that still fits your need. For instance you'd only need RSAPublicKey to get RSA specific properties such as the modulus. Otherwise you might as well use PublicKey variables that also accepts ECPublicKey. That way you can make your code (relatively) algorithm agnostic.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I see, so having the hard coded byte array in Shankar's example is okay then? – Austin Oct 28 '14 at 22:12
  • Depends on what you are trying to do. Mostly not though, check the "hard coding" part of the answer. And note that there are *books* about key management. – Maarten Bodewes Oct 28 '14 at 22:31