Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.moz1q1:stringfog:buriedpoint_3.0.2'
}
dependencies {
implementation("com.github.moz1q1:stringfog:buriedpoint_3.0.2")
}
<dependency>
<groupId>com.github.moz1q1</groupId>
<artifactId>stringfog</artifactId>
<version>buriedpoint_3.0.2</version>
</dependency>
libraryDependencies += "com.github.moz1q1" % "stringfog" % "buriedpoint_3.0.2"
:dependencies [[com.github.moz1q1/stringfog "buriedpoint_3.0.2"]]
一款自动对dex/aar/jar文件中的字符串进行加密Android插件工具,正如名字所言,给字符串加上一层雾霭,使人难以窥视其真面目。
<br>
String a = "This is a string!";
String a = StringFog.decrypt(new byte[]{-113, 71...}, new byte[]{-23, 53});
decrypt: new byte[]{-113, 71...} => "This is a string!"
StringFog和混淆完全不冲突,也不需要配置反混淆,实际上StringFog配上混淆效果会更好!
由于开发了gradle插件,所以在集成时非常简单,不会影响到打包的配置。插件已经上传到MavenCentral,直接引用依赖就可以。 jcenter已经废弃,3.0+版本取消发布
buildscript {
repositories {
mavenCentral()
}
dependencies {
...
classpath 'com.github.megatronking.stringfog:gradle-plugin:5.0.0'
// 选用加解密算法库,默认实现了xor算法,也可以使用自己的加解密库。
classpath 'com.github.megatronking.stringfog:xor:5.0.0'
}
}
apply plugin: 'stringfog'
// 导入RandomKeyGenerator类,如果不使用RandomKeyGenerator,可以删除此行
import com.github.megatronking.stringfog.plugin.kg.RandomKeyGenerator
stringfog {
// 必要:加解密库的实现类路径,需和上面配置的加解密算法库一致。
implementation 'com.github.megatronking.stringfog.xor.StringFogImpl'
// 可选:加密开关,默认开启。
enable true
// 可选:指定需加密的代码包路径,可配置多个,未指定将默认全部加密。
fogPackages = ['com.xxx.xxx']
// 可选(3.0版本新增):指定密钥生成器,默认使用长度8的随机密钥(每个字符串均有不同随机密钥),
// 也可以指定一个固定的密钥:HardCodeKeyGenerator("This is a key")
kg new RandomKeyGenerator()
// 可选(4.0版本新增):用于控制字符串加密后在字节码中的存在形式, 默认为base64,
// 也可以使用text或者bytes
mode base64
}
kts中配置参考
plugins {
//...lib or application
id("stringfog")
}
apply(plugin = "stringfog")
configure<StringFogExtension> {
implementation = "com.github.megatronking.stringfog.xor.StringFogImpl"// 必要:加解密库的实现类路径,需和上面配置的加解密算法库一致。
enable = true// 可选:加密开关,默认开启。
//fogPackages = arrayOf("com.xxx.xxx")// 可选:指定需加密的代码包路径,可配置多个,未指定将默认全部加密。
kg = com.github.megatronking.stringfog.plugin.kg.RandomKeyGenerator()
mode = com.github.megatronking.stringfog.plugin.StringFogMode.bytes//base64或者bytes
}
dependencies {
...
// 这里要和上面选用的加解密算法库一致,用于运行时解密。
compile 'com.github.megatronking.stringfog:xor:5.0.0'
}
从AGP 8.0开始,默认不生成BuildConfig,但是StringFog依赖此配置,请注意加上下面的配置。
android {
// 注意请加上此配置
buildFeatures {
buildConfig = true
}
...
}
如果开发者有不需要自动加密的类,可以使用注解StringFogIgnore来忽略:
@StringFogIgnore
public class Test {
...
}
实现IStringFog接口,参考stringfog-ext目录下面的xor算法实现。 注意某些算法在不同平台上会有差异,可能出现在运行时无法正确解密的问题。如何集成请参考下方范例!
public final class StringFogImpl implements IStringFog {
@Override
public byte[] encrypt(String data, byte[] key) {
// 自定义加密
}
@Override
public String decrypt(byte[] data, byte[] key) {
// 自定义解密
}
@Override
public boolean shouldFog(String data) {
// 控制指定字符串是否加密
// 建议过滤掉不重要或者过长的字符串
return true;
}
}
实现IKeyGenerator接口,参考RandomKeyGenerator的实现。
加解密的字符串明文和暗文会自动生成mapping映射文件,位于outputs/mapping/stringfog.txt。
Copyright (C) 2022, Megatron King
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.