pull/1296/head
gedoor 3 years ago
parent 74fe52fa0c
commit db068853f9
  1. 9
      app/src/main/java/io/legado/app/data/entities/BaseSource.kt
  2. 32
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 38
      app/src/main/java/io/legado/app/utils/EncoderUtils.kt

@ -82,11 +82,16 @@ interface BaseSource : JsExtensions {
* 用户信息采用aes加密存储 * 用户信息采用aes加密存储
*/ */
fun getLoginInfo(): String? { fun getLoginInfo(): String? {
try {
val cache = CacheManager.get("userInfo_${getStoreUrl()}") ?: return null val cache = CacheManager.get("userInfo_${getStoreUrl()}") ?: return null
val byteArrayB = Base64.decode(cache, Base64.DEFAULT) val byteArrayB = Base64.decode(cache, Base64.DEFAULT)
val byteArrayA = EncoderUtils.decryptAES(byteArrayB, AppConst.androidId.toByteArray()) val byteArrayA = EncoderUtils.decryptAES(byteArrayB, AppConst.androidId.toByteArray())
?: return null ?: return null
return String(byteArrayA) return String(byteArrayA)
} catch (e: Exception) {
e.printStackTrace()
return null
}
} }
fun getLoginInfoMap(): Map<String, String>? { fun getLoginInfoMap(): Map<String, String>? {
@ -97,6 +102,7 @@ interface BaseSource : JsExtensions {
* 保存用户信息,aes加密 * 保存用户信息,aes加密
*/ */
fun putLoginInfo(info: String) { fun putLoginInfo(info: String) {
try {
val data = Base64.encodeToString( val data = Base64.encodeToString(
EncoderUtils.decryptAES( EncoderUtils.decryptAES(
info.toByteArray(), info.toByteArray(),
@ -105,6 +111,9 @@ interface BaseSource : JsExtensions {
Base64.DEFAULT Base64.DEFAULT
) )
CacheManager.put("userInfo_${getStoreUrl()}", data) CacheManager.put("userInfo_${getStoreUrl()}", data)
} catch (e: Exception) {
e.printStackTrace()
}
} }
fun removeLoginInfo() { fun removeLoginInfo() {

@ -455,12 +455,18 @@ interface JsExtensions {
fun aesDecodeToByteArray( fun aesDecodeToByteArray(
str: String, key: String, transformation: String, iv: String str: String, key: String, transformation: String, iv: String
): ByteArray? { ): ByteArray? {
return EncoderUtils.decryptAES( return try {
EncoderUtils.decryptAES(
data = str.encodeToByteArray(), data = str.encodeToByteArray(),
key = key.encodeToByteArray(), key = key.encodeToByteArray(),
transformation, transformation,
iv.encodeToByteArray() iv.encodeToByteArray()
) )
} catch (e: java.lang.Exception) {
e.printStackTrace()
log(e.localizedMessage ?: "aesDecodeToByteArrayERROR")
null
}
} }
/** /**
@ -488,12 +494,18 @@ interface JsExtensions {
fun aesBase64DecodeToByteArray( fun aesBase64DecodeToByteArray(
str: String, key: String, transformation: String, iv: String str: String, key: String, transformation: String, iv: String
): ByteArray? { ): ByteArray? {
return EncoderUtils.decryptBase64AES( return try {
EncoderUtils.decryptBase64AES(
str.encodeToByteArray(), str.encodeToByteArray(),
key.encodeToByteArray(), key.encodeToByteArray(),
transformation, transformation,
iv.encodeToByteArray() iv.encodeToByteArray()
) )
} catch (e: Exception) {
e.printStackTrace()
log(e.localizedMessage ?: "aesDecodeToByteArrayERROR")
null
}
} }
/** /**
@ -520,12 +532,18 @@ interface JsExtensions {
fun aesEncodeToByteArray( fun aesEncodeToByteArray(
data: String, key: String, transformation: String, iv: String data: String, key: String, transformation: String, iv: String
): ByteArray? { ): ByteArray? {
return EncoderUtils.encryptAES( return try {
EncoderUtils.encryptAES(
data.encodeToByteArray(), data.encodeToByteArray(),
key = key.encodeToByteArray(), key = key.encodeToByteArray(),
transformation, transformation,
iv.encodeToByteArray() iv.encodeToByteArray()
) )
} catch (e: Exception) {
e.printStackTrace()
log(e.localizedMessage ?: "aesEncodeToByteArrayERROR")
null
}
} }
/** /**
@ -551,12 +569,18 @@ interface JsExtensions {
fun aesEncodeToBase64ByteArray( fun aesEncodeToBase64ByteArray(
data: String, key: String, transformation: String, iv: String data: String, key: String, transformation: String, iv: String
): ByteArray? { ): ByteArray? {
return EncoderUtils.encryptAES2Base64( return try {
EncoderUtils.encryptAES2Base64(
data.encodeToByteArray(), data.encodeToByteArray(),
key.encodeToByteArray(), key.encodeToByteArray(),
transformation, transformation,
iv.encodeToByteArray() iv.encodeToByteArray()
) )
} catch (e: Exception) {
e.printStackTrace()
log(e.localizedMessage ?: "aesEncodeToBase64ByteArrayERROR")
null
}
} }
/** /**

@ -38,6 +38,7 @@ object EncoderUtils {
fun base64Encode(str: String, flags: Int = Base64.NO_WRAP): String? { fun base64Encode(str: String, flags: Int = Base64.NO_WRAP): String? {
return Base64.encodeToString(str.toByteArray(), flags) return Base64.encodeToString(str.toByteArray(), flags)
} }
//////////AES Start //////////AES Start
/** /**
@ -45,16 +46,18 @@ object EncoderUtils {
* *
* @param data The data. * @param data The data.
* @param key The key. * @param key The key.
* @param transformation The name of the transformation, e.g., *DES/CBC/PKCS5Padding*. * @param transformation The name of the transformation,
* 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
* @param iv The buffer with the IV. The contents of the * @param iv The buffer with the IV. The contents of the
* buffer are copied to protect against subsequent modification. * buffer are copied to protect against subsequent modification.
* @return the Base64-encode bytes of AES encryption * @return the Base64-encode bytes of AES encryption
*/ */
@Throws(Exception::class)
fun encryptAES2Base64( fun encryptAES2Base64(
data: ByteArray?, data: ByteArray?,
key: ByteArray?, key: ByteArray?,
transformation: String?, transformation: String? = "DES/ECB/PKCS5Padding",
iv: ByteArray? iv: ByteArray? = null
): ByteArray? { ): ByteArray? {
return Base64.encode(encryptAES(data, key, transformation, iv), Base64.NO_WRAP) return Base64.encode(encryptAES(data, key, transformation, iv), Base64.NO_WRAP)
} }
@ -64,16 +67,18 @@ object EncoderUtils {
* *
* @param data The data. * @param data The data.
* @param key The key. * @param key The key.
* @param transformation The name of the transformation, e.g., *DES/CBC/PKCS5Padding*. * @param transformation The name of the transformation,
* 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
* @param iv The buffer with the IV. The contents of the * @param iv The buffer with the IV. The contents of the
* buffer are copied to protect against subsequent modification. * buffer are copied to protect against subsequent modification.
* @return the bytes of AES encryption * @return the bytes of AES encryption
*/ */
@Throws(Exception::class)
fun encryptAES( fun encryptAES(
data: ByteArray?, data: ByteArray?,
key: ByteArray?, key: ByteArray?,
transformation: String?, transformation: String? = "DES/ECB/PKCS5Padding",
iv: ByteArray? iv: ByteArray? = null
): ByteArray? { ): ByteArray? {
return symmetricTemplate(data, key, "AES", transformation!!, iv, true) return symmetricTemplate(data, key, "AES", transformation!!, iv, true)
} }
@ -84,15 +89,17 @@ object EncoderUtils {
* *
* @param data The data. * @param data The data.
* @param key The key. * @param key The key.
* @param transformation The name of the transformation, e.g., *DES/CBC/PKCS5Padding*. * @param transformation The name of the transformation,
* 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
* @param iv The buffer with the IV. The contents of the * @param iv The buffer with the IV. The contents of the
* buffer are copied to protect against subsequent modification. * buffer are copied to protect against subsequent modification.
* @return the bytes of AES decryption for Base64-encode bytes * @return the bytes of AES decryption for Base64-encode bytes
*/ */
@Throws(Exception::class)
fun decryptBase64AES( fun decryptBase64AES(
data: ByteArray?, data: ByteArray?,
key: ByteArray?, key: ByteArray?,
transformation: String = "DES/CBC/PKCS5Padding", transformation: String = "DES/ECB/PKCS5Padding",
iv: ByteArray? = null iv: ByteArray? = null
): ByteArray? { ): ByteArray? {
return decryptAES(Base64.decode(data, Base64.NO_WRAP), key, transformation, iv) return decryptAES(Base64.decode(data, Base64.NO_WRAP), key, transformation, iv)
@ -103,15 +110,17 @@ object EncoderUtils {
* *
* @param data The data. * @param data The data.
* @param key The key. * @param key The key.
* @param transformation The name of the transformation, e.g., *DES/CBC/PKCS5Padding*. * @param transformation The name of the transformation,
* 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
* @param iv The buffer with the IV. The contents of the * @param iv The buffer with the IV. The contents of the
* buffer are copied to protect against subsequent modification. * buffer are copied to protect against subsequent modification.
* @return the bytes of AES decryption * @return the bytes of AES decryption
*/ */
@Throws(Exception::class)
fun decryptAES( fun decryptAES(
data: ByteArray?, data: ByteArray?,
key: ByteArray?, key: ByteArray?,
transformation: String = "DES/CBC/PKCS5Padding", transformation: String = "AES/ECB/PKCS5Padding",
iv: ByteArray? = null iv: ByteArray? = null
): ByteArray? { ): ByteArray? {
return symmetricTemplate(data, key, "AES", transformation, iv, false) return symmetricTemplate(data, key, "AES", transformation, iv, false)
@ -124,13 +133,15 @@ object EncoderUtils {
* @param data The data. * @param data The data.
* @param key The key. * @param key The key.
* @param algorithm The name of algorithm. * @param algorithm The name of algorithm.
* @param transformation The name of the transformation, e.g., <i>DES/CBC/PKCS5Padding</i>. * @param transformation The name of the transformation,
* 加密算法/加密模式/填充类型, <i>DES/CBC/PKCS5Padding</i>.
* @param iv The buffer with the IV. The contents of the * @param iv The buffer with the IV. The contents of the
* buffer are copied to protect against subsequent modification. * buffer are copied to protect against subsequent modification.
* @param isEncrypt True to encrypt, false otherwise. * @param isEncrypt True to encrypt, false otherwise.
* @return the bytes of symmetric encryption or decryption * @return the bytes of symmetric encryption or decryption
*/ */
@Suppress("SameParameterValue") @Suppress("SameParameterValue")
@Throws(Exception::class)
private fun symmetricTemplate( private fun symmetricTemplate(
data: ByteArray?, data: ByteArray?,
key: ByteArray?, key: ByteArray?,
@ -140,7 +151,7 @@ object EncoderUtils {
isEncrypt: Boolean isEncrypt: Boolean
): ByteArray? { ): ByteArray? {
return if (data == null || data.isEmpty() || key == null || key.isEmpty()) null return if (data == null || data.isEmpty() || key == null || key.isEmpty()) null
else try { else {
val keySpec = SecretKeySpec(key, algorithm) val keySpec = SecretKeySpec(key, algorithm)
val cipher = Cipher.getInstance(transformation) val cipher = Cipher.getInstance(transformation)
if (iv == null || iv.isEmpty()) { if (iv == null || iv.isEmpty()) {
@ -154,9 +165,6 @@ object EncoderUtils {
) )
} }
cipher.doFinal(data) cipher.doFinal(data)
} catch (e: Throwable) {
e.printStackTrace()
null
} }
} }

Loading…
Cancel
Save