使用Android SharedPreferences存储和读取KeyPair

时间:2013-01-10 19:15:09

标签: android serialization preferences

我正在寻找一种用于java.security.KeyPair的序列化来存储和读取共享首选项。 存储.toString()现在非常有罪,因为没有KeyPair的构造函数。

建议?

2 个答案:

答案 0 :(得分:0)

我担心无法在SharedPreferences中存储Serializable对象。我建议将其保存为私人文件,有关详细信息,请参阅Android Storage OptionsFileOutputStreamObjectOutputStream

public static void write(Context context, Object obj, String filename) {
    ObjectOutputStream oos = null;

    try {
        FileOutputStream file = context.openFileOutput(filename, Activity.MODE_PRIVATE);
        oos = new ObjectOutputStream(file);
        oos.writeObject(obj);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public static Object read(Context context, String filename) {
    ObjectInputStream ois = null;
    Object obj = null;

    try {
        FileInputStream file = context.getApplicationContext().openFileInput(filename);
        ois = new ObjectInputStream(file);
        obj = ois.readObject();
    } catch (FileNotFoundException e) {
       // Just let it return null.
       e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return obj;
}

答案 1 :(得分:0)

我实际上是这样解决的:

我首先使用Base64创建一个String,我存储然后从Shared Proferences重新创建:

    SharedPreferences prefs = this.getSharedPreferences(
                PATH, Context.MODE_PRIVATE);
    String key = prefs.getString(KEYPATH, "");

    if (key.equals("")) {
        // generate KeyPair
        KeyPair kp = Encrypter.generateKeyPair();
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        ObjectOutputStream o;
        try {
            o = new ObjectOutputStream(b);
            o.writeObject(kp);
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] res = b.toByteArray();
        String encodedKey = Base64.encodeToString(res, Base64.DEFAULT);

        prefs.edit().putString(KEYPATH, encodedKey).commit();

    } else {
        // read the KeyPair from internal storage
        byte[] res = Base64.decode(key, Base64.DEFAULT);
        ByteArrayInputStream bi = new ByteArrayInputStream(res);
        ObjectInputStream oi;
        try {
            oi = new ObjectInputStream(bi);
            Object obj = oi.readObject();
            Encrypter.setMyKeyPair((KeyPair) obj);
            Log.w(TAG, ((KeyPair) obj).toString());
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }