大家都知道Java 编译为class 后可以通过反编译工具再编译为Java 文件.
常用的反编译工具:idea,jd,jad
如果,不希望别人看到自己人源代码,要怎么做?
在JavaScript 中可以通过压缩代码,代码混淆来避免.Java 里也有类似的工具.
allatori
用于将已有代码混淆,防止反编译.
工作原理:
读取 jar 文件,对其代码按照XML 中的配置进行重写.如:字符串替换,代码改写为内部类等等
maven方式代码:
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-and-filter-allatori-config</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>${basedir}/lib</directory>
<includes>
<include>allatori.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Running Allatori -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>run-allatori</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Xms128m</argument>
<argument>-Xmx512m</argument>
<argument>-jar</argument>
<!-- Copy allatori.jar to 'allatori' directory to use the commented line -->
<argument>${basedir}/lib/allatori.jar</argument>
<!-- <argument>${basedir}/allatori/allatori.jar</argument> -->
<argument>${basedir}/target/allatori.xml</argument>
</arguments>
</configuration>
</plugin>
配置文件:
<config>
<input>
<jar in="my-allatori.jar" out="my-allatori-encrypt.jar"/>
</input>
<keep-names>
<class access="protected+">
<field access="protected+"/>
<method access="protected+"/>
</class>
</keep-names>
<property name="log-file" value="log.xml"/>
</config>
混淆前代码:
混淆后代码:
注意事项:
- 重写会改变类名,可以在配置文件中控制是否保持类名,以及重写完类名是否影响代码运行.
- 需要测试,保证代码的重写不会影响使用