title: Byte Array to String and back again are not the same even with UTF-8 encoding (also Base64)
body: <p><strong>SOLVED</strong></p>
  
  <p>Apparently the code below works, but Eclipse had some sort of issue and I restarted it to fix it. (After all these hours of debugging...)</p>
  
  <p>I am writing a small encryption mechanism for stuff in my app. It is not connected to a service that provides tokens, so I have to store some sensative data locally, which I want to be encrypted. I figured a good place would be in SharedPreferences, where I can store some data that is encrypted, anad the user must provide a 'key' that is used for unlocking the data (it is part of the algorithm, so that part is never truley stored).</p>
  
  <p>The issue is that I make an encrypted piece of data (which is returned to me as a <code>byte[]</code>, which I convert to Base64 (also tried UTF-8), and store in SharedPreferences. For testing purposes right now I am immediately reading back the string out of shared preferences and attempting to decrypt it with the same 'key' used to encrypt, but it's throwing some exceptions, and the byte arrays for storing and the one retreived (and converted to bytes) are not the same.</p>
  
  <p>I'm using the Crypto Example given in the accepted answer of the following question:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/15952618/java-string-encrypt">Java string encrypt</a></li>
  </ul>
  
  <p>My code is as follows:</p>
  
  <pre><code>SharedPreferences crypto = getActivity().getSharedPreferences("cryptodb",
              Context.MODE_PRIVATE);
      String uuid = createLocalUUID(); //used to prevent moving data to a different device (security risk)
      if (uuid != null) {
          try {
              SecretKey secret = Cryptography.generateKey(passPhrase,
                      uuid.getBytes("UTF-8")); //This key generator is the same as the one used for decryption below
              byte[] encrypted = Cryptography.encryptMsg(uuid, secret);
              SharedPreferences.Editor editor = crypto.edit();
              String putdata = Base64.encodeBytes(encrypted);
              editor.putString("pass_check", putdata); //decrypt this back to the "stored" UUID to show that this is the correct passphrase
              // Commit the edits!
              editor.commit();
  
              //Test decrypting the validator object
              String validation = crypto.getString("pass_check", "FAILURE"); //get string. Must provide fallback string
              String result = Cryptography.decryptMsg(Base64.decode(validation), secret); //fails         
          } catch (Exception e) {
              // too many exceptions to catch.
              e.printStackTrace();
          }
     }
  </code></pre>
  
  <p>The Cryptography class I am using works, as it encrypts and decrypts <code>if I don't put and get the string from SharedPreferences</code>. I have also tried storing the data as UTF-8 rather than Base64, but that same issue still appears.</p>
  
  <p>The 'strings' that I put and read back are the same (I tested in code), but when I compare the byte arrays (using <code>Arrays.compare()</code>, it returns that they are different. So I am not sure what is going on...</p>
  
  <p>Any help is appreciated.</p>