Android 一个对sharedpreferences 数据进行加密的开源库
1、项目地址
https://github.com/iamMehedi/Secured-Preference-Store
2、使用方法
2.1、存数据
//存数据 SecuredPreferenceStore prefStore = SecuredPreferenceStore.getSharedInstance(getApplicationContext()); prefStore.edit().putString( "t1" , "tttttt1111111").apply(); ;
2.2、 取数据
String data1 = prefStore.getString( "t1" , "default") ;
3、xml文件内容
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="11CD15241CB4D6F953FA27C76F72C10920C5FADF14FF2824104FA5D67D25B43C">ZMnr87IlDKg81hKw2SQ6Lw==]dhP/ymX7CMSaCkP6jQvNig==</string> <string name="C8D076EFD8542A5F02F86B176F667B42BFB9B1472E974E6AF31EB27CEA5689D4">JQ6Y4TQ/Y3iYw7KtatkqAg==]P+gpavV0MXiy1Qg0UHlBMg==</string> <string name="F2AA713F406544A3E9ABA20A32364FA29613F01C867B3D922A85DF4FA54FA13D">jMH1Wjnk0vehHOogT27HRA==]e8UHX1ihYjtP6Cv8dWdHLBptLwowt6IojKYa+1jkeH4=</string> <string name="C06C6027E72B7CE947885F6ADE3A73E338881197DBE02D8B7B7248F629BE26DA">EAGwO8u2ZPdxwdpAwPlu6A==]797VOGtpzDBO1ZU3m+Sb1A==</string> <string name="33188AFFEC74B412765C3C86859DE4620B5427C774D92F9026D95A7A8AAE1F96">s0b5h8XNnerci5AtallCQziSbqpm+ndjIsAQQadSxM+xzw7865sE3P+hbxGmMAQQj0kK35/C//eA MXuQ0N/F+oapBiDIKdRt2GJB3wJ+eshuh6TcEv+J8NQhqn1eO2fdao353XthHpRomIeGEWLvB4Yd 7G5YYIajLWOGWzQVsMTg1eqdcJ7+BAMXdOdWhjTTo91NvhvykgLMC03FsePOZ/X8ej4vByH1i0en hJCiChk90AQ9FhSkaF/Oum9KoWqg7NU0PGurK755VZflXfyn1vZ8hhTulW7BrA2o9HvT9tbju+bk 4yJ5lMxgS6o4b+0tqo+H4TPOUiZPgehTwsrzJg== </string> <string name="9DCB904DFDA83286B41A329A7D8648B0BFF73C63E844C88800B2AA5119204845">XPuUd1t97pnwsOzzHY3OCA==]xqXJrEfcgDhYo2K4TTAvY9IQwP/tGctd4Fa1JT/1sB8=</string> </map>
可以看到xml文件里面的内容都已经变成了混乱的字符,从而实现加密。
4、SecurePreferenceStore源码解析
可以看到 SecurePreferenceStore 这个类继承了系统的SharePreferences.
然后重写了 里面的 get方法,在重写的方法中,用EncryptionManager的decrypt()方法去对 value 值解密。
用Editor类继承 SharedPreferences.Editor ,然后重写里面的put 方法,对传进来的value值进行加密。