Back to project page trivial-password.
The source code is released under:
MIT License
If you think the Android project trivial-password listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.hbabcock.trivialpassword; /*from w w w . jav a 2 s.c o m*/ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONTokener; import android.content.Context; import android.util.Log; public class TrivialPasswordJSONSerializer { private static final String TAG = "TrivialPasswordJSONSerializer"; private Context mContext; private String mFilename; public TrivialPasswordJSONSerializer(Context c, String f){ mContext = c; mFilename = f; } public String decodeFile(String key, byte[] fileData){ String decrypted = ""; try { SecretKeySpec skeySpec = new SecretKeySpec(resizeKey(key), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); decrypted = new String(cipher.doFinal(fileData)); } catch (Exception e){ Log.d(TAG, "decodeFile " + e); } return decrypted; } public byte[] encodeFile(String key, String fileData){ byte[] encrypted = null; try{ SecretKeySpec skeySpec = new SecretKeySpec(resizeKey(key), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encrypted = cipher.doFinal(fileData.getBytes("UTF-8")); } catch (Exception e){ Log.d(TAG,"encodeFile " + e); } return encrypted; } public ArrayList<Account> loadAccounts() throws IOException, JSONException { Log.i(TAG,"loadAccounts " + PasswordManager.get(mContext).havePassword()); ArrayList<Account> accounts = new ArrayList<Account>(); BufferedInputStream reader = null; try{ // Read binary data. reader = new BufferedInputStream(mContext.openFileInput(mFilename)); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int read = 0; while ((read = reader.read(buffer)) != -1) { out.write(buffer, 0, read); } // Decode. String jsonString = decodeFile(PasswordManager.get(mContext).getPassword(), out.toByteArray()); // Parse. JSONArray array = (JSONArray) new JSONTokener(jsonString).nextValue(); for (int i = 0; i < array.length(); i++){ accounts.add(new Account(array.getJSONObject(i))); } } catch (FileNotFoundException e){ } finally { if (reader != null) reader.close(); } return accounts; } private byte[] resizeKey(String key){ byte[] keyBytes = key.getBytes(); byte[] key128 = new byte[16]; int j = 0; for (int i = 0; i < 16; i++){ key128[i] = keyBytes[j]; j++; if (j == keyBytes.length){ j = 0; } } return key128; } public void saveAccounts(ArrayList<Account> accounts) throws JSONException, IOException { Log.i(TAG,"saveAccounts " + PasswordManager.get(mContext).havePassword()); if (!PasswordManager.get(mContext).havePassword()){ Log.i(TAG, "No password set, accounts not saved."); return; } JSONArray array = new JSONArray(); for (Account a : accounts){ array.put(a.toJSON()); } BufferedOutputStream out = null; try { out = new BufferedOutputStream(mContext.openFileOutput(mFilename, Context.MODE_PRIVATE)); out.write(encodeFile(PasswordManager.get(mContext).getPassword(), array.toString())); Log.i(TAG,"Accounts saved."); } finally { if (out != null) out.close(); } } }