|
@@ -1,13 +1,9 @@
|
|
|
package io.legado.app.utils
|
|
package io.legado.app.utils
|
|
|
|
|
|
|
|
import android.util.Base64
|
|
import android.util.Base64
|
|
|
-import cn.hutool.crypto.symmetric.SymmetricCrypto
|
|
|
|
|
-import java.security.spec.AlgorithmParameterSpec
|
|
|
|
|
-import javax.crypto.spec.IvParameterSpec
|
|
|
|
|
-import javax.crypto.spec.SecretKeySpec
|
|
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * transformations https://developer.android.google.cn/reference/kotlin/javax/crypto/Cipher?hl=en
|
|
|
|
|
|
|
+ * 编码工具 escape base64
|
|
|
*/
|
|
*/
|
|
|
@Suppress("unused")
|
|
@Suppress("unused")
|
|
|
object EncoderUtils {
|
|
object EncoderUtils {
|
|
@@ -42,302 +38,4 @@ object EncoderUtils {
|
|
|
return Base64.encodeToString(str.toByteArray(), flags)
|
|
return Base64.encodeToString(str.toByteArray(), flags)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //////////AES Start
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the Base64-encode bytes of AES encryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *AES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the Base64-encode bytes of AES encryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun encryptAES2Base64(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String? = "AES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return Base64.encode(encryptAES(data, key, transformation, iv), Base64.NO_WRAP)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of AES encryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *AES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES encryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun encryptAES(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String? = "AES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return symmetricTemplate(data, key, "AES", transformation!!, iv, true)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of AES decryption for Base64-encode bytes.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *AES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES decryption for Base64-encode bytes
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun decryptBase64AES(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String = "AES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return decryptAES(Base64.decode(data, Base64.NO_WRAP), key, transformation, iv)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of AES decryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *AES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES decryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun decryptAES(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String = "AES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return symmetricTemplate(data, key, "AES", transformation, iv, false)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //////////DES Start
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the Base64-encode bytes of DES encryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the Base64-encode bytes of AES encryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun encryptDES2Base64(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String? = "DES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return Base64.encode(encryptDES(data, key, transformation, iv), Base64.NO_WRAP)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of DES encryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES encryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun encryptDES(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String? = "DES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return symmetricTemplate(data, key, "DES", transformation!!, iv, true)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of DES decryption for Base64-encode bytes.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES decryption for Base64-encode bytes
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun decryptBase64DES(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String = "DES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return decryptDES(Base64.decode(data, Base64.NO_WRAP), key, transformation, iv)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of DES decryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DES/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES decryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun decryptDES(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String = "DES/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return symmetricTemplate(data, key, "DES", transformation, iv, false)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //////////DESede Start
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the Base64-encode bytes of DESede encryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DESede/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the Base64-encode bytes of AES encryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun encryptDESede2Base64(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String? = "DESede/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return Base64.encode(encryptDESede(data, key, transformation, iv), Base64.NO_WRAP)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of DESede encryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DESede/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES encryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun encryptDESede(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String? = "DESede/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return symmetricTemplate(data, key, "DESede", transformation!!, iv, true)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of DESede decryption for Base64-encode bytes.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DESede/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES decryption for Base64-encode bytes
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun decryptBase64DESede(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String = "DESede/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return decryptDESede(Base64.decode(data, Base64.NO_WRAP), key, transformation, iv)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of DESede decryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, *DESede/CBC/PKCS5Padding*.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @return the bytes of AES decryption
|
|
|
|
|
- */
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- fun decryptDESede(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- transformation: String = "DESede/ECB/PKCS5Padding",
|
|
|
|
|
- iv: ByteArray? = null
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return symmetricTemplate(data, key, "DESede", transformation, iv, false)
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Return the bytes of symmetric encryption or decryption.
|
|
|
|
|
- *
|
|
|
|
|
- * @param data The data.
|
|
|
|
|
- * @param key The key.
|
|
|
|
|
- * @param algorithm The name of algorithm.
|
|
|
|
|
- * @param transformation The name of the transformation,
|
|
|
|
|
- * 加密算法/加密模式/填充类型, <i>DES/CBC/PKCS5Padding</i>.
|
|
|
|
|
- * @param iv The buffer with the IV. The contents of the
|
|
|
|
|
- * buffer are copied to protect against subsequent modification.
|
|
|
|
|
- * @param isEncrypt True to encrypt, false otherwise.
|
|
|
|
|
- * @return the bytes of symmetric encryption or decryption
|
|
|
|
|
- */
|
|
|
|
|
- @Suppress("SameParameterValue")
|
|
|
|
|
- @Throws(Exception::class)
|
|
|
|
|
- private fun symmetricTemplate(
|
|
|
|
|
- data: ByteArray?,
|
|
|
|
|
- key: ByteArray?,
|
|
|
|
|
- algorithm: String,
|
|
|
|
|
- transformation: String,
|
|
|
|
|
- iv: ByteArray?,
|
|
|
|
|
- isEncrypt: Boolean
|
|
|
|
|
- ): ByteArray? {
|
|
|
|
|
- return if (data == null || data.isEmpty() || key == null || key.isEmpty()) null
|
|
|
|
|
- else {
|
|
|
|
|
- //hutool support zeropadding
|
|
|
|
|
- val keySpec = SecretKeySpec(key, algorithm)
|
|
|
|
|
- var params: AlgorithmParameterSpec? = null
|
|
|
|
|
- if (iv != null && !iv.isEmpty()) {
|
|
|
|
|
- params = IvParameterSpec(iv)
|
|
|
|
|
- }
|
|
|
|
|
- val symmetricCrypto = SymmetricCrypto(transformation, keySpec, params)
|
|
|
|
|
- if (isEncrypt) symmetricCrypto.encrypt(data) else symmetricCrypto.decrypt(data)
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|