From c42cf0a59e93eed8475ac8c36a88da4f5f0a3987 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Mon, 23 May 2022 21:40:38 +0800 Subject: [PATCH] feat(JsExtensions): use EncoderUtils implement DES/tripleDES bug: iv must be null when mode is `ECB` feat: implements DES docs: update jsHelp.md --- app/src/main/assets/help/jsHelp.md | 10 + .../java/io/legado/app/help/JsExtensions.kt | 171 ++++++++++++------ 2 files changed, 126 insertions(+), 55 deletions(-) diff --git a/app/src/main/assets/help/jsHelp.md b/app/src/main/assets/help/jsHelp.md index 420f45ebe..9e5ba8bc4 100644 --- a/app/src/main/assets/help/jsHelp.md +++ b/app/src/main/assets/help/jsHelp.md @@ -143,6 +143,16 @@ java.aesEncodeToString(str: String, key: String, transformation: String, iv: Str 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 ``` * @param data 被加密的字符串 diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 0d1c117a3..e42d951b2 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -4,8 +4,6 @@ import android.net.Uri import android.util.Base64 import androidx.annotation.Keep 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.dateFormat import io.legado.app.constant.AppLog @@ -299,14 +297,6 @@ interface JsExtensions { 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() } + fun androidId(): String { + return AppConst.androidId + } + +//******************对称加密解密************************// + + /////AES /** * AES 解码为 ByteArray * @param str 传入的AES加密的数据 @@ -640,6 +637,31 @@ interface JsExtensions { 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 * @param str 传入的AES Base64加密的数据 @@ -753,35 +775,78 @@ interface JsExtensions { 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 mode 模式 * @param padding 补码方式 * @param iv Base64后的加盐 - * @return 解密后的字符串 + * @return 加密后的Base64 */ - fun aesDecodeArgsBase64Str( + fun aesEncodeArgsBase64Str( data: String, key: String, mode: String, padding: String, iv: String ): String? { - return AES( - mode, - padding, + return EncoderUtils.encryptAES2Base64( + data.encodeToByteArray(), Base64.decode(key, Base64.NO_WRAP), + "AES/${mode}/${padding}", 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解密 * @@ -799,7 +864,12 @@ interface JsExtensions { padding: String, iv: 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, iv: String ): String? { - return DESede( - mode, - padding, + return EncoderUtils.decryptDESede( + data.encodeToByteArray(), Base64.decode(key, Base64.NO_WRAP), + "DESede/${mode}/${padding}", 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 @@ -869,7 +915,12 @@ interface JsExtensions { padding: String, iv: 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, iv: String ): String? { - return DESede( - mode, - padding, + return EncoderUtils.encryptDESede2Base64( + data.encodeToByteArray(), Base64.decode(key, Base64.NO_WRAP), + "DESede/${mode}/${padding}", Base64.decode(iv, Base64.NO_WRAP) - ).encryptBase64(data) + )?.let { String(it) } } +//******************消息摘要************************// + /** * 生成摘要,并转为16进制字符串 * @@ -925,4 +978,12 @@ interface JsExtensions { 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) + } + }