Android EncryptedSharedPreferences

passยท2023๋…„ 7์›” 26์ผ
0

Android

๋ชฉ๋ก ๋ณด๊ธฐ
22/41

๐Ÿ”ฅ Android EncryptedSharedPreferences ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ , ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์ž.


๐ŸŽˆ EncryptedSharedPreferences ๋ž€?

EncryptedSharedPreferences๋Š” Android Jetpack ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ค‘ security ์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.
๊ธฐ์กด์— ์กด์žฌํ•˜๋Š” SharedPreferences๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.
Jetpack security ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Android Keystore ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜์—ฌ ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ์•ฑ ๋‚ด๋ถ€๊ฐ€ ์•„๋‹Œ ์‹œ์Šคํ…œ๋งŒ์ด ์ ‘๊ทผ๊ฐ€๋Šฅํ•œ ๊ณณ์— ์ €์žฅํ•˜๋„๋ก ํ•œ๋‹ค.

EncryptedSharedPreferences๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ SharedPreferences ์™€ ๋น„์Šทํ•˜์ง€๋งŒ, ๊ฐ’์„ ์ €์žฅ ๋ฐ ๊ฒ€์ƒ‰ํ•  ๋•Œ ์•”ํ˜ธํ™” ๋ฐ ๋ณตํ˜ธํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ˆ„์ถœ์„ ๋ฐฉ์ง€ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์•ฑ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ค‘์š”ํ•œ ๋ฐ์ดํ„ฐ(์˜ˆ: API ํ‚ค, ์‚ฌ์šฉ์ž ์ •๋ณด ๋“ฑ)๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋˜๋Š” ํ‚ค ๊ฐ’์€ Android Keystore ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ด€๋ฆฌํ•œ๋‹ค.
EncryptedSharedPreferences๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ๊ธฐ๋ณธ์ ์œผ๋กœ AES256-GCM ์•”ํ˜ธํ™”์™€ ๊ฐ™์€ ์•ˆ์ „ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ SharedPreferences๋ฅผ ๋ณดํ˜ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์ œ์—์„œ AES256 ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ EncryptedSharedPreferences ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์ž.


๊ณต์‹ ๋ฌธ์„œ

https://developer.android.com/topic/security/data?hl=ko
https://developer.android.com/jetpack/androidx/releases/security?hl=ko



๐Ÿ”ฅ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

โœ“ build.gradle

implementation "androidx.security:security-crypto:1.1.0-alpha06"

โœ“ MainActivity.java

// ์ƒ์ˆ˜
private static final String PREFS_NAME = "my_api_key_prefs";
private static final String PREFS_KEY = "api_key";


// MasterKey ์ƒ์„ฑ
MasterKey masterKey = new MasterKey.Builder(context)
		.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
        .build();

// EncryptedSharedPreferences ์ƒ์„ฑ
SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
        this,
		PREFS_NAME,
		masterKey,
		EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
		EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);

// data ์ €์žฅ
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(PREFS_KEY, "์ €์žฅํ•  ๋ฐ์ดํ„ฐ");
editor.apply();

// data ๊ฐ€์ ธ์˜ค๊ธฐ
String data = sharedPreferences.getString(PREFS_KEY, null);



profile
์•ˆ๋“œ๋กœ์ด๋“œ ๊ฐœ๋ฐœ์ž ์ง€๋ง์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€