Преглед изворни кода

feat(EncoderUtils): add DES DESede method

Xwite пре 4 година
родитељ
комит
467c39352b
1 измењених фајлова са 174 додато и 1 уклоњено
  1. 174 1
      app/src/main/java/io/legado/app/utils/EncoderUtils.kt

+ 174 - 1
app/src/main/java/io/legado/app/utils/EncoderUtils.kt

@@ -126,6 +126,179 @@ object EncoderUtils {
         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.
@@ -166,4 +339,4 @@ object EncoderUtils {
     }
 
 
-}
+}