分离js加解密文件,保留之前的方法,标记为弃用

pull/2448/head
kunfei 2 years ago
parent 2838bd6984
commit a57eb82236
  1. 474
      app/src/main/java/io/legado/app/help/JsEncodeUtils.kt
  2. 128
      app/src/main/java/io/legado/app/help/JsExtensions.kt

@ -0,0 +1,474 @@
package io.legado.app.help
import android.util.Base64
import cn.hutool.crypto.digest.DigestUtil
import cn.hutool.crypto.digest.HMac
import cn.hutool.crypto.symmetric.SymmetricCrypto
import io.legado.app.utils.MD5Utils
interface JsEncodeUtils {
fun md5Encode(str: String): String {
return MD5Utils.md5Encode(str)
}
fun md5Encode16(str: String): String {
return MD5Utils.md5Encode16(str)
}
//******************对称加密解密************************//
/**
* 在js中这样使用
* java.createSymmetricCrypto(transformation, key, iv).decrypt(data)
* java.createSymmetricCrypto(transformation, key, iv).decryptStr(data)
* java.createSymmetricCrypto(transformation, key, iv).encrypt(data)
* java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data)
* java.createSymmetricCrypto(transformation, key, iv).encryptHex(data)
*/
/* 调用SymmetricCrypto key为null时使用随机密钥*/
fun createSymmetricCrypto(
transformation: String,
key: ByteArray?,
iv: ByteArray?
): SymmetricCrypto {
val symmetricCrypto = SymmetricCrypto(transformation, key)
return if (iv != null && iv.isNotEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto
}
fun createSymmetricCrypto(
transformation: String,
key: ByteArray
): SymmetricCrypto {
return createSymmetricCrypto(transformation, key, null)
}
fun createSymmetricCrypto(
transformation: String,
key: String
): SymmetricCrypto {
return createSymmetricCrypto(transformation, key, null)
}
fun createSymmetricCrypto(
transformation: String,
key: String,
iv: String?
): SymmetricCrypto {
return createSymmetricCrypto(
transformation, key.encodeToByteArray(), iv?.encodeToByteArray()
)
}
//******************对称加密解密old************************//
/////AES
/**
* AES 解码为 ByteArray
* @param str 传入的AES加密的数据
* @param key AES 解密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decrypt(str)")
)
fun aesDecodeToByteArray(
str: String, key: String, transformation: String, iv: String
): ByteArray? {
return createSymmetricCrypto(transformation, key, iv).decrypt(str)
}
/**
* AES 解码为 String
* @param str 传入的AES加密的数据
* @param key AES 解密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(str)")
)
fun aesDecodeToString(
str: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).decryptStr(str)
}
/**
* AES解码为String算法参数经过Base64加密
*
* @param data 加密的字符串
* @param key Base64后的密钥
* @param mode 模式
* @param padding 补码方式
* @param iv Base64后的加盐
* @return 解密后的字符串
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)")
)
fun aesDecodeArgsBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return createSymmetricCrypto(
"AES/${mode}/${padding}",
Base64.decode(key, Base64.NO_WRAP),
Base64.decode(iv, Base64.NO_WRAP)
).decryptStr(data)
}
/**
* 已经base64的AES 解码为 ByteArray
* @param str 传入的AES Base64加密的数据
* @param key AES 解密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decrypt(str)")
)
fun aesBase64DecodeToByteArray(
str: String, key: String, transformation: String, iv: String
): ByteArray? {
return createSymmetricCrypto(transformation, key, iv).decrypt(str)
}
/**
* 已经base64的AES 解码为 String
* @param str 传入的AES Base64加密的数据
* @param key AES 解密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(str)")
)
fun aesBase64DecodeToString(
str: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).decryptStr(str)
}
/**
* 加密aes为ByteArray
* @param data 传入的原始数据
* @param key AES加密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decrypt(data)")
)
fun aesEncodeToByteArray(
data: String, key: String, transformation: String, iv: String
): ByteArray? {
return createSymmetricCrypto(transformation, key, iv).decrypt(data)
}
/**
* 加密aes为String
* @param data 传入的原始数据
* @param key AES加密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)")
)
fun aesEncodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).decryptStr(data)
}
/**
* 加密aes后Base64化的ByteArray
* @param data 传入的原始数据
* @param key AES加密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data).toByteArray()")
)
fun aesEncodeToBase64ByteArray(
data: String, key: String, transformation: String, iv: String
): ByteArray? {
return createSymmetricCrypto(transformation, key, iv).encryptBase64(data).toByteArray()
}
/**
* 加密aes后Base64化的String
* @param data 传入的原始数据
* @param key AES加密的key
* @param transformation AES加密的方式
* @param iv ECB模式的偏移向量
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)")
)
fun aesEncodeToBase64String(
data: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).encryptBase64(data)
}
/**
* AES加密并转为Base64算法参数经过Base64加密
*
* @param data 被加密的字符串
* @param key Base64后的密钥
* @param mode 模式
* @param padding 补码方式
* @param iv Base64后的加盐
* @return 加密后的Base64
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)")
)
fun aesEncodeArgsBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return createSymmetricCrypto("AES/${mode}/${padding}", key, iv).encryptBase64(data)
}
/////DES
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)")
)
fun desDecodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).decryptStr(data)
}
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)")
)
fun desBase64DecodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).decryptStr(data)
}
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encrypt(data)")
)
fun desEncodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return String(createSymmetricCrypto(transformation, key, iv).encrypt(data))
}
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)")
)
fun desEncodeToBase64String(
data: String, key: String, transformation: String, iv: String
): String? {
return createSymmetricCrypto(transformation, key, iv).encryptBase64(data)
}
//////3DES
/**
* 3DES解密
*
* @param data 加密的字符串
* @param key 密钥
* @param mode 模式
* @param padding 补码方式
* @param iv 加盐
* @return 解密后的字符串
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)")
)
fun tripleDESDecodeStr(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return createSymmetricCrypto("DESede/${mode}/${padding}", key, iv).decryptStr(data)
}
/**
* 3DES解密算法参数经过Base64加密
*
* @param data 加密的字符串
* @param key Base64后的密钥
* @param mode 模式
* @param padding 补码方式
* @param iv Base64后的加盐
* @return 解密后的字符串
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).decryptStr(data)")
)
fun tripleDESDecodeArgsBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return createSymmetricCrypto(
"DESede/${mode}/${padding}",
Base64.decode(key, Base64.NO_WRAP),
iv.encodeToByteArray()
).decryptStr(data)
}
/**
* 3DES加密并转为Base64
*
* @param data 被加密的字符串
* @param key 密钥
* @param mode 模式
* @param padding 补码方式
* @param iv 加盐
* @return 加密后的Base64
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)")
)
fun tripleDESEncodeBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return createSymmetricCrypto("DESede/${mode}/${padding}", key, iv)
.encryptBase64(data)
}
/**
* 3DES加密并转为Base64算法参数经过Base64加密
*
* @param data 被加密的字符串
* @param key Base64后的密钥
* @param mode 模式
* @param padding 补码方式
* @param iv Base64后的加盐
* @return 加密后的Base64
*/
@Deprecated(
"过于繁琐弃用",
ReplaceWith("createSymmetricCrypto(transformation, key, iv).encryptBase64(data)")
)
fun tripleDESEncodeArgsBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return createSymmetricCrypto(
"DESede/${mode}/${padding}",
Base64.decode(key, Base64.NO_WRAP),
iv.encodeToByteArray()
).encryptBase64(data)
}
//******************消息摘要/散列消息鉴别码************************//
/**
* 生成摘要并转为16进制字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @return 16进制字符串
*/
fun digestHex(
data: String,
algorithm: String,
): String {
return DigestUtil.digester(algorithm).digestHex(data)
}
/**
* 生成摘要并转为Base64字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @return Base64字符串
*/
fun digestBase64Str(
data: String,
algorithm: String,
): String {
return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP)
}
/**
* 生成散列消息鉴别码并转为16进制字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @param key 密钥
* @return 16进制字符串
*/
@Suppress("FunctionName")
fun HMacHex(
data: String,
algorithm: String,
key: String
): String {
return HMac(algorithm, key.toByteArray()).digestHex(data)
}
/**
* 生成散列消息鉴别码并转为Base64字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @param key 密钥
* @return Base64字符串
*/
@Suppress("FunctionName")
fun HMacBase64(
data: String,
algorithm: String,
key: String
): String {
return Base64.encodeToString(
HMac(algorithm, key.toByteArray()).digest(data),
Base64.NO_WRAP
)
}
}

