title: Java Strings storing byte arrays
body: <p>I want to store a byte array wrapped in a String object.  Here's the scenario</p>
  
  <ol>
  <li>The user enters a password.</li>
  <li>The bytes of that password are obtained using the getBytes() String method.</li>
  <li>They bytes are encrypted using java's crypo package.</li>
  <li>Those bytes are then converted into a String using the constructor new String(bytes[])</li>
  <li>That String is stored, or otherwise passed around (NOT changed)</li>
  <li>The bytes of that String are obtained and they are different then the encoded bytes.</li>
  </ol>
  
  <p>Here's a snippet of code that describes what I'm talking about.</p>
  
  <pre><code>String s = "test123";
  byte[] a = s.getBytes();
  byte[] b = env.encrypt(a);
  String t = new String(b);
  byte[] c = t.getBytes();
  byte[] d = env.decrypt(c);
  </code></pre>
  
  <p>Where env.encrypt() and env.decrypt() do the encryption and decryption.  The problem I'm having is that the b array is of length 8 and the c array is of length 16.  I would think that they would be equal.  What's going on here?  I tried to modify the code as below</p>
  
  <pre><code>String s = "test123";
  Charset charset = Charset.getDefaultCharset();
  byte[] a = s.getBytes(charset);
  byte[] b = env.encrypt(a);
  String t = new String(b, charset);
  byte[] c = t.getBytes(charset);
  byte[] d = env.decrypt(c);
  </code></pre>
  
  <p>but that didn't help.</p>
  
  <p>Any ideas?</p>