SharedPreferences / EncryptedSharedPreferences 의 사용법입니다.
1. SharedPreferences
간단하게 아래와 같이 구현해줄 수 있습니다.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sharedPref = this.getPreferences(Context.MODE_PRIVATE) ?: return
with (sharedPref.edit()) {
putString("myString", "keyValue")
apply()
}
val highScore = sharedPref.getString("myString", "defaultValue")
Log.e("a", highScore.toString())
}
}
이 친구를 찾아보면 아래와 같이 평문으로 저장된 것을 볼 수 있습니다.
이 친구를 암호화 해보겠습니다.
2. EncryptedSharedPreferences
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
val sharedPreferences = EncryptedSharedPreferences.create(
"this_is_file_name",
masterKeyAlias,
applicationContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
val editor = sharedPreferences.edit()
editor.putString("key", "value")
editor.apply()
}
}
3. EncryptedFile
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// read 파일 쓰기
// val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
// val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
//
// val fileToRead = "my_sensitive_data.txt"
// val encryptedFile = EncryptedFile.Builder(
// File(applicationContext.filesDir, fileToRead),
// applicationContext,
// mainKeyAlias,
// EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
// ).build()
//
// val inputStream = encryptedFile.openFileInput()
// val byteArrayOutputStream = ByteArrayOutputStream()
// var nextByte: Int = inputStream.read()
// while (nextByte != -1) {
// byteArrayOutputStream.write(nextByte)
// nextByte = inputStream.read()
// }
//
// val plaintext: ByteArray = byteArrayOutputStream.toByteArray()
//
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
// Create a file with this name, or replace an entire existing file
// that has the same name. Note that you cannot append to an existing file,
// and the file name cannot contain path separators.
val fileToWrite = "my_sensitive_data.txt"
val encryptedFile = EncryptedFile.Builder(
File(applicationContext.filesDir, fileToWrite),
applicationContext,
mainKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
).build()
val fileContent = "MY SUPER-SECRET INFORMATION".toByteArray(StandardCharsets.UTF_8)
encryptedFile.openFileOutput().apply {
write(fileContent)
flush()
close()
}
}
}
- 참조
https://developer.android.com/training/data-storage/shared-preferences?hl=ko
https://developer.android.com/topic/security/data?hl=ko
https://www.youtube.com/watch?v=2uResVLUCNI
https://thdev.tech/android/2019/12/21/Android-Security-Library/
'Android(Kotlin)' 카테고리의 다른 글
Android KeyStore (0) | 2022.07.29 |
---|---|
Android 디컴파일 (0) | 2022.07.29 |
Android Lottie (0) | 2022.05.16 |
android 12 splash (0) | 2022.05.15 |
SaveFile Android kotlin (0) | 2022.05.02 |