@ -4,10 +4,6 @@ import android.net.Uri
import android.util.Base64
import androidx.annotation.Keep
import cn.hutool.core.util.HexUtil
import cn.hutool.crypto.SecureUtil
import cn.hutool.crypto.digest.DigestUtil
import cn.hutool.crypto.digest.HMac
import cn.hutool.crypto.symmetric.SymmetricCrypto
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.constant.AppLog
@ -47,7 +43,7 @@ import java.util.zip.ZipInputStream
*/
@Keep
@Suppress("unused")
interface JsExtensions {
interface JsExtensions : JsEncodeUtils {
fun getSource(): BaseSource?
@ -667,126 +663,4 @@ interface JsExtensions {
return AppConst.androidId
}
//******************对称加密解密************************//
/**
* 在js中这样使用
* java.createSymmetricCrypto(transformation, key, iv).decrypt(data)
* java.createSymmetricCrypto(transformation, key, iv).decryptStr(data)
* java.createSymmetricCrypto(transformation, key, iv).encrypt(data)
* java.createSymmetricCrypto(transformation, key, iv).encryptBase64(data)
* java.createSymmetricCrypto(transformation, key, iv).encryptHex(data)
*/
/* 调用SymmetricCrypto key为null时使用随机密钥*/
fun createSymmetricCrypto(
transformation: String,
key: ByteArray?,
iv: ByteArray?
): SymmetricCrypto {
val symmetricCrypto = SymmetricCrypto(transformation, key)
return if (iv != null && iv.isNotEmpty()) symmetricCrypto.setIv(iv) else symmetricCrypto
}
fun createSymmetricCrypto(
transformation: String,
key: ByteArray
): SymmetricCrypto {
return createSymmetricCrypto(transformation, key, null)
}
fun createSymmetricCrypto(
transformation: String,
key: String
): SymmetricCrypto {
return createSymmetricCrypto(transformation, key, null)
}
fun createSymmetricCrypto(
transformation: String,
key: String,
iv: String?
): SymmetricCrypto {
return createSymmetricCrypto(
transformation, key.encodeToByteArray(), iv?.encodeToByteArray()
)
}
//******************消息摘要/散列消息鉴别码************************//
/**
* 生成摘要并转为16进制字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @return 16进制字符串
*/
fun digestHex(
data: String,
algorithm: String,
): String {
return DigestUtil.digester(algorithm).digestHex(data)
}
/**
* 生成摘要并转为Base64字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @return Base64字符串
*/
fun digestBase64Str(
data: String,
algorithm: String,
): String {
return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP)
}
/**
* 生成散列消息鉴别码并转为16进制字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @param key 密钥
* @return 16进制字符串
*/
@Suppress("FunctionName")
fun HMacHex(
data: String,
algorithm: String,
key: String
): String {
return HMac(algorithm, key.toByteArray()).digestHex(data)
}
/**
* 生成散列消息鉴别码并转为Base64字符串
*
* @param data 被摘要数据
* @param algorithm 签名算法
* @param key 密钥
* @return Base64字符串
*/
@Suppress("FunctionName")
fun HMacBase64(
data: String,
algorithm: String,
key: String
): String {
return Base64.encodeToString(
HMac(algorithm, key.toByteArray()).digest(data),
Base64.NO_WRAP
)
}
fun md5Encode(str: String): String {
return MD5Utils.md5Encode(str)
}
fun md5Encode16(str: String): String {
return MD5Utils.md5Encode16(str)
}
}

Loading…
Cancel
Save