Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can convert the <code>SecretKey</code> to a byte array (<code>byte[]</code>), then Base64 encode that to a <code>String</code>. To convert back to a <code>SecretKey</code>, Base64 decode the String and use it in a <code>SecretKeySpec</code> to rebuild your original <code>SecretKey</code>.</p> <h2>For Java 8</h2> <p><strong>SecretKey to String:</strong></p> <pre><code>// create new key SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey(); // get base64 encoded version of the key String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded()); </code></pre> <p><strong>String to SecretKey:</strong></p> <pre><code>// decode the base64 encoded string byte[] decodedKey = Base64.getDecoder().decode(encodedKey); // rebuild key using SecretKeySpec SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES"); </code></pre> <hr> <h2>For Java 7 and before (including Android):</h2> <p><strong>NOTE I:</strong> you can skip the Base64 encoding/decoding part and just store the <code>byte[]</code> in SQLite. That said, performing Base64 encoding/decoding is not an expensive operation and you can store strings in almost any DB without issues.</p> <p><strong>NOTE II:</strong> Earlier Java versions do not include a Base64 in one of the <code>java.lang</code> or <code>java.util</code> packages. It is however possible to use codecs from <a href="http://commons.apache.org/proper/commons-codec/" rel="noreferrer">Apache Commons Codec</a>, <a href="https://www.bouncycastle.org/java.html" rel="noreferrer">Bouncy Castle</a> or <a href="https://github.com/google/guava" rel="noreferrer">Guava</a>.</p> <p><strong>SecretKey to String:</strong></p> <pre><code>// CREATE NEW KEY // GET ENCODED VERSION OF KEY (THIS CAN BE STORED IN A DB) SecretKey secretKey; String stringKey; try {secretKey = KeyGenerator.getInstance("AES").generateKey();} catch (NoSuchAlgorithmException e) {/* LOG YOUR EXCEPTION */} if (secretKey != null) {stringKey = Base64.encodeToString(secretKey.getEncoded(), Base64.DEFAULT)} </code></pre> <p><strong>String to SecretKey:</strong></p> <pre><code>// DECODE YOUR BASE64 STRING // REBUILD KEY USING SecretKeySpec byte[] encodedKey = Base64.decode(stringKey, Base64.DEFAULT); SecretKey originalKey = new SecretKeySpec(encodedKey, 0, encodedKey.length, "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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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