Merge pull request #2439 from Xwite/master

修复自动转换Base64 Hex导致的解密失败
pull/2440/head
Xwite 2 years ago committed by GitHub
commit 8e64419c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/src/main/assets/help/jsHelp.md
  2. 2
      app/src/main/java/io/legado/app/constant/AppPattern.kt
  3. 44
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  4. 6
      app/src/main/java/io/legado/app/utils/StringExtensions.kt
  5. 16
      app/src/main/java/io/legado/app/utils/StringUtils.kt

@ -162,14 +162,19 @@ deleteFile(path: String)
> DES transformation默认实现DES/ECB/PKCS5Padding > DES transformation默认实现DES/ECB/PKCS5Padding
> TripleDES tansformation默认实现DESede/ECB/PKCS5Padding > TripleDES tansformation默认实现DESede/ECB/PKCS5Padding
> 内部实现为cn.hutool.crypto 解密加密接口支持ByteArray|Base64String|HexString|InputStream > 内部实现为cn.hutool.crypto 解密加密接口支持ByteArray|Base64String|HexString|InputStream
> 输入参数key iv 支持ByteArray|Base64String|HexString|Utf8String > 输入参数key iv 支持ByteArray|Utf8String
> 如果key iv 为Hex Base64,且需要解码为ByteArray,调用java.decodeBase64Hex
``` ```
//解密为ByteArray 字符串 //解密为ByteArray 字符串
java.createSymmetricCrypto(transformation, key, iv).decrypt(data) java.createSymmetricCrypto(transformation, key, iv).decrypt(data)
java.createSymmetricCrypto(transformation, key, iv).decryptStr(data) java.createSymmetricCrypto(transformation, key, iv).decryptStr(data)
//加密为ByteArray Base64字符 HEX字符 //加密为ByteArray Base64字符 HEX字符
java.createSymmetricCrypto(transformation, key, iv).encrypt(data) java.createSymmetricCrypto(transformation, key, iv).encrypt(data)
java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data) java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data)
java.createSymmetricCrypto(transformation, key, iv).encryptHex(data) java.createSymmetricCrypto(transformation, key, iv).encryptHex(data)
``` ```
* 摘要 * 摘要

@ -13,8 +13,6 @@ object AppPattern {
//dataURL图片类型 //dataURL图片类型
val dataUriRegex = Regex("data:.*?;base64,(.*)") val dataUriRegex = Regex("data:.*?;base64,(.*)")
//Base64字符串
val base64Regex = Regex("^[a-zA-Z0-9\\+\\/=]+$")
val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著") val nameRegex = Regex("\\s+作\\s*者.*|\\s+\\S+\\s+著")
val authorRegex = Regex("^\\s*作\\s*者[::\\s]+|\\s+著") val authorRegex = Regex("^\\s*作\\s*者[::\\s]+|\\s+著")

@ -7,6 +7,7 @@ import cn.hutool.crypto.digest.DigestUtil
import cn.hutool.crypto.digest.HMac import cn.hutool.crypto.digest.HMac
import cn.hutool.core.util.HexUtil import cn.hutool.core.util.HexUtil
import cn.hutool.crypto.symmetric.SymmetricCrypto import cn.hutool.crypto.symmetric.SymmetricCrypto
import cn.hutool.crypto.SecureUtil
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
@ -658,51 +659,50 @@ interface JsExtensions {
* java.createSymmetricCrypto(transformation, key, iv).encrypt(data) * java.createSymmetricCrypto(transformation, key, iv).encrypt(data)
* java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data) * java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data)
* java.createSymmetricCrypto(transformation, key, iv).encryptHex(data) * java.createSymmetricCrypto(transformation, key, iv).encryptHex(data)
*/
/* 自动转换key iv 到ByteArray 支持base64 hex uft8*/ * 如果key iv 为Hex Base64,且需要解码为ByteArray调用java.decodeBase64Hex
fun createSymmetricCrypto(
transformation: String, */
key: String /* iv key 为Base64String HexString 到ByteArray的转换函数 */
): SymmetricCrypto { fun decodeBase64Hex(str: String): ByteArray? {
return createSymmetricCrypto(transformation, key, null) return SecureUtil.decode(str)
} }
/* 调用SymmetricCrypto key为null时使用随机密钥*/
fun createSymmetricCrypto( fun createSymmetricCrypto(
transformation: String, transformation: String,
key: String, key: ByteArray?,
iv: String? iv: ByteArray?
): SymmetricCrypto { ): SymmetricCrypto {
return createSymmetricCrypto( val symmetricCrypto = SymmetricCrypto(transformation, key)
transformation, return if (iv != null && !iv.isEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto
StringUtils.encodeStringToByteArray(key),
StringUtils.encodeStringToByteArray(iv)
)
} }
/* 调用SymmetricCrypto key为null时使用随机密钥*/
fun createSymmetricCrypto( fun createSymmetricCrypto(
transformation: String transformation: String,
key: ByteArray
): SymmetricCrypto { ): SymmetricCrypto {
return createSymmetricCrypto(transformation, null, null) return createSymmetricCrypto(transformation, key, null)
} }
fun createSymmetricCrypto( fun createSymmetricCrypto(
transformation: String, transformation: String,
key: ByteArray key: String
): SymmetricCrypto { ): SymmetricCrypto {
return createSymmetricCrypto(transformation, key, null) return createSymmetricCrypto(transformation, key, null)
} }
fun createSymmetricCrypto( fun createSymmetricCrypto(
transformation: String, transformation: String,
key: ByteArray?, key: String,
iv: ByteArray? iv: String?
): SymmetricCrypto { ): SymmetricCrypto {
val symmetricCrypto = SymmetricCrypto(transformation, key) return createSymmetricCrypto(
return if (iv != null && !iv.isEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto transformation, key.encodeToByteArray(), iv?.encodeToByteArray()
)
} }
//******************消息摘要/散列消息鉴别码************************// //******************消息摘要/散列消息鉴别码************************//
/** /**

@ -8,7 +8,6 @@ import android.net.Uri
import android.text.Editable import android.text.Editable
import cn.hutool.core.lang.Validator import cn.hutool.core.lang.Validator
import io.legado.app.constant.AppPattern.dataUriRegex import io.legado.app.constant.AppPattern.dataUriRegex
import io.legado.app.constant.AppPattern.base64Regex
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -39,11 +38,6 @@ fun String?.isDataUrl() =
dataUriRegex.matches(it) dataUriRegex.matches(it)
} ?: false } ?: false
fun String?.isBase64() =
this?.let {
base64Regex.matches(it)
} ?: false
fun String?.isJson(): Boolean = fun String?.isJson(): Boolean =
this?.run { this?.run {
val str = this.trim() val str = this.trim()

@ -1,12 +1,8 @@
package io.legado.app.utils package io.legado.app.utils
import android.util.Base64
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.text.TextUtils.isEmpty import android.text.TextUtils.isEmpty
import cn.hutool.core.util.HexUtil
import cn.hutool.core.lang.Validator
import java.text.DecimalFormat import java.text.DecimalFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -291,16 +287,4 @@ object StringUtils {
return buf.toString() return buf.toString()
} }
/**
* 自动识别Base64 Hex Utf8字符串 并转成ByteArray
*/
fun encodeStringToByteArray(str: String?): ByteArray? {
return when {
str.isNullOrBlank() -> null
Validator.isHex(str) -> HexUtil.decodeHex(str)
str.isBase64() -> Base64.decode(str, Base64.DEFAULT)
else -> str.encodeToByteArray()
}
}
} }

Loading…
Cancel
Save