From e35b592691c3925c58d00fa107963e60c1afa8ae Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Fri, 27 May 2022 12:45:29 +0800 Subject: [PATCH] feat(EncoderUtils): support zeropadding --- .../java/io/legado/app/utils/EncoderUtils.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt index f410075ec..9b3d192de 100644 --- a/app/src/main/java/io/legado/app/utils/EncoderUtils.kt +++ b/app/src/main/java/io/legado/app/utils/EncoderUtils.kt @@ -2,9 +2,9 @@ package io.legado.app.utils import android.util.Base64 import java.security.spec.AlgorithmParameterSpec -import javax.crypto.Cipher import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.SecretKeySpec +import cn.hutool.crypto.symmetric.SymmetricCrypto /** * transformations https://developer.android.google.cn/reference/kotlin/javax/crypto/Cipher?hl=en @@ -328,16 +328,14 @@ object EncoderUtils { ): ByteArray? { return if (data == null || data.isEmpty() || key == null || key.isEmpty()) null else { + //hutool support zeropadding val keySpec = SecretKeySpec(key, algorithm) - val cipher = Cipher.getInstance(transformation) - val mode = if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE - if (iv == null || iv.isEmpty()) { - cipher.init(mode, keySpec) - } else { - val params: AlgorithmParameterSpec = IvParameterSpec(iv) - cipher.init(mode, keySpec, params) + var params: AlgorithmParameterSpec? = null + if (iv != null && !iv.isEmpty()) { + params = IvParameterSpec(iv) } - cipher.doFinal(data) + val symmetricCrypto = SymmetricCrypto(transformation, keySpec, params) + return if (isEncrypt) symmetricCrypto.encrypt(data) else symmetricCrypto.decrypt(data) } }