SOLVED
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...)
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).
The issue is that I make an encrypted piece of data (which is returned to me as a byte[], 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.
I'm using the Crypto Example given in the accepted answer of the following question:
My code is as follows:
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();
}
}
The Cryptography class I am using works, as it encrypts and decrypts if I don't put and get the string from SharedPreferences. I have also tried storing the data as UTF-8 rather than Base64, but that same issue still appears.
The 'strings' that I put and read back are the same (I tested in code), but when I compare the byte arrays (using Arrays.compare(), it returns that they are different. So I am not sure what is going on...
Any help is appreciated.