android - เพิ่มความปลอดภัยให้ Shared Preferences ด้วย Secure Preferences

Securing Shared Preferences with Third Party Libraries




Secure Preferences
คือ Third party crypto library ที่มาช่วยเพิ่มความปลอดภัยของ Shared Preferences
บน Android application ที่ใช้เก็บข้อมูลเป็นไฟล์ xml อยู่บนมือถือที่ติดตั้งแอพพลิเคชั่น

Shared Preference โดยปกติจะไม่เข้ารหัส (PlainText)
จึงต้องมีคำว่า crypto เข้ามาเกี่ยวข้อง ,ซึ่ง library นี้ก็ไม่ได้ทำให้ปลอดภัยขึ้น 100%
แต่ก็การันตรีว่า app จะปลอดภัยในกรณีที่ Devices Rooted
หรือทำการ root เครื่องมาเรียบร้อยแล้ว เราสามารถทำให้แอพปลอดภัยมากขึ้น
(แต่ช้าลง) เมื่อเปรียบเทียบกับ Shared Preference

Secure Preferences คือ encryption ที่ใช้ AES algorithm มาเข้ารหัส Value
ที่เก็บไว้ใน Shared Preference  อีกทีที่ถูกเก็บไว้ใน XML File ที่สำคัญคือมันฟรี และ Open Source
รวมถึงคอนเซ็บการใช้งานก็จะค่อยข้างเหมือนกับ Shared Preference



Secure preferences
https://github.com/scottyab/secure-preferences


Example
SharedPreferences prefs = new SecurePreferences(MainActivity.this);


Generated at runtime not stored in the backing pref file. (recommended)
SharedPreferences prefs = new SecurePreferences(context, "secret_key", "user_login");


Put Key &Value into secure preferences 
SharedPreferences prefs = new SecurePreferences(MainActivity.this);
Editor editor = spf.edit();
editor.putString("key_name", input);
editor.commit();


Get Value from key
String out = spf.getString("key_name", null);

XML with SecurePreferences
    <map>
    <string name="TuwbBU0IrAyL9znGBJ87uEi7pW0FwYwX8SZiiKnD2VZ7">
        pD2UhS2K2MNjWm8KzpFrag==:MWm7NgaEhvaxAvA9wASUl0HUHCVBWkn3c2T1WoSAE/g=rroijgeWEGRDFSS/hg
    </string>
    <string name="8lqCQqn73Uo84Rj">k73tlfVNYsPshll19ztma7U">
        pD2UhS2K2MNjWm8KzpFrag==:MWm7NgaEhvaxAvA9wASUl0HUHCVBWkn3c2T1WoSAE/g=:jWm8KzUl0HUHCVBWkn3c2T1WoSAE/g=
    </string>
</map>