Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, I've found a solution!<br> For starters I've changed a bit of my code. I've created a myPBEkey class with two methods, encrypt and decrypt, both return a Cipher object with the respective "opmode": <br><br> Then I've changed my saveProfile and loadProfile methos code to:</p> <pre><code>public void saveProfile(Profile newProfile) { try { SharedPreferences.Editor editor = prefs.edit(); String profileJSONfied = new Gson().toJson(newProfile); byte[] encryptedProfile = pbeKey.encrypt().doFinal(profileJSONfied.getBytes(HTTP.UTF_8)); byte[] encryptedProfileBase64 = Base64.encode(encryptedProfile, Base64.DEFAULT); editor.putString(PROFILE, new String(encryptedProfileBase64, HTTP.UTF_8)); editor.commit(); profile = newProfile; } catch (Exception e) { Log.i(C.TAG, e.getMessage()); } } </code></pre> <p><br></p> <pre><code>public Profile loadProfile() { if (profile == null) { try { byte[] decodedProfileBase64 = Base64.decode(prefs.getString(PROFILE, null), Base64.DEFAULT); byte[] plainTextProfileBytes = pbeKey.decrypt().doFinal(decodedProfileBase64); profile = new Gson().fromJson(new String(plainTextProfileBytes, HTTP.UTF_8), PROFILE_TYPE); } catch (Exception e) { Log.i(C.TAG, e.getMessage()); } return profile; </code></pre> <p>I think what solved the problem was separating the encrypt/decrypt from <code>Base64</code> encode/decode, so first we encrypt and then encode the encrypted <code>byte[]</code> and finally store it. The same goes when decrypting, first we decode the encrypted base64 profile and then decrypt the decoded <code>byte[]</code>. Voilà!<br><br> Thanks for your time, hope it helps you.</p>
    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.
    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