11package com.funny.data_saver.core
22
3+ import com.funny.data_saver.kmp.Log
4+ import java.io.FileInputStream
35import java.io.FileNotFoundException
4- import java.io.FileReader
5- import java.io.FileWriter
6+ import java.io.FileOutputStream
7+ import java.io.InputStreamReader
8+ import java.io.OutputStreamWriter
69import java.security.MessageDigest
710import java.util.Base64
811import java.util.Properties
@@ -11,40 +14,45 @@ import javax.crypto.spec.IvParameterSpec
1114import javax.crypto.spec.SecretKeySpec
1215
1316/* *
14- * Use [Properties] to save data in a properties file with encryption. The algorithm is AES/CBC/NoPadding .
17+ * Use [Properties] to save data in a properties file with encryption. The algorithm is AES/CBC/PKCS5Padding .
1518 *
1619 * @property filePath String The file path to save the data file. 数据文件的保存路径。
1720 * @param encryptionKey String The key to encrypt the data, can be any string. 用于加密数据的密钥,可以是任意字符串(实际使用的密钥为该字符串的 SHA-256 哈希值,且仅用其前 16 位产生 iv)
21+ * @param fileEncoding String The encoding of the data file, default to UTF-8. 数据文件的编码,默认为 UTF-8。
1822 */
1923
20- open class DataSaverEncryptedProperties (private val filePath : String , private val encryptionKey : String ) : DataSaverInterface() {
24+ open class DataSaverEncryptedProperties (
25+ private val filePath : String ,
26+ private val encryptionKey : String ,
27+ private val fileEncoding : String = " UTF-8"
28+ ) : DataSaverInterface() {
2129 private val properties = Properties ()
2230 private val hashedKey = hashKey(encryptionKey)
2331 private val encryptCipher by lazy { createCipher(Cipher .ENCRYPT_MODE ) }
2432 private val decryptCipher by lazy { createCipher(Cipher .DECRYPT_MODE ) }
2533
2634 init {
2735 try {
28- createFile(filePath)
29- FileReader (filePath).use { reader ->
36+ InputStreamReader (FileInputStream (filePath), fileEncoding).use { reader ->
3037 properties.load(reader)
3138 }
3239 } catch (e: FileNotFoundException ) {
33- // Handle file not found exception
40+ // 处理文件不存在等异常
41+ createFile(filePath)
3442 } catch (e: Exception ) {
35- // Handle other exceptions
36- e.printStackTrace()
43+ Log .e(TAG , " Error loading properties: ${e.message} " , e)
3744 }
3845 }
3946
4047 private fun saveProperties () {
4148 try {
42- FileWriter ( filePath).use { writer ->
49+ OutputStreamWriter ( FileOutputStream ( filePath), fileEncoding ).use { writer ->
4350 properties.store(writer, null )
4451 }
52+ } catch (e: FileNotFoundException ) {
53+ Log .e(TAG , " File not found: $filePath " )
4554 } catch (e: Exception ) {
46- // Handle file write exception
47- e.printStackTrace()
55+ Log .e(TAG , " Error saving properties: ${e.message} " , e)
4856 }
4957 }
5058
@@ -99,4 +107,8 @@ open class DataSaverEncryptedProperties(private val filePath: String, private va
99107 override fun contains (key : String ): Boolean {
100108 return properties.containsKey(key)
101109 }
110+
111+ companion object {
112+ private const val TAG = " DataSaverEncryptedProperties"
113+ }
102114}
0 commit comments