js 方法添加aes加密解密方法

pull/946/head
ag2s20150909 4 years ago
parent b3545d3966
commit 86b8ca67c0
  1. 113
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  3. 124
      app/src/main/java/io/legado/app/utils/EncoderUtils.kt
  4. 4
      epublib/src/main/java/me/ag2s/epublib/util/ResourceUtil.java
  5. 2
      epublib/src/main/java/me/ag2s/epublib/util/StringUtil.java

@ -338,4 +338,117 @@ interface JsExtensions {
Debug.log(msg)
return msg
}
/**
* AES 解码为 ByteArray
*/
fun aesDecodeToByteArray(
str: String,
key: String,
transformation: String,
iv: String = ""
): ByteArray? {
return EncoderUtils.decryptAES(
data = str.encodeToByteArray(),
key = key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)
}
/**
* AES 解码为 String
*/
fun aesDecodeToString(
str: String,
key: String,
transformation: String,
iv: String = ""
): String? {
return aesDecodeToByteArray(str, key, transformation, iv)?.let { String(it) }
}
/**
* 已经base64的AES 解码为 ByteArray
*/
fun aesBase64DecodeToByteArray(
str: String,
key: String,
transformation: String,
iv: String = ""
): ByteArray? {
return EncoderUtils.decryptBase64AES(
data = str.encodeToByteArray(),
key = key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)
}
/**
* 已经base64的AES 解码为 String
*/
fun aesBase64DecodeToString(
str: String,
key: String,
transformation: String,
iv: String = ""
): String? {
return aesBase64DecodeToByteArray(str, key, transformation, iv)?.let { String(it) }
}
/**
* 加密aes为ByteArray
*/
fun aesEncodeByteArray(
data: String, key: String, transformation: String,
iv: String = ""
): ByteArray? {
return EncoderUtils.encryptAES(
data.encodeToByteArray(),
key = key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)
}
/**
* 加密aes为String
*/
fun aesEncodeString(
data: String, key: String, transformation: String,
iv: String = ""
): String? {
return aesEncodeByteArray(data, key, transformation, iv)?.let { String(it) }
}
/**
* 加密aes为Base64后的ByteArray
*/
fun aesBase64EncodeByteArray(
data: String, key: String, transformation: String,
iv: String = ""
): ByteArray? {
return EncoderUtils.encryptAES2Base64(
data.encodeToByteArray(),
key = key.encodeToByteArray(),
transformation,
iv.encodeToByteArray()
)
}
/**
* 加密aes为Base64后的String
*/
fun aesBase64EncodeString(
data: String, key: String, transformation: String,
iv: String = ""
): String? {
return aesBase64EncodeByteArray(data, key, transformation, iv)?.let { String(it) }
}
}

@ -207,7 +207,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
//set css
epubBook.resources.add(
Resource(
"body,div{background:white;margin:0 auto;padding:0;outline:none;width:100%;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{display:inline-block;width:100%;height:100%;max-width: 100%;max-height:100%;}".encodeToByteArray(),
"body,div{background:white;outline:none;width:100%;}h2{color:#005a9c;text-align:left;}p{text-indent:2em;text-align:justify;}img{display:inline-block;width:100%;height:auto;max-width: 100%;max-height:100%;}".encodeToByteArray(),
"css/style.css"
)
)

@ -1,6 +1,10 @@
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
@Suppress("unused")
object EncoderUtils {
@ -34,4 +38,124 @@ object EncoderUtils {
fun base64Encode(str: String, flags: Int = Base64.NO_WRAP): String? {
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, e.g., *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
*/
fun encryptAES2Base64(
data: ByteArray?,
key: ByteArray?,
transformation: String?,
iv: ByteArray?
): 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, e.g., *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
*/
fun encryptAES(
data: ByteArray?,
key: ByteArray?,
transformation: String?,
iv: ByteArray?
): 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, e.g., *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
*/
fun decryptBase64AES(
data: ByteArray?,
key: ByteArray?,
transformation: String?,
iv: ByteArray?
): 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, e.g., *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
*/
fun decryptAES(
data: ByteArray?,
key: ByteArray?,
transformation: String?,
iv: ByteArray?
): ByteArray? {
return symmetricTemplate(data, key, "AES", 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, e.g., <i>DES/CBC/PKCS5Padding</i>.
* @param isEncrypt True to encrypt, false otherwise.
* @return the bytes of symmetric encryption or decryption
*/
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 try {
val keySpec = SecretKeySpec(key, algorithm)
val cipher = Cipher.getInstance(transformation)
if (iv == null || iv.isEmpty()) {
cipher.init(if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE, keySpec)
} else {
val params: AlgorithmParameterSpec = IvParameterSpec(iv)
cipher.init(
if (isEncrypt) Cipher.ENCRYPT_MODE else Cipher.DECRYPT_MODE,
keySpec,
params
)
}
cipher.doFinal(data)
} catch (e: Throwable) {
e.printStackTrace()
null
}
}
}

@ -67,9 +67,7 @@ public class ResourceUtil {
if (s.length() != 0) {
if (s.contains("<img")) {
//加上div的话多看能点看大图,但掌阅的图会因为排版变得非常小。
//body.append("<div class=\"duokan-image-single img-note\">")
body.append(s);
//.append("</div>");
body.append("<div class=\"duokan-image-single img-note\">").append(s).append("</div>");
} else {
body.append("<p>").append(s).append("</p>");
}

@ -275,7 +275,7 @@ public class StringUtil {
}
public static String FixTrim(String s) {
if (s==null){
return "null";
return "";
}
Pattern r = Pattern.compile("^[\\s]{1,9}(.*?)[\\s]{1,9}$");
Matcher m = r.matcher(s);

Loading…
Cancel
Save