1. 准备工作
- aspose官网下载
aspose-words-19.7-jdk17.jar
- 下载JByteMod(或者其他反编译工具)—— github地址,解压zip, 进入目录cmd下执行
java -jar JByteMod-1.8.2.jar
2. 分析jar包license验证代码
将
aspose-words-19.7-jdk17.jar
拖进JByteMod,找到Lincense.class
补充:另一个setLicense()方法调用的zzX()最终也是调用zzV(),所以zzV()是关键。
补充2:上图写着两个Deprecated不用看,这个也关注下,zzZKN的zzZmz()方法。
找到上图提到的关键——类zzZKN的zzV方法。
进一步看下zzV方法最后设置了什么(关键)
补充2提到的zzZmz()方法,看下怎么让它可以通过。
去到上图提到关键地方——类zzYQ1的zzYBu()
3. 分析完成后,开始动手修改字节码
这边选择一个maven项目(这个随意),引入依赖
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.25.0-GA</version>
</dependency>
新建一个类,main方法写相应代码——修改字节码
// jar包地址
ClassPool.getDefault().insertClassPath("C:/Users/gavin/Desktop/aspose-words-19.7-jdk17.jar");
// 修改zzZKN的zzV方法,修改三个变量
CtClass zzZKNClass = ClassPool.getDefault().getCtClass("com.aspose.words.zzZKN");
CtMethod zzV = zzZKNClass.getDeclaredMethod("zzV");
zzV.setBody("{this.zzYNK = new java.util.Date(Long.MAX_VALUE);this.zzYNJ = 1;zzYNI = this;}");
zzZKNClass.writeFile();
// 修改zzYQ1的zzYBu方法让其返回256.
CtClass zzYQ1Class = ClassPool.getDefault().getCtClass("com.aspose.words.zzYQ1");
CtMethod zzYBuMethod = zzYQ1Class.getDeclaredMethod("zzYBu");
zzYBuMethod.setBody("{return 256;}");
zzYQ1Class.writeFile();
run main()
生成两个字节码文件zzZKN.class和zzYBu.class(src同级目录会生成com,往下就是/aspose/words)
用解压软件解压jar,进入目录替换两个class文件,然后如下图——删除签名文件
重新压缩成jar
// 压缩当前目前所有文件到 jar
jar cvfM0 aspose-words-19.7-jdk17.jar *
引用jar到pom.xml,测试word转pdf
public static boolean getLicense() {
boolean result = false;
try {
//Test.class.getClassLoader().getResourceAsStream("license.xml");
//InputStream is = main.class.getClassLoader().getResourceAsStream("license.xml");
String license =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>111</Signature>\n" +
"</License>";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
License asposeLic = new License();
asposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void word2pdf(String wordUrl, String pdfUrl) throws Exception {
if (!getLicense()) {
return;
}
try (InputStream is = new FileInputStream(wordUrl);) {
Document doc = new Document(is);
doc.save(pdfUrl);
}
}
原文链接:https://blog.csdn.net/qq_42834405/article/details/98208561