0

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.

5
  • You are storing the encrypted data with the key "pass_check" and decrypting the data stored with they key "validator". Is this intentional? (edit: the SharedPreferences key, not a crypto key. You might not be decrypting the right piece of data) Commented Feb 17, 2014 at 2:28
  • Ah, sorry. I was making the code fuzzy and forgot to change that part. My project is private so I can't share direct code. I'll fix that. Commented Feb 17, 2014 at 2:29
  • Have you checked that validation.equals(putdata);? That would determine whether SharedPreferences has anything to do with the problem. Commented Feb 17, 2014 at 2:57
  • Yes. They are the same. In my print statements (that I removed) I tested to make sure they were. I am going to try to clean the project and restart eclipse to see if something is funky with the IDE because I do not see where there can be problems.. Commented Feb 17, 2014 at 3:07
  • Wow. I literally changed nothing but restarted the IDE, and it seems to work... Commented Feb 17, 2014 at 3:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.