feat(JsExtensions): use EncoderUtils implement DES/tripleDES

bug: iv must be null when mode is `ECB`
feat: implements DES
docs: update jsHelp.md
pull/1902/head
Xwite 3 years ago
parent c1fb5b80ac
commit c42cf0a59e
  1. 10
      app/src/main/assets/help/jsHelp.md
  2. 171
      app/src/main/java/io/legado/app/help/JsExtensions.kt

@ -143,6 +143,16 @@ java.aesEncodeToString(str: String, key: String, transformation: String, iv: Str
java.aesEncodeToBase64String(str: String, key: String, transformation: String, iv: String) java.aesEncodeToBase64String(str: String, key: String, transformation: String, iv: String)
``` ```
* DES
```
java.desDecodeToString(str: String, key: String, transformation: String, iv: String)
java.desBase64DecodeToString(str: String, key: String, transformation: String, iv: String)
java.desEncodeToString(str: String, key: String, transformation: String, iv: String)
java.desEncodeToBase64String(str: String, key: String, transformation: String, iv: String)
```
* 3DES * 3DES
``` ```
* @param data 被加密的字符串 * @param data 被加密的字符串

@ -4,8 +4,6 @@ import android.net.Uri
import android.util.Base64 import android.util.Base64
import androidx.annotation.Keep import androidx.annotation.Keep
import cn.hutool.crypto.digest.DigestUtil import cn.hutool.crypto.digest.DigestUtil
import cn.hutool.crypto.symmetric.AES
import cn.hutool.crypto.symmetric.DESede
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
@ -299,14 +297,6 @@ interface JsExtensions {
return EncoderUtils.base64Encode(str, flags) return EncoderUtils.base64Encode(str, flags)
} }
fun md5Encode(str: String): String {
return MD5Utils.md5Encode(str)
}
fun md5Encode16(str: String): String {
return MD5Utils.md5Encode16(str)
}
/** /**
* 格式化时间 * 格式化时间
*/ */
@ -602,6 +592,13 @@ interface JsExtensions {
return UUID.randomUUID().toString() return UUID.randomUUID().toString()
} }
fun androidId(): String {
return AppConst.androidId
}
//******************对称加密解密************************//
/////AES
/** /**
* AES 解码为 ByteArray * AES 解码为 ByteArray
* @param str 传入的AES加密的数据 * @param str 传入的AES加密的数据
@ -640,6 +637,31 @@ interface JsExtensions {
return aesDecodeToByteArray(str, key, transformation, iv)?.let { String(it) } return aesDecodeToByteArray(str, key, transformation, iv)?.let { String(it) }
} }
/**
* AES解码为String算法参数经过Base64加密
*
* @param data 加密的字符串
* @param key Base64后的密钥
* @param mode 模式
* @param padding 补码方式
* @param iv Base64后的加盐
* @return 解密后的字符串
*/
fun aesDecodeArgsBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return EncoderUtils.decryptAES(
data.encodeToByteArray(),
Base64.decode(key, Base64.NO_WRAP),
"AES/${mode}/${padding}",
Base64.decode(iv, Base64.NO_WRAP)
)?.let { String(it) }
}
/** /**
* 已经base64的AES 解码为 ByteArray * 已经base64的AES 解码为 ByteArray
* @param str 传入的AES Base64加密的数据 * @param str 传入的AES Base64加密的数据
@ -753,35 +775,78 @@ interface JsExtensions {
return aesEncodeToBase64ByteArray(data, key, transformation, iv)?.let { String(it) } return aesEncodeToBase64ByteArray(data, key, transformation, iv)?.let { String(it) }
} }
fun androidId(): String {
return AppConst.androidId
}
/** /**
* AES解密算法参数经过Base64加密 * AES加密并转为Base64算法参数经过Base64加密
* *
* @param data 加密的字符串 * @param data 加密的字符串
* @param key Base64后的密钥 * @param key Base64后的密钥
* @param mode 模式 * @param mode 模式
* @param padding 补码方式 * @param padding 补码方式
* @param iv Base64后的加盐 * @param iv Base64后的加盐
* @return 解密后的字符串 * @return 加密后的Base64
*/ */
fun aesDecodeArgsBase64Str( fun aesEncodeArgsBase64Str(
data: String, data: String,
key: String, key: String,
mode: String, mode: String,
padding: String, padding: String,
iv: String iv: String
): String? { ): String? {
return AES( return EncoderUtils.encryptAES2Base64(
mode, data.encodeToByteArray(),
padding,
Base64.decode(key, Base64.NO_WRAP), Base64.decode(key, Base64.NO_WRAP),
"AES/${mode}/${padding}",
Base64.decode(iv, Base64.NO_WRAP) Base64.decode(iv, Base64.NO_WRAP)
).decryptStr(data) )?.let { String(it) }
}
/////DES
fun desDecodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return EncoderUtils.decryptDES(
data.encodeToByteArray(),
key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)?.let { String(it) }
}
fun desBase64DecodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return EncoderUtils.decryptBase64DES(
data.encodeToByteArray(),
key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)?.let { String(it) }
} }
fun desEncodeToString(
data: String, key: String, transformation: String, iv: String
): String? {
return EncoderUtils.encryptDES(
data.encodeToByteArray(),
key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)?.let { String(it) }
}
fun desEncodeToBase64String(
data: String, key: String, transformation: String, iv: String
): String? {
return EncoderUtils.encryptDES2Base64(
data.encodeToByteArray(),
key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)?.let { String(it) }
}
//////3DES
/** /**
* 3DES解密 * 3DES解密
* *
@ -799,7 +864,12 @@ interface JsExtensions {
padding: String, padding: String,
iv: String iv: String
): String? { ): String? {
return DESede(mode, padding, key.toByteArray(), iv.toByteArray()).decryptStr(data) return EncoderUtils.decryptDESede(
data.encodeToByteArray(),
key.encodeToByteArray(),
"DESede/${mode}/${padding}",
iv.encodeToByteArray()
)?.let { String(it) }
} }
/** /**
@ -819,38 +889,14 @@ interface JsExtensions {
padding: String, padding: String,
iv: String iv: String
): String? { ): String? {
return DESede( return EncoderUtils.decryptDESede(
mode, data.encodeToByteArray(),
padding,
Base64.decode(key, Base64.NO_WRAP), Base64.decode(key, Base64.NO_WRAP),
"DESede/${mode}/${padding}",
Base64.decode(iv, Base64.NO_WRAP) Base64.decode(iv, Base64.NO_WRAP)
).decryptStr(data) )?.let { String(it) }
} }
/**
* AES加密并转为Base64算法参数经过Base64加密
*
* @param data 被加密的字符串
* @param key Base64后的密钥
* @param mode 模式
* @param padding 补码方式
* @param iv Base64后的加盐
* @return 加密后的Base64
*/
fun aesEncodeArgsBase64Str(
data: String,
key: String,
mode: String,
padding: String,
iv: String
): String? {
return AES(
mode,
padding,
Base64.decode(key, Base64.NO_WRAP),
Base64.decode(iv, Base64.NO_WRAP)
).encryptBase64(data)
}
/** /**
* 3DES加密并转为Base64 * 3DES加密并转为Base64
@ -869,7 +915,12 @@ interface JsExtensions {
padding: String, padding: String,
iv: String iv: String
): String? { ): String? {
return DESede(mode, padding, key.toByteArray(), iv.toByteArray()).encryptBase64(data) return EncoderUtils.encryptDESede2Base64(
data.encodeToByteArray(),
key.encodeToByteArray(),
"DESede/${mode}/${padding}",
iv.encodeToByteArray()
)?.let { String(it) }
} }
/** /**
@ -889,14 +940,16 @@ interface JsExtensions {
padding: String, padding: String,
iv: String iv: String
): String? { ): String? {
return DESede( return EncoderUtils.encryptDESede2Base64(
mode, data.encodeToByteArray(),
padding,
Base64.decode(key, Base64.NO_WRAP), Base64.decode(key, Base64.NO_WRAP),
"DESede/${mode}/${padding}",
Base64.decode(iv, Base64.NO_WRAP) Base64.decode(iv, Base64.NO_WRAP)
).encryptBase64(data) )?.let { String(it) }
} }
//******************消息摘要************************//
/** /**
* 生成摘要并转为16进制字符串 * 生成摘要并转为16进制字符串
* *
@ -925,4 +978,12 @@ interface JsExtensions {
return Base64.encodeToString(DigestUtil.digester(algorithm).digest(data), Base64.NO_WRAP) return Base64.encodeToString(DigestUtil.digester(algorithm).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