From 1a2612ba86f7aeb34fc8b4bb774a751ada364328 Mon Sep 17 00:00:00 2001 From: TonyJiangWJ Date: Sat, 26 Dec 2020 12:18:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=A0=E8=A7=A3=E5=AF=86=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autojs-tool-encrypt/.gitignore | 1 + autojs-tool-encrypt/build.gradle | 49 +++++++++ .../src/main/AndroidManifest.xml | 2 + .../tony/autojs/tool/encrypt/AESEncrypt.java | 66 +++++++++++ .../autojs/tool/encrypt/DecryptRunner.java | 103 ++++++++++++++++++ .../autojs/tool/encrypt/EnginesDelegate.java | 29 +++++ 6 files changed, 250 insertions(+) create mode 100644 autojs-tool-encrypt/.gitignore create mode 100644 autojs-tool-encrypt/build.gradle create mode 100644 autojs-tool-encrypt/src/main/AndroidManifest.xml create mode 100644 autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/AESEncrypt.java create mode 100644 autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/DecryptRunner.java create mode 100644 autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/EnginesDelegate.java diff --git a/autojs-tool-encrypt/.gitignore b/autojs-tool-encrypt/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/autojs-tool-encrypt/.gitignore @@ -0,0 +1 @@ +/build diff --git a/autojs-tool-encrypt/build.gradle b/autojs-tool-encrypt/build.gradle new file mode 100644 index 00000000..a6a8250e --- /dev/null +++ b/autojs-tool-encrypt/build.gradle @@ -0,0 +1,49 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 29 + + + defaultConfig { + minSdkVersion 21 + targetSdkVersion 29 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles 'consumer-rules.pro' + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + + lintOptions { + abortOnError false + } +} + + +task makeEncryptJar(type: Copy) { + dependsOn build + // 删除存在的 + delete 'build/libs/encrypt-sdk.jar' + // 设置拷贝的文件 + from('build/intermediates/packaged-classes/release/') + // 打进jar包后的文件目录 + into('build/libs/') + // 将classes.jar放入build/libs/目录下 + // include ,exclude参数来设置过滤 + //(我们只关心classes.jar这个文件) + include('classes.jar') + // 重命名 + rename ('classes.jar', 'encrypt-sdk.jar') +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + api project(path: ':autojs') +} diff --git a/autojs-tool-encrypt/src/main/AndroidManifest.xml b/autojs-tool-encrypt/src/main/AndroidManifest.xml new file mode 100644 index 00000000..f9fba661 --- /dev/null +++ b/autojs-tool-encrypt/src/main/AndroidManifest.xml @@ -0,0 +1,2 @@ + diff --git a/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/AESEncrypt.java b/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/AESEncrypt.java new file mode 100644 index 00000000..96d2b432 --- /dev/null +++ b/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/AESEncrypt.java @@ -0,0 +1,66 @@ +package com.tony.autojs.tool.encrypt; + +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +public class AESEncrypt { + public static SecretKey loadKeyByBytes(byte[] keyBytes) { + return new SecretKeySpec(keyBytes, 0, keyBytes.length, "AES"); + } + + public static byte[] generateAesKey() throws NoSuchAlgorithmException { + KeyGenerator generator = KeyGenerator.getInstance("AES"); + // The AES key size in number of bits + generator.init(128); + SecretKey secKey = generator.generateKey(); + return secKey.getEncoded(); + } + + public static byte[] encrypt(byte[] content, SecretKey key) { + Cipher aesCipher = null; + try { + aesCipher = Cipher.getInstance("AES"); + aesCipher.init(Cipher.ENCRYPT_MODE, key); + return aesCipher.doFinal(content); + } catch (NoSuchAlgorithmException e) { + System.err.println("非可用算法"); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + System.err.println("明文长度不正确"); + } catch (InvalidKeyException e) { + System.err.println("秘钥已损坏"); + } + return null; + } + + public static byte[] decrypt(byte[] content, SecretKey key) { + Cipher aesCipher = null; + try { + aesCipher = Cipher.getInstance("AES"); + aesCipher.init(Cipher.DECRYPT_MODE, key); + return aesCipher.doFinal(content); + } catch (NoSuchAlgorithmException e) { + System.err.println("非可用算法"); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + System.err.println("密文长度不正确"); + } catch (InvalidKeyException e) { + System.err.println("秘钥已损坏"); + } + return null; + } +} diff --git a/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/DecryptRunner.java b/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/DecryptRunner.java new file mode 100644 index 00000000..d8611b54 --- /dev/null +++ b/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/DecryptRunner.java @@ -0,0 +1,103 @@ +package com.tony.autojs.tool.encrypt; + + +import android.app.Activity; +import android.content.Context; + +import com.stardust.autojs.execution.ExecutionConfig; +import com.stardust.autojs.runtime.api.Engines; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; + +import javax.crypto.SecretKey; + +public class DecryptRunner { + + private EnginesDelegate engines; + private Context context; + private Activity activity; + private boolean isPro; + private SecretKey secretKey; + + /** + * @param engines + * @param context + * @param activity + */ + public DecryptRunner(Engines engines, Context context, Activity activity) { + this.engines = new EnginesDelegate(engines); + this.context = context; + this.activity = activity; + this.isPro = this.context.getPackageName().equals("org.autojs.autojspro"); + } + + public void executeDecryptScriptString(String script, String workingDir) { + ExecutionConfig config = new ExecutionConfig(); + config.setWorkingDirectory(workingDir); + if (!this.isPro) { + engines.execScript("tmp", script, config); + } else { + engines.execScript(activity, "tmp", script, config); + } + } + + public void decryptAndRun(String filePath, String workingDir) { + executeDecryptScriptString(decryptData(filePath), workingDir); + } + + public void encryptScript(String originFilePath, String destFilePath) { + File originFile = new File(originFilePath); + if (originFile.exists()) { + try ( + FileInputStream fileInputStream = new FileInputStream(originFile); + FileOutputStream fileOutputStream = new FileOutputStream(destFilePath); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ) { + byte[] buffer = new byte[8192]; + int len = 0; + while ((len = fileInputStream.read(buffer)) > 0) { + byteArrayOutputStream.write(buffer, 0, len); + } + byte[] bytes = AESEncrypt.encrypt(byteArrayOutputStream.toByteArray(), secretKey); + if (bytes != null) { + fileOutputStream.write(bytes); + } + } catch (Exception e) { + + } + } + } + + private String decryptData(String filePath) { + try ( + FileInputStream fileInputStream = new FileInputStream(filePath); + BufferedInputStream reader = new BufferedInputStream(fileInputStream); + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ) { + byte[] buffer = new byte[8192]; + int length = -1; + while ((length = reader.read(buffer)) > 0) { + byteArrayOutputStream.write(buffer, 0, length); + } + byte[] bytes = AESEncrypt.decrypt(byteArrayOutputStream.toByteArray(), secretKey); + if (bytes != null) { + return new String(bytes, StandardCharsets.UTF_8); + } + } catch (Exception e) { + } + return null; + } + + public SecretKey getSecretKey() { + return secretKey; + } + + public void setSecretKey(SecretKey secretKey) { + this.secretKey = secretKey; + } +} diff --git a/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/EnginesDelegate.java b/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/EnginesDelegate.java new file mode 100644 index 00000000..725c6128 --- /dev/null +++ b/autojs-tool-encrypt/src/main/java/com/tony/autojs/tool/encrypt/EnginesDelegate.java @@ -0,0 +1,29 @@ +package com.tony.autojs.tool.encrypt; + +import android.app.Activity; + +import com.stardust.autojs.execution.ExecutionConfig; +import com.stardust.autojs.execution.ScriptExecution; +import com.stardust.autojs.runtime.api.Engines; + +import java.lang.reflect.Method; + +public final class EnginesDelegate { + private Engines originEngine; + + public EnginesDelegate(Engines originEngine) { + this.originEngine = originEngine; + } + + public ScriptExecution execScript(String name, String script, ExecutionConfig config) { + return originEngine.execScript(name, script, config); + } + + public void execScript(Activity activity, String name, String script, ExecutionConfig config) { + try { + Method execScriptMethod = originEngine.getClass().getMethod("execScript", Activity.class, String.class, String.class, ExecutionConfig.class); + execScriptMethod.invoke(originEngine, activity, name, script, config); + } catch (Exception e) { + } + } +} From 8efb74eae00e69ee0651ae0e1ed01dede12f5dc4 Mon Sep 17 00:00:00 2001 From: TonyJiangWJ Date: Sat, 26 Dec 2020 12:19:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/stardust/autojs/runtime/ScriptRuntime.java | 1 + .../main/java/com/stardust/autojs/runtime/api/Images.java | 5 ++++- settings.gradle | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java b/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java index 6158c18d..679a9205 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/ScriptRuntime.java @@ -424,6 +424,7 @@ public class ScriptRuntime { }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ignoresException(images::releaseScreenCapturer); + ignoresException(images::recycle); } ignoresException(sensors::unregisterAll); ignoresException(timers::recycle); diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java index 322a8238..e1e71d42 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Images.java @@ -267,7 +267,6 @@ public class Images { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mScreenCapturer != null) { mScreenCapturer.release(); } - mScriptRuntime = null; } public Point findImage(ImageWrapper image, ImageWrapper template) { @@ -366,4 +365,8 @@ public class Images { } } + + public void recycle() { + mScriptRuntime = null; + } } diff --git a/settings.gradle b/settings.gradle index f65eecc8..6c164af4 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ include ':app', ':automator', ':common', ':autojs', ':inrt', ':autojs-tool-color-center' include ':autojs-tool-download' include ':autojs-tool-webview' +include ':autojs-tool-encrypt'