Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most Java <code>Key</code> instances are represented as a string of bytes resulting from their <a href="http://java.sun.com/javase/6/docs/api/java/security/Key.html#getEncoded()" rel="noreferrer"><code>getEncoded()</code></a> method. This is what needs to be stored in order to reconstruct the key later.</p> <p>However, to store a key safely in electronic form, it should be encrypted. Of course, encrypting the key would require another key (or password)&hellip; and so you have an infinite regress. A Java <code>KeyStore</code> can be used to store <code>SecretKey</code> objects in this manner, and that is useful when you have many secret keys that are all protected by a single "master" password. But for protecting a single key, it doesn't make a lot of sense.</p> <p>One alternative is to present the key to the user in a form that can be stored in some safe manner (in many applications, that might be on a slip of paper in their wallet). This could be as simple as displaying the bytes of the key encoded in hexadecimal, Base-64, or other text encoding, and asking the user to write it down.</p> <p>Another approach is to allow the user to choose a memorable password, and generate a key with that, using an algorithm like PBKDF2. The salt (and maybe the iteration count) used for key derivation would need to be recorded somewhere though. Another drawback is that people tend to choose from a relatively limited number of passwords out of the total available. So keys derived from passwords may be easier guess than than the key size suggests.</p> <hr> <p>Here is an illustration of the basic technique for persisting and reconstituting a secret key.</p> <pre><code>byte[] encoded = aesKey.getEncoded(); /* Now store "encoded" somewhere. For example, display the key and ask the user to write it down. */ String output = Base64.getEncoder().withoutPadding().encodeToString(encoded); System.out.println("Keep it secret, keep it safe! " + output); ... /* At some point, you need to reconstitute the key. Let's say the user enters it as a base-64 number that you convert to bytes. */ String input = ... ; byte[] encoded = Base64.getDecoder().decode(input); SecretKey aesKey = new SecretKeySpec(encoded, "AES"); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload