【问题标题】:Store and read KeyPair with Android SharedPreferences使用 Android SharedPreferences 存储和读取 KeyPair
【发布时间】:2013-01-10 19:15:08
【问题描述】:

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

建议?

【问题讨论】:

    标签: android serialization preferences


    【解决方案1】:

    恐怕没有办法在 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;
    }
    

    【讨论】:

      【解决方案2】:

      其实我是这样解决的:

      我首先使用 Base64 创建一个字符串,我存储它,然后从 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();
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-19
        • 1970-01-01
        • 2013-05-12
        • 2021-11-15
        • 1970-01-01
        • 2011-04-07
        • 2023-03-09
        相关资源
        最近更新 更多