Encrypted save and decrypted load of an ArrayList of serializable objects

前端 未结 3 1534
情书的邮戳
情书的邮戳 2021-02-04 17:37

I save and load in sd card a file that contains an ArrayList of serializable object with these two methods

save method

public static void sa         


        
3条回答
  •  -上瘾入骨i
    2021-02-04 18:00

    I suggest taking a look at Conceal, recently released by facebook: http://facebook.github.io/conceal/

    This should be a trivial modification to wrap a Conceal output stream with an ObjectOutputStream used in your current code:

    public static void saveUserList(ArrayList userList) {
        if (storageAvailable()) {
            try {
                createFolder();
                Crypto crypto = new Crypto(
                    new SharedPrefsBackedKeyChain(context),
                    new SystemNativeCryptoLibrary());
    
                FileOutputStream userList = new FileOutputStream(
                        baseDir + File.separator + baseAppDir + File.separator
                                + fileName);
    
                OutputStream cryptedStream = crypto.getCipherOutputStream(
                    userList, new Entity("UserList");
    
    
                ObjectOutputStream oos = new ObjectOutputStream(
                        cryptedStream);
                oos.writeObject(userList);
    
                oos.close();
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    
    }
    

    I'll leave the restore as an exercise for the reader. ;)

提交回复
热议问题