Android Open Source - trivial-password Trivial Password J S O N Serializer






From Project

Back to project page trivial-password.

License

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.

Java Source Code

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();
    }
  }
}




Java Source Code List

org.hbabcock.trivialpassword.AccountEditorFragment.java
org.hbabcock.trivialpassword.AccountFragment.java
org.hbabcock.trivialpassword.AccountListActivity.java
org.hbabcock.trivialpassword.AccountListFragment.java
org.hbabcock.trivialpassword.AccountManager.java
org.hbabcock.trivialpassword.AccountPagerActivity.java
org.hbabcock.trivialpassword.Account.java
org.hbabcock.trivialpassword.PasswordActivity.java
org.hbabcock.trivialpassword.PasswordChangeFragment.java
org.hbabcock.trivialpassword.PasswordFragment.java
org.hbabcock.trivialpassword.PasswordManager.java
org.hbabcock.trivialpassword.SingleFragmentActivity.java
org.hbabcock.trivialpassword.TrivialPasswordJSONSerializer.java