java jar包代码使用ClassFinal加密

地址:https://github.com/roseboy/classfinal

下载和使用方式都有

java -jar classfinal-fatjar-1.2.1.jar -file server-cdzh-2.jar -packages com.ctsi -cfgfiles application-dev.yml,wechat.properties -pwd 123123 -Y

也可以用的maven插件模式

<plugin>
                <!-- https://gitee.com/roseboy/classfinal -->
                <groupId>net.roseboy</groupId>
                <artifactId>classfinal-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <!--                    无密码模式-->
                    <password>123123</password><!--加密打包之后pom.xml会被删除,不用担心在jar包里找到此密码-->
                    <packages>com.ctsi</packages>
                    <cfgfiles>application-dev.yml,wxpay.properties</cfgfiles>
                    <excludes>org.spring</excludes>
                    <libjars></libjars>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>classFinal</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

注,我在加密配置文件的时候不成功,因为我的配置文件没有直接放在resources资源目录下,所以把配置文件拿出来才成功的

启动

java -javaagent:server-cdzh-3-encrypted.jar='-pwd' -jar server-cdzh-3-encrypted.jar -spring.profiles.active=dev

 启动的时候要输入密码

ClassFinal是一款Java class文件安全加密工具,支持直接加密jar包或war包,无需修改任何项目代码,兼容spring-framework,可避免源码泄漏或字节码被反编译。 项目模块说明: classfinal-core:ClassFinalde的核心模块,几乎所有加密代码都在这里; classfinal-fatjarClassFinal打包成独立运行的jar包classfinal-maven-plugin:ClassFinal加密的maven插件; 功能特性: 无需修改原项目代码,只要把编译好的jar/war包用本工具加密即可。 运行加密项目时,无需求修改tomcat,spring等源代码。 支持普通jar包、springboot jar包以及普通java web项目编译的war包。 支持spring framework、swagger等需要在启动过程中扫描注解或生成字节码的框架。 支持maven插件,添加插件后在打包过程中自动加密。 支持加密WEB-INF/lib或BOOT-INF/lib下的依赖jar包。 环境依赖: JDK 1.8 + 本工具使用AES算法加密class文件,密码是保证不被破解的关键,请保存好密码,请勿泄漏。 密码一旦忘记,项目不可启动且无法恢复,请牢记密码。 本工具加密后,原始的class文件并不会完全被加密,只是方法体被清空,保留方法参数、注解等信息,这是为了兼容spring,swagger等扫描注解的框架; 方法体被清空后,反编译者只能看到方法名和注解,看不到方法的具体内容;当classclassloader加载时,真正的方法体会被解密注入。
Java提供了很多加密算法,可以用于加密和解密jar文件。常用的加密算法有AES、DES、RSA等。 下面是一个使用AES算法对jar文件进行加密和解密的示例代码: ```java import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class JarEncryptor { private static final String ALGO = "AES"; private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding"; private static final byte[] IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; public static void encrypt(String key, File inputFile, File outputFile) throws Exception { doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile); } public static void decrypt(String key, File inputFile, File outputFile) throws Exception { doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile); } private static void doCrypto(int cipherMode, String key, File inputFile, File outputFile) throws Exception { Key secretKey = new SecretKeySpec(key.getBytes(), ALGO); Cipher cipher = Cipher.getInstance(TRANSFORMATION); IvParameterSpec iv = new IvParameterSpec(IV); cipher.init(cipherMode, secretKey, iv); FileInputStream inputStream = new FileInputStream(inputFile); byte[] inputBytes = new byte[(int) inputFile.length()]; inputStream.read(inputBytes); byte[] outputBytes = cipher.doFinal(inputBytes); FileOutputStream outputStream = new FileOutputStream(outputFile); outputStream.write(outputBytes); inputStream.close(); outputStream.close(); } } ``` 在上面的代码中,我们使用AES算法和CBC模式对jar文件进行加密和解密。加密和解密的关键是密钥,我们需要提供一个密钥来进行加密和解密操作。 以下是使用示例: ```java public static void main(String[] args) throws Exception { String key = "mysecretkey"; File inputFile = new File("test.jar"); File encryptedFile = new File("test_encrypted.jar"); File decryptedFile = new File("test_decrypted.jar"); JarEncryptor.encrypt(key, inputFile, encryptedFile); JarEncryptor.decrypt(key, encryptedFile, decryptedFile); } ``` 在上面的示例中,我们使用“mysecretkey”作为密钥对“test.jar”进行加密,然后再对加密后的文件进行解密,最终得到的文件名为“test_decrypted.jar”。 需要注意的是,加密和解密过程中需要使用相同的密钥,否则无法正确解密。同时,密钥应该足够复杂和安全,以保证加密后的文件不会被破解。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值