From 32508a05550d74a4c113aa15525a19605abdfd6a Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Wed, 3 Jul 2019 23:07:27 +0800 Subject: [PATCH 01/37] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=97=B6=E7=AD=BE?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 21eb14a2e1b887388b3d891350d93c4587c3af5a) --- src/main/java/io/xjar/XDigest.java | 17 ++++++++ .../java/io/xjar/XDigestedInputStream.java | 35 +++++++++++++++++ .../java/io/xjar/XDigestedOutputStream.java | 32 +++++++++++++++ src/main/java/io/xjar/digest/XMD2Digest.java | 39 +++++++++++++++++++ src/main/java/io/xjar/digest/XMD5Digest.java | 39 +++++++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 src/main/java/io/xjar/XDigest.java create mode 100644 src/main/java/io/xjar/XDigestedInputStream.java create mode 100644 src/main/java/io/xjar/XDigestedOutputStream.java create mode 100644 src/main/java/io/xjar/digest/XMD2Digest.java create mode 100644 src/main/java/io/xjar/digest/XMD5Digest.java diff --git a/src/main/java/io/xjar/XDigest.java b/src/main/java/io/xjar/XDigest.java new file mode 100644 index 0000000..943f7a2 --- /dev/null +++ b/src/main/java/io/xjar/XDigest.java @@ -0,0 +1,17 @@ +package io.xjar; + +/** + * 摘要算法 + * + * @author Payne 646742615@qq.com + * 2019/7/3 22:25 + */ +public interface XDigest { + + void digest(byte[] buf, int off, int len); + + byte[] finish(); + + void resume(); + +} diff --git a/src/main/java/io/xjar/XDigestedInputStream.java b/src/main/java/io/xjar/XDigestedInputStream.java new file mode 100644 index 0000000..50b9c95 --- /dev/null +++ b/src/main/java/io/xjar/XDigestedInputStream.java @@ -0,0 +1,35 @@ +package io.xjar; + +import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; + +import java.io.IOException; +import java.io.InputStream; + +/** + * 摘要计算的输入流 + * + * @author Payne 646742615@qq.com + * 2019/7/3 22:31 + */ +public class XDigestedInputStream extends JarArchiveInputStream { + private final XDigest xDigest; + + public XDigestedInputStream(InputStream in, XDigest xDigest) { + super(in); + this.xDigest = xDigest; + } + + public XDigestedInputStream(InputStream in, String encoding, XDigest xDigest) { + super(in, encoding); + this.xDigest = xDigest; + } + + @Override + public int read(byte[] buffer, int offset, int length) throws IOException { + length = super.read(buffer, offset, length); + if (length > 0) { + xDigest.digest(buffer, offset, length); + } + return length; + } +} diff --git a/src/main/java/io/xjar/XDigestedOutputStream.java b/src/main/java/io/xjar/XDigestedOutputStream.java new file mode 100644 index 0000000..6121c2f --- /dev/null +++ b/src/main/java/io/xjar/XDigestedOutputStream.java @@ -0,0 +1,32 @@ +package io.xjar; + +import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * 摘要计算的输出流 + * + * @author Payne 646742615@qq.com + * 2019/7/3 22:30 + */ +public class XDigestedOutputStream extends JarArchiveOutputStream { + private final XDigest xDigest; + + public XDigestedOutputStream(OutputStream out, XDigest xDigest) { + super(out); + this.xDigest = xDigest; + } + + public XDigestedOutputStream(OutputStream out, String encoding, XDigest xDigest) { + super(out, encoding); + this.xDigest = xDigest; + } + + @Override + public void write(byte[] buffer, int offset, int length) throws IOException { + xDigest.digest(buffer, offset, length); + super.write(buffer, offset, length); + } +} diff --git a/src/main/java/io/xjar/digest/XMD2Digest.java b/src/main/java/io/xjar/digest/XMD2Digest.java new file mode 100644 index 0000000..3ac4976 --- /dev/null +++ b/src/main/java/io/xjar/digest/XMD2Digest.java @@ -0,0 +1,39 @@ +package io.xjar.digest; + +import io.xjar.XDigest; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * MD2摘要算法 + * + * @author Payne 646742615@qq.com + * 2019/7/3 22:27 + */ +public class XMD2Digest implements XDigest { + private final MessageDigest md; + + public XMD2Digest() { + try { + md = MessageDigest.getInstance("MD2"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + + @Override + public void digest(byte[] buf, int off, int len) { + md.update(buf, off, len); + } + + @Override + public byte[] finish() { + return md.digest(); + } + + @Override + public void resume() { + md.reset(); + } +} diff --git a/src/main/java/io/xjar/digest/XMD5Digest.java b/src/main/java/io/xjar/digest/XMD5Digest.java new file mode 100644 index 0000000..fe16cd5 --- /dev/null +++ b/src/main/java/io/xjar/digest/XMD5Digest.java @@ -0,0 +1,39 @@ +package io.xjar.digest; + +import io.xjar.XDigest; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * MD5摘要算法 + * + * @author Payne 646742615@qq.com + * 2019/7/3 22:27 + */ +public class XMD5Digest implements XDigest { + private final MessageDigest md; + + public XMD5Digest() { + try { + md = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); + } + } + + @Override + public void digest(byte[] buf, int off, int len) { + md.update(buf, off, len); + } + + @Override + public byte[] finish() { + return md.digest(); + } + + @Override + public void resume() { + md.reset(); + } +} From b2711d817919757d506913386b5f6490c7137b47 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 4 Jul 2019 14:34:23 +0800 Subject: [PATCH 02/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=89=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=B7=AF=E5=BE=84=E5=8A=A0=E8=A7=A3=E5=AF=86=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XDecryptor.java | 10 ++++++++++ src/main/java/io/xjar/XEncryptor.java | 10 ++++++++++ src/main/java/io/xjar/XJdkDecryptor.java | 10 ++++++++-- src/main/java/io/xjar/XJdkEncryptor.java | 10 ++++++++-- src/main/java/io/xjar/XNopDecryptor.java | 5 +++++ src/main/java/io/xjar/XNopEncryptor.java | 5 +++++ src/main/java/io/xjar/XWrappedDecryptor.java | 5 +++++ src/main/java/io/xjar/XWrappedEncryptor.java | 5 +++++ 8 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/xjar/XDecryptor.java b/src/main/java/io/xjar/XDecryptor.java index 256d685..5f859dc 100644 --- a/src/main/java/io/xjar/XDecryptor.java +++ b/src/main/java/io/xjar/XDecryptor.java @@ -15,6 +15,16 @@ */ public interface XDecryptor { + /** + * 解密,将目标文件解密输出至目标文件。 + * + * @param key 密钥 + * @param src 源文件 + * @param dest 目标文件 + * @throws IOException I/O 异常 + */ + void decrypt(XKey key, String src, String dest) throws IOException; + /** * 解密,将目标文件解密输出至目标文件。 * diff --git a/src/main/java/io/xjar/XEncryptor.java b/src/main/java/io/xjar/XEncryptor.java index ef50ddf..9867e59 100644 --- a/src/main/java/io/xjar/XEncryptor.java +++ b/src/main/java/io/xjar/XEncryptor.java @@ -15,6 +15,16 @@ */ public interface XEncryptor { + /** + * 加密,将目标文件加密输出至目标文件。 + * + * @param key 密钥 + * @param src 源文件 + * @param dest 目标文件 + * @throws IOException I/O 异常 + */ + void encrypt(XKey key, String src, String dest) throws IOException; + /** * 加密,将目标文件加密输出至目标文件。 * diff --git a/src/main/java/io/xjar/XJdkDecryptor.java b/src/main/java/io/xjar/XJdkDecryptor.java index 4e06169..6a85902 100644 --- a/src/main/java/io/xjar/XJdkDecryptor.java +++ b/src/main/java/io/xjar/XJdkDecryptor.java @@ -21,10 +21,16 @@ public XJdkDecryptor(String algorithm) { this.algorithm = algorithm; } + @Override + public void decrypt(XKey key, String src, String dest) throws IOException { + decrypt(key, new File(src), new File(dest)); + } + @Override public void decrypt(XKey key, File src, File dest) throws IOException { - if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) { - throw new IOException("could not make directory: " + dest.getParentFile()); + File dir = dest.getParentFile(); + if (!dir.exists() && !dir.mkdirs() && !dir.exists()) { + throw new IOException("could not make directory: " + dir); } try ( InputStream in = new FileInputStream(src); diff --git a/src/main/java/io/xjar/XJdkEncryptor.java b/src/main/java/io/xjar/XJdkEncryptor.java index 602e524..861480a 100644 --- a/src/main/java/io/xjar/XJdkEncryptor.java +++ b/src/main/java/io/xjar/XJdkEncryptor.java @@ -21,10 +21,16 @@ public XJdkEncryptor(String algorithm) { this.algorithm = algorithm; } + @Override + public void encrypt(XKey key, String src, String dest) throws IOException { + encrypt(key, new File(src), new File(dest)); + } + @Override public void encrypt(XKey key, File src, File dest) throws IOException { - if (!dest.getParentFile().exists() && !dest.getParentFile().mkdirs()) { - throw new IOException("could not make directory: " + dest.getParentFile()); + File dir = dest.getParentFile(); + if (!dir.exists() && !dir.mkdirs() && !dir.exists()) { + throw new IOException("could not make directory: " + dir); } try ( InputStream in = new FileInputStream(src); diff --git a/src/main/java/io/xjar/XNopDecryptor.java b/src/main/java/io/xjar/XNopDecryptor.java index 52a158a..1b5ef5d 100644 --- a/src/main/java/io/xjar/XNopDecryptor.java +++ b/src/main/java/io/xjar/XNopDecryptor.java @@ -12,6 +12,11 @@ */ public class XNopDecryptor implements XDecryptor { + @Override + public void decrypt(XKey key, String src, String dest) throws IOException { + decrypt(key, new File(src), new File(dest)); + } + @Override public void decrypt(XKey key, File src, File dest) throws IOException { try ( diff --git a/src/main/java/io/xjar/XNopEncryptor.java b/src/main/java/io/xjar/XNopEncryptor.java index 0ec8fd7..6e3fd32 100644 --- a/src/main/java/io/xjar/XNopEncryptor.java +++ b/src/main/java/io/xjar/XNopEncryptor.java @@ -12,6 +12,11 @@ */ public class XNopEncryptor implements XEncryptor { + @Override + public void encrypt(XKey key, String src, String dest) throws IOException { + encrypt(key, new File(src), new File(dest)); + } + @Override public void encrypt(XKey key, File src, File dest) throws IOException { try ( diff --git a/src/main/java/io/xjar/XWrappedDecryptor.java b/src/main/java/io/xjar/XWrappedDecryptor.java index 0537f01..a045c85 100644 --- a/src/main/java/io/xjar/XWrappedDecryptor.java +++ b/src/main/java/io/xjar/XWrappedDecryptor.java @@ -20,6 +20,11 @@ protected XWrappedDecryptor(XDecryptor xDecryptor) { this.xDecryptor = xDecryptor; } + @Override + public void decrypt(XKey key, String src, String dest) throws IOException { + xDecryptor.decrypt(key, src, dest); + } + @Override public void decrypt(XKey key, File src, File dest) throws IOException { xDecryptor.decrypt(key, src, dest); diff --git a/src/main/java/io/xjar/XWrappedEncryptor.java b/src/main/java/io/xjar/XWrappedEncryptor.java index 5e2ebc1..cdbfd46 100644 --- a/src/main/java/io/xjar/XWrappedEncryptor.java +++ b/src/main/java/io/xjar/XWrappedEncryptor.java @@ -20,6 +20,11 @@ protected XWrappedEncryptor(XEncryptor xEncryptor) { this.xEncryptor = xEncryptor; } + @Override + public void encrypt(XKey key, String src, String dest) throws IOException { + xEncryptor.encrypt(key, src, dest); + } + @Override public void encrypt(XKey key, File src, File dest) throws IOException { xEncryptor.encrypt(key, src, dest); From d59538c30d3fadcae6eb00720892601ea8629959 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 4 Jul 2019 15:35:40 +0800 Subject: [PATCH 03/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XEntryDecryptor.java | 9 ++++++++ src/main/java/io/xjar/XEntryEncryptor.java | 9 ++++++++ src/main/java/io/xjar/XWrappedDecryptor.java | 11 ++++++++++ src/main/java/io/xjar/XWrappedEncryptor.java | 11 ++++++++++ src/main/java/io/xjar/jar/XJarDecryptor.java | 21 ++++++++++++++---- src/main/java/io/xjar/jar/XJarEncryptor.java | 23 ++++++++++++++++++++ 6 files changed, 80 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/xjar/XEntryDecryptor.java b/src/main/java/io/xjar/XEntryDecryptor.java index b23244b..40c2571 100644 --- a/src/main/java/io/xjar/XEntryDecryptor.java +++ b/src/main/java/io/xjar/XEntryDecryptor.java @@ -23,4 +23,13 @@ protected XEntryDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { public boolean filtrate(E entry) { return filter == null || filter.filtrate(entry); } + + protected static abstract class XEntryDecryptorBuilder, B extends XEntryDecryptorBuilder> extends XWrappedDecryptorBuilder { + protected XEntryFilter filter; + + public B filter(XEntryFilter filter) { + this.filter = filter; + return (B) this; + } + } } diff --git a/src/main/java/io/xjar/XEntryEncryptor.java b/src/main/java/io/xjar/XEntryEncryptor.java index 699ea99..1950ab5 100644 --- a/src/main/java/io/xjar/XEntryEncryptor.java +++ b/src/main/java/io/xjar/XEntryEncryptor.java @@ -23,4 +23,13 @@ protected XEntryEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { public boolean filtrate(E entry) { return filter == null || filter.filtrate(entry); } + + protected static abstract class XEntryEncryptorBuilder, B extends XEntryEncryptorBuilder> extends XWrappedEncryptorBuilder { + protected XEntryFilter filter; + + public B filter(XEntryFilter filter) { + this.filter = filter; + return (B) this; + } + } } diff --git a/src/main/java/io/xjar/XWrappedDecryptor.java b/src/main/java/io/xjar/XWrappedDecryptor.java index a045c85..54f50f5 100644 --- a/src/main/java/io/xjar/XWrappedDecryptor.java +++ b/src/main/java/io/xjar/XWrappedDecryptor.java @@ -44,4 +44,15 @@ public InputStream decrypt(XKey key, InputStream in) throws IOException { public OutputStream decrypt(XKey key, OutputStream out) throws IOException { return xDecryptor.decrypt(key, out); } + + protected static abstract class XWrappedDecryptorBuilder> { + protected XDecryptor decryptor; + + public B decryptor(XDecryptor decryptor) { + this.decryptor = decryptor; + return (B) this; + } + + public abstract T build(); + } } diff --git a/src/main/java/io/xjar/XWrappedEncryptor.java b/src/main/java/io/xjar/XWrappedEncryptor.java index cdbfd46..2f11199 100644 --- a/src/main/java/io/xjar/XWrappedEncryptor.java +++ b/src/main/java/io/xjar/XWrappedEncryptor.java @@ -44,4 +44,15 @@ public InputStream encrypt(XKey key, InputStream in) throws IOException { public OutputStream encrypt(XKey key, OutputStream out) throws IOException { return xEncryptor.encrypt(key, out); } + + protected static abstract class XWrappedEncryptorBuilder> { + protected XEncryptor encryptor; + + public B encryptor(XEncryptor encryptor) { + this.encryptor = encryptor; + return (B) this; + } + + public abstract T build(); + } } diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index 0a5803c..e8b7804 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -20,16 +20,16 @@ public class XJarDecryptor extends XEntryDecryptor implements XDecryptor, XConstants { private final int level; - public XJarDecryptor(XDecryptor xEncryptor) { - this(xEncryptor, new XJarAllEntryFilter()); + public XJarDecryptor(XDecryptor xDecryptor) { + this(xDecryptor, new XJarAllEntryFilter()); } public XJarDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { this(xDecryptor, Deflater.DEFLATED, filter); } - public XJarDecryptor(XDecryptor xEncryptor, int level) { - this(xEncryptor, level, new XJarAllEntryFilter()); + public XJarDecryptor(XDecryptor xDecryptor, int level) { + this(xDecryptor, level, new XJarAllEntryFilter()); } public XJarDecryptor(XDecryptor xDecryptor, int level, XEntryFilter filter) { @@ -102,4 +102,17 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti } } + public static class XJarDecryptorBuilder extends XEntryDecryptorBuilder { + private int level; + + public XJarDecryptor.XJarDecryptorBuilder level(int level) { + this.level = level; + return this; + } + + @Override + public XJarDecryptor build() { + return new XJarDecryptor(decryptor, level, filter); + } + } } diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index c87075e..402c656 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -142,4 +142,27 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti } } + public static XJarEncryptorBuilder builder() { + return new XJarEncryptorBuilder(); + } + + public static class XJarEncryptorBuilder extends XEntryEncryptorBuilder { + private int level; + private int mode; + + public XJarEncryptorBuilder level(int level) { + this.level = level; + return this; + } + + public XJarEncryptorBuilder mode(int mode) { + this.mode = mode; + return this; + } + + @Override + public XJarEncryptor build() { + return new XJarEncryptor(encryptor, level, mode, filter); + } + } } From 810437ee6fc40bb6c48d08e2752a15b42de1fd18 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 4 Jul 2019 15:36:16 +0800 Subject: [PATCH 04/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/jar/XJarDecryptor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index e8b7804..a7b59a3 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -105,7 +105,7 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti public static class XJarDecryptorBuilder extends XEntryDecryptorBuilder { private int level; - public XJarDecryptor.XJarDecryptorBuilder level(int level) { + public XJarDecryptorBuilder level(int level) { this.level = level; return this; } From fb36507031f179b5a04a56499779754f7d0a254c Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 4 Jul 2019 16:38:44 +0800 Subject: [PATCH 05/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 7 ++++ src/main/java/io/xjar/XSmtDecryptor.java | 42 ++++++++++++++++++++ src/main/java/io/xjar/XSmtEncryptor.java | 42 ++++++++++++++++++++ src/main/java/io/xjar/jar/XJarDecryptor.java | 30 ++++++++++---- src/main/java/io/xjar/jar/XJarEncryptor.java | 14 ++++++- src/test/java/test/io/xjar/XJarTest.java | 24 +++++++++++ 6 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 src/main/java/io/xjar/XSmtDecryptor.java create mode 100644 src/main/java/io/xjar/XSmtEncryptor.java create mode 100644 src/test/java/test/io/xjar/XJarTest.java diff --git a/pom.xml b/pom.xml index dac3df3..4c5f579 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,13 @@ 2.0.1.RELEASE provided + + + junit + junit + 4.12 + test + diff --git a/src/main/java/io/xjar/XSmtDecryptor.java b/src/main/java/io/xjar/XSmtDecryptor.java new file mode 100644 index 0000000..4e58d5d --- /dev/null +++ b/src/main/java/io/xjar/XSmtDecryptor.java @@ -0,0 +1,42 @@ +package io.xjar; + +import io.xjar.key.XKey; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * 智能解密器 + * + * @author Payne 646742615@qq.com + * 2019/7/4 16:10 + */ +public class XSmtDecryptor implements XDecryptor { + + @Override + public void decrypt(XKey key, String src, String dest) throws IOException { + new XJdkDecryptor(key.getAlgorithm()).decrypt(key, src, dest); + } + + @Override + public void decrypt(XKey key, File src, File dest) throws IOException { + new XJdkDecryptor(key.getAlgorithm()).decrypt(key, src, dest); + } + + @Override + public void decrypt(XKey key, InputStream in, OutputStream out) throws IOException { + new XJdkDecryptor(key.getAlgorithm()).decrypt(key, in, out); + } + + @Override + public InputStream decrypt(XKey key, InputStream in) throws IOException { + return new XJdkDecryptor(key.getAlgorithm()).decrypt(key, in); + } + + @Override + public OutputStream decrypt(XKey key, OutputStream out) throws IOException { + return new XJdkDecryptor(key.getAlgorithm()).decrypt(key, out); + } +} diff --git a/src/main/java/io/xjar/XSmtEncryptor.java b/src/main/java/io/xjar/XSmtEncryptor.java new file mode 100644 index 0000000..0160646 --- /dev/null +++ b/src/main/java/io/xjar/XSmtEncryptor.java @@ -0,0 +1,42 @@ +package io.xjar; + +import io.xjar.key.XKey; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * 智能加密器 + * + * @author Payne 646742615@qq.com + * 2019/7/4 16:10 + */ +public class XSmtEncryptor implements XEncryptor { + + @Override + public void encrypt(XKey key, String src, String dest) throws IOException { + new XJdkEncryptor(key.getAlgorithm()).encrypt(key, src, dest); + } + + @Override + public void encrypt(XKey key, File src, File dest) throws IOException { + new XJdkEncryptor(key.getAlgorithm()).encrypt(key, src, dest); + } + + @Override + public void encrypt(XKey key, InputStream in, OutputStream out) throws IOException { + new XJdkEncryptor(key.getAlgorithm()).encrypt(key, in, out); + } + + @Override + public InputStream encrypt(XKey key, InputStream in) throws IOException { + return new XJdkEncryptor(key.getAlgorithm()).encrypt(key, in); + } + + @Override + public OutputStream encrypt(XKey key, OutputStream out) throws IOException { + return new XJdkEncryptor(key.getAlgorithm()).encrypt(key, out); + } +} diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index a7b59a3..cb49568 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -37,14 +37,13 @@ public XJarDecryptor(XDecryptor xDecryptor, int level, XEntryFilter { - private int level; + private int level = Deflater.DEFLATED; + + { + decryptor(new XSmtDecryptor()); + filter(new XJarAllEntryFilter()); + } public XJarDecryptorBuilder level(int level) { this.level = level; diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 402c656..a5218c8 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -49,6 +49,11 @@ public XJarEncryptor(XEncryptor xEncryptor, int level, int mode, XEntryFilter { - private int level; - private int mode; + private int level = Deflater.DEFLATED; + private int mode = MODE_NORMAL; + + { + encryptor(new XSmtEncryptor()); + filter(new XJarAllEntryFilter()); + } public XJarEncryptorBuilder level(int level) { this.level = level; diff --git a/src/test/java/test/io/xjar/XJarTest.java b/src/test/java/test/io/xjar/XJarTest.java new file mode 100644 index 0000000..bcd5abc --- /dev/null +++ b/src/test/java/test/io/xjar/XJarTest.java @@ -0,0 +1,24 @@ +package test.io.xjar; + +import io.xjar.XKit; +import io.xjar.jar.XJarEncryptor; +import org.junit.Test; + +/** + * @author Payne 646742615@qq.com + * 2019/7/4 16:24 + */ +public class XJarTest { + + @Test + public void test() throws Exception { + XJarEncryptor.builder() + .build() + .encrypt( + XKit.key("io.xjar"), + "C:\\Users\\Chang\\Downloads\\Android-SDK\\target\\nwhs-sdk-v5.3.0-alpha.2.jar", + "C:\\Users\\Chang\\Downloads\\Android-SDK\\target\\nwhs-sdk-v5.3.0-alpha.2.xjar" + ); + } + +} From e01163b9e0db81c6e43e2a5a53e045e55f45e630 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 4 Jul 2019 17:11:19 +0800 Subject: [PATCH 06/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XArchiveDecryptor.java | 43 +++++++++++++++++++ src/main/java/io/xjar/XArchiveEncryptor.java | 43 +++++++++++++++++++ .../java/io/xjar/boot/XBootDecryptor.java | 12 +----- .../java/io/xjar/boot/XBootEncryptor.java | 12 +----- src/main/java/io/xjar/dir/XDirDecryptor.java | 5 +++ src/main/java/io/xjar/dir/XDirEncryptor.java | 5 +++ src/main/java/io/xjar/jar/XJarDecryptor.java | 23 +++------- src/main/java/io/xjar/jar/XJarEncryptor.java | 23 +++------- src/main/java/io/xjar/zip/XZipDecryptor.java | 16 ++----- src/main/java/io/xjar/zip/XZipEncryptor.java | 16 ++----- 10 files changed, 116 insertions(+), 82 deletions(-) create mode 100644 src/main/java/io/xjar/XArchiveDecryptor.java create mode 100644 src/main/java/io/xjar/XArchiveEncryptor.java diff --git a/src/main/java/io/xjar/XArchiveDecryptor.java b/src/main/java/io/xjar/XArchiveDecryptor.java new file mode 100644 index 0000000..4929645 --- /dev/null +++ b/src/main/java/io/xjar/XArchiveDecryptor.java @@ -0,0 +1,43 @@ +package io.xjar; + +import io.xjar.key.XKey; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * 记录可过滤的加密器 + * + * @author Payne 646742615@qq.com + * 2018/11/23 20:38 + */ +public abstract class XArchiveDecryptor extends XEntryDecryptor implements XDecryptor, XEntryFilter { + + protected XArchiveDecryptor(XDecryptor xDecryptor) { + super(xDecryptor); + } + + protected XArchiveDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { + super(xDecryptor, filter); + } + + @Override + public void decrypt(XKey key, String src, String dest) throws IOException { + decrypt(key, new File(src), new File(dest)); + } + + @Override + public void decrypt(XKey key, File src, File dest) throws IOException { + try ( + FileInputStream fis = new FileInputStream(src); + FileOutputStream fos = new FileOutputStream(dest) + ) { + decrypt(key, fis, fos); + } + } + + protected static abstract class XArchiveDecryptorBuilder, B extends XArchiveDecryptorBuilder> extends XEntryDecryptorBuilder { + } +} diff --git a/src/main/java/io/xjar/XArchiveEncryptor.java b/src/main/java/io/xjar/XArchiveEncryptor.java new file mode 100644 index 0000000..53c6ccb --- /dev/null +++ b/src/main/java/io/xjar/XArchiveEncryptor.java @@ -0,0 +1,43 @@ +package io.xjar; + +import io.xjar.key.XKey; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +/** + * 记录可过滤的加密器 + * + * @author Payne 646742615@qq.com + * 2018/11/23 20:38 + */ +public abstract class XArchiveEncryptor extends XEntryEncryptor implements XEncryptor, XEntryFilter { + + protected XArchiveEncryptor(XEncryptor xEncryptor) { + super(xEncryptor); + } + + protected XArchiveEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { + super(xEncryptor, filter); + } + + @Override + public void encrypt(XKey key, String src, String dest) throws IOException { + encrypt(key, new File(src), new File(dest)); + } + + @Override + public void encrypt(XKey key, File src, File dest) throws IOException { + try ( + FileInputStream fis = new FileInputStream(src); + FileOutputStream fos = new FileOutputStream(dest) + ) { + encrypt(key, fis, fos); + } + } + + protected static abstract class XArchiveEncryptorBuilder, B extends XArchiveEncryptorBuilder> extends XEntryEncryptorBuilder { + } +} diff --git a/src/main/java/io/xjar/boot/XBootDecryptor.java b/src/main/java/io/xjar/boot/XBootDecryptor.java index c4ea690..9c1bd72 100644 --- a/src/main/java/io/xjar/boot/XBootDecryptor.java +++ b/src/main/java/io/xjar/boot/XBootDecryptor.java @@ -21,7 +21,7 @@ * @author Payne 646742615@qq.com * 2018/11/22 15:27 */ -public class XBootDecryptor extends XEntryDecryptor implements XDecryptor, XConstants { +public class XBootDecryptor extends XArchiveDecryptor implements XDecryptor, XConstants { private final int level; public XBootDecryptor(XDecryptor xEncryptor) { @@ -41,16 +41,6 @@ public XBootDecryptor(XDecryptor xDecryptor, int level, XEntryFilter implements XEncryptor, XConstants { +public class XBootEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final Map map = new HashMap<>(); { @@ -66,16 +66,6 @@ public XBootEncryptor(XEncryptor xEncryptor, int level, int mode, XEntryFilter filter) { super(xDecryptor, filter); } + @Override + public void decrypt(XKey key, String src, String dest) throws IOException { + decrypt(key, new File(src), new File(dest)); + } + @Override public void decrypt(XKey key, File src, File dest) throws IOException { if (src.isFile()) { diff --git a/src/main/java/io/xjar/dir/XDirEncryptor.java b/src/main/java/io/xjar/dir/XDirEncryptor.java index a33192d..6db82e6 100644 --- a/src/main/java/io/xjar/dir/XDirEncryptor.java +++ b/src/main/java/io/xjar/dir/XDirEncryptor.java @@ -24,6 +24,11 @@ public XDirEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { super(xEncryptor, filter); } + @Override + public void encrypt(XKey key, String src, String dest) throws IOException { + encrypt(key, new File(src), new File(dest)); + } + @Override public void encrypt(XKey key, File src, File dest) throws IOException { if (src.isFile()) { diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index cb49568..4852c20 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -6,7 +6,9 @@ import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.jar.Attributes; import java.util.jar.Manifest; import java.util.zip.Deflater; @@ -17,7 +19,7 @@ * @author Payne 646742615@qq.com * 2018/11/22 15:27 */ -public class XJarDecryptor extends XEntryDecryptor implements XDecryptor, XConstants { +public class XJarDecryptor extends XArchiveDecryptor implements XDecryptor, XConstants { private final int level; public XJarDecryptor(XDecryptor xDecryptor) { @@ -41,11 +43,6 @@ public static XJarDecryptorBuilder builder() { return new XJarDecryptorBuilder(); } - @Override - public void decrypt(XKey key, String src, String dest) throws IOException { - decrypt(key, new File(src), new File(dest)); - } - @Override public void decrypt(XKey key, InputStream in, OutputStream out) throws IOException { JarArchiveInputStream zis = null; @@ -101,17 +98,7 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti } } - @Override - public void decrypt(XKey key, File src, File dest) throws IOException { - try ( - FileInputStream fis = new FileInputStream(src); - FileOutputStream fos = new FileOutputStream(dest) - ) { - decrypt(key, fis, fos); - } - } - - public static class XJarDecryptorBuilder extends XEntryDecryptorBuilder { + public static class XJarDecryptorBuilder extends XArchiveDecryptorBuilder { private int level = Deflater.DEFLATED; { diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index a5218c8..7ad19ee 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -6,7 +6,9 @@ import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.LinkedHashSet; import java.util.Set; import java.util.jar.Attributes; @@ -19,7 +21,7 @@ * @author Payne 646742615@qq.com * 2018/11/22 15:27 */ -public class XJarEncryptor extends XEntryEncryptor implements XEncryptor, XConstants { +public class XJarEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final int level; private final int mode; @@ -49,21 +51,6 @@ public XJarEncryptor(XEncryptor xEncryptor, int level, int mode, XEntryFilter { + public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; private int mode = MODE_NORMAL; diff --git a/src/main/java/io/xjar/zip/XZipDecryptor.java b/src/main/java/io/xjar/zip/XZipDecryptor.java index f7c6200..a3169fe 100644 --- a/src/main/java/io/xjar/zip/XZipDecryptor.java +++ b/src/main/java/io/xjar/zip/XZipDecryptor.java @@ -6,7 +6,9 @@ import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.zip.Deflater; /** @@ -15,7 +17,7 @@ * @author Payne 646742615@qq.com * 2018/11/22 15:27 */ -public class XZipDecryptor extends XEntryDecryptor implements XDecryptor { +public class XZipDecryptor extends XArchiveDecryptor implements XDecryptor { private final int level; public XZipDecryptor(XDecryptor xEncryptor) { @@ -35,16 +37,6 @@ public XZipDecryptor(XDecryptor xDecryptor, int level, XEntryFilter implements XEncryptor { +public class XZipEncryptor extends XArchiveEncryptor implements XEncryptor { private final int level; public XZipEncryptor(XEncryptor xEncryptor) { @@ -35,16 +37,6 @@ public XZipEncryptor(XEncryptor xEncryptor, int level, XEntryFilter Date: Thu, 4 Jul 2019 17:45:51 +0800 Subject: [PATCH 07/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XDigestedOutputStream.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/xjar/XDigestedOutputStream.java b/src/main/java/io/xjar/XDigestedOutputStream.java index 6121c2f..c00f833 100644 --- a/src/main/java/io/xjar/XDigestedOutputStream.java +++ b/src/main/java/io/xjar/XDigestedOutputStream.java @@ -26,7 +26,9 @@ public XDigestedOutputStream(OutputStream out, String encoding, XDigest xDigest) @Override public void write(byte[] buffer, int offset, int length) throws IOException { - xDigest.digest(buffer, offset, length); super.write(buffer, offset, length); + if (length > 0) { + xDigest.digest(buffer, offset, length); + } } } From cdfd498653a730c8fc1aaf0131f124eea582c1db Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 4 Jul 2019 17:57:27 +0800 Subject: [PATCH 08/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XKit.java | 6 +++--- src/main/java/io/xjar/XLauncher.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/xjar/XKit.java b/src/main/java/io/xjar/XKit.java index e2ff257..e69a6e2 100644 --- a/src/main/java/io/xjar/XKit.java +++ b/src/main/java/io/xjar/XKit.java @@ -380,11 +380,11 @@ public static boolean isAbsolute(String path) { return roots.contains(root); } - public static String absolutize(String path) { - return normalize(isAbsolute(path) ? path : System.getProperty("user.dir") + File.separator + path); + public static String toAbsolute(String path) { + return toStandard(isAbsolute(path) ? path : System.getProperty("user.dir") + File.separator + path); } - public static String normalize(String path) { + public static String toStandard(String path) { return path.replaceAll("[/\\\\]+", "/"); } diff --git a/src/main/java/io/xjar/XLauncher.java b/src/main/java/io/xjar/XLauncher.java index 331dbd0..b197f74 100644 --- a/src/main/java/io/xjar/XLauncher.java +++ b/src/main/java/io/xjar/XLauncher.java @@ -77,7 +77,7 @@ public XLauncher(String... args) throws Exception { Properties key = null; File keyfile = null; if (keypath != null) { - String path = XKit.absolutize(keypath); + String path = XKit.toAbsolute(keypath); File file = new File(path); if (file.exists() && file.isFile()) { keyfile = file; @@ -89,7 +89,7 @@ public XLauncher(String... args) throws Exception { throw new FileNotFoundException("could not find key file at path: " + file.getCanonicalPath()); } } else { - String path = XKit.absolutize("xjar.key"); + String path = XKit.toAbsolute("xjar.key"); File file = new File(path); if (file.exists() && file.isFile()) { keyfile = file; From 30b198cfb11506dc3b53ed0bfdc45f583c2bb345 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Fri, 5 Jul 2019 09:36:30 +0800 Subject: [PATCH 09/37] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Builder=E6=9D=A5?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=8A=A0=E5=AF=86=E8=A7=A3=E5=AF=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/jar/XJarDecryptor.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index 4852c20..76909e3 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -39,10 +39,6 @@ public XJarDecryptor(XDecryptor xDecryptor, int level, XEntryFilter { private int level = Deflater.DEFLATED; From 7ffd87bcd5abddaa2ff502bb30aa8e225d7d1c21 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Fri, 5 Jul 2019 10:50:30 +0800 Subject: [PATCH 10/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XDigest.java | 15 +++++++ src/main/java/io/xjar/XDigestFactory.java | 21 ++++++++++ .../{XMD2Digest.java => XJdkDigest.java} | 12 ++---- .../io/xjar/digest/XJdkDigestFactory.java | 20 ++++++++++ src/main/java/io/xjar/digest/XMD5Digest.java | 39 ------------------- 5 files changed, 60 insertions(+), 47 deletions(-) create mode 100644 src/main/java/io/xjar/XDigestFactory.java rename src/main/java/io/xjar/digest/{XMD2Digest.java => XJdkDigest.java} (66%) create mode 100644 src/main/java/io/xjar/digest/XJdkDigestFactory.java delete mode 100644 src/main/java/io/xjar/digest/XMD5Digest.java diff --git a/src/main/java/io/xjar/XDigest.java b/src/main/java/io/xjar/XDigest.java index 943f7a2..4e9ebb5 100644 --- a/src/main/java/io/xjar/XDigest.java +++ b/src/main/java/io/xjar/XDigest.java @@ -8,10 +8,25 @@ */ public interface XDigest { + /** + * 计算数据摘要 + * + * @param buf 缓冲 + * @param off 下标 + * @param len 长度 + */ void digest(byte[] buf, int off, int len); + /** + * 结束计算并返回摘要值 + * + * @return 最终摘要值 + */ byte[] finish(); + /** + * 恢复初始状态 + */ void resume(); } diff --git a/src/main/java/io/xjar/XDigestFactory.java b/src/main/java/io/xjar/XDigestFactory.java new file mode 100644 index 0000000..8e2b028 --- /dev/null +++ b/src/main/java/io/xjar/XDigestFactory.java @@ -0,0 +1,21 @@ +package io.xjar; + +import java.security.NoSuchAlgorithmException; + +/** + * 摘要算法对象工厂 + * + * @author Payne 646742615@qq.com + * 2019/7/5 10:16 + */ +public interface XDigestFactory { + + /** + * 生产摘要算法对象 + * + * @param algorithm 摘要算法 + * @return 摘要算法对象 + */ + XDigest produce(String algorithm) throws NoSuchAlgorithmException; + +} diff --git a/src/main/java/io/xjar/digest/XMD2Digest.java b/src/main/java/io/xjar/digest/XJdkDigest.java similarity index 66% rename from src/main/java/io/xjar/digest/XMD2Digest.java rename to src/main/java/io/xjar/digest/XJdkDigest.java index 3ac4976..dcd804d 100644 --- a/src/main/java/io/xjar/digest/XMD2Digest.java +++ b/src/main/java/io/xjar/digest/XJdkDigest.java @@ -6,20 +6,16 @@ import java.security.NoSuchAlgorithmException; /** - * MD2摘要算法 + * JDK内置摘要算法 * * @author Payne 646742615@qq.com * 2019/7/3 22:27 */ -public class XMD2Digest implements XDigest { +public class XJdkDigest implements XDigest { private final MessageDigest md; - public XMD2Digest() { - try { - md = MessageDigest.getInstance("MD2"); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } + public XJdkDigest(String algorithm) throws NoSuchAlgorithmException { + md = MessageDigest.getInstance(algorithm); } @Override diff --git a/src/main/java/io/xjar/digest/XJdkDigestFactory.java b/src/main/java/io/xjar/digest/XJdkDigestFactory.java new file mode 100644 index 0000000..c04f0a9 --- /dev/null +++ b/src/main/java/io/xjar/digest/XJdkDigestFactory.java @@ -0,0 +1,20 @@ +package io.xjar.digest; + +import io.xjar.XDigest; +import io.xjar.XDigestFactory; + +import java.security.NoSuchAlgorithmException; + +/** + * JDK内置摘要算法对象工厂 + * + * @author Payne 646742615@qq.com + * 2019/7/5 10:22 + */ +public class XJdkDigestFactory implements XDigestFactory { + + @Override + public XDigest produce(String algorithm) throws NoSuchAlgorithmException { + return new XJdkDigest(algorithm); + } +} diff --git a/src/main/java/io/xjar/digest/XMD5Digest.java b/src/main/java/io/xjar/digest/XMD5Digest.java deleted file mode 100644 index fe16cd5..0000000 --- a/src/main/java/io/xjar/digest/XMD5Digest.java +++ /dev/null @@ -1,39 +0,0 @@ -package io.xjar.digest; - -import io.xjar.XDigest; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * MD5摘要算法 - * - * @author Payne 646742615@qq.com - * 2019/7/3 22:27 - */ -public class XMD5Digest implements XDigest { - private final MessageDigest md; - - public XMD5Digest() { - try { - md = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException e) { - throw new RuntimeException(e); - } - } - - @Override - public void digest(byte[] buf, int off, int len) { - md.update(buf, off, len); - } - - @Override - public byte[] finish() { - return md.digest(); - } - - @Override - public void resume() { - md.reset(); - } -} From d287bda4d3e1718d62880601baab3b21f1afdbe5 Mon Sep 17 00:00:00 2001 From: core-lib <646742615@qq.com> Date: Sat, 6 Jul 2019 11:52:46 +0800 Subject: [PATCH 11/37] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E6=97=B6=E7=AD=BE?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XInjector.java | 5 +++-- src/main/java/io/xjar/boot/XBootEncryptor.java | 2 +- src/main/java/io/xjar/jar/XJarEncryptor.java | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/xjar/XInjector.java b/src/main/java/io/xjar/XInjector.java index f5eb6ef..8e9f801 100644 --- a/src/main/java/io/xjar/XInjector.java +++ b/src/main/java/io/xjar/XInjector.java @@ -23,11 +23,12 @@ public class XInjector { * 往JAR包中注入XJar框架的classes * * @param zos jar包输出流 + * @param ant 资源路径ANT表达式 * @throws IOException I/O 异常 */ - public static void inject(JarArchiveOutputStream zos) throws IOException { + public static void inject(JarArchiveOutputStream zos, String ant) throws IOException { Set directories = new HashSet<>(); - Enumeration resources = Loaders.ant().load("io/xjar/**"); + Enumeration resources = Loaders.ant().load(ant); while (resources.hasMoreElements()) { Resource resource = resources.nextElement(); String name = resource.getName(); diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index 9617146..e4bc95f 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -167,7 +167,7 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { String mainClass = manifest != null && manifest.getMainAttributes() != null ? manifest.getMainAttributes().getValue("Main-Class") : null; if (mainClass != null) { - XInjector.inject(zos); + XInjector.inject(zos, "io/xjar/**"); } zos.finish(); diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 7ad19ee..a498021 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -124,7 +124,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti String mainClass = manifest != null && manifest.getMainAttributes() != null ? manifest.getMainAttributes().getValue("Main-Class") : null; if (mainClass != null) { - XInjector.inject(zos); + XInjector.inject(zos, "io/xjar/**"); } zos.finish(); From d2ef5dfc9af66f29d3bdeeaaf4f775f51a44b374 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Mon, 8 Jul 2019 15:02:02 +0800 Subject: [PATCH 12/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XArchiveDecryptor.java | 4 - src/main/java/io/xjar/XArchiveEncryptor.java | 4 - src/main/java/io/xjar/XEntryDecryptor.java | 4 - src/main/java/io/xjar/XEntryEncryptor.java | 4 - src/main/java/io/xjar/boot/XBoot.java | 973 +----------------- .../java/io/xjar/boot/XBootDecryptor.java | 38 +- .../java/io/xjar/boot/XBootEncryptor.java | 51 +- src/main/java/io/xjar/jar/XJar.java | 973 +----------------- src/main/java/io/xjar/jar/XJarDecryptor.java | 16 +- src/main/java/io/xjar/jar/XJarEncryptor.java | 35 +- 10 files changed, 54 insertions(+), 2048 deletions(-) diff --git a/src/main/java/io/xjar/XArchiveDecryptor.java b/src/main/java/io/xjar/XArchiveDecryptor.java index 4929645..529c5f7 100644 --- a/src/main/java/io/xjar/XArchiveDecryptor.java +++ b/src/main/java/io/xjar/XArchiveDecryptor.java @@ -15,10 +15,6 @@ */ public abstract class XArchiveDecryptor extends XEntryDecryptor implements XDecryptor, XEntryFilter { - protected XArchiveDecryptor(XDecryptor xDecryptor) { - super(xDecryptor); - } - protected XArchiveDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { super(xDecryptor, filter); } diff --git a/src/main/java/io/xjar/XArchiveEncryptor.java b/src/main/java/io/xjar/XArchiveEncryptor.java index 53c6ccb..b72503c 100644 --- a/src/main/java/io/xjar/XArchiveEncryptor.java +++ b/src/main/java/io/xjar/XArchiveEncryptor.java @@ -15,10 +15,6 @@ */ public abstract class XArchiveEncryptor extends XEntryEncryptor implements XEncryptor, XEntryFilter { - protected XArchiveEncryptor(XEncryptor xEncryptor) { - super(xEncryptor); - } - protected XArchiveEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { super(xEncryptor, filter); } diff --git a/src/main/java/io/xjar/XEntryDecryptor.java b/src/main/java/io/xjar/XEntryDecryptor.java index 40c2571..9a078c2 100644 --- a/src/main/java/io/xjar/XEntryDecryptor.java +++ b/src/main/java/io/xjar/XEntryDecryptor.java @@ -10,10 +10,6 @@ public abstract class XEntryDecryptor extends XWrappedDecryptor implements XD protected final XEntryFilter filter; protected final XNopDecryptor xNopDecryptor = new XNopDecryptor(); - protected XEntryDecryptor(XDecryptor xDecryptor) { - this(xDecryptor, null); - } - protected XEntryDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { super(xDecryptor); this.filter = filter; diff --git a/src/main/java/io/xjar/XEntryEncryptor.java b/src/main/java/io/xjar/XEntryEncryptor.java index 1950ab5..72459fd 100644 --- a/src/main/java/io/xjar/XEntryEncryptor.java +++ b/src/main/java/io/xjar/XEntryEncryptor.java @@ -10,10 +10,6 @@ public abstract class XEntryEncryptor extends XWrappedEncryptor implements XE protected final XEntryFilter filter; protected final XNopEncryptor xNopEncryptor = new XNopEncryptor(); - protected XEntryEncryptor(XEncryptor xEncryptor) { - this(xEncryptor, null); - } - protected XEntryEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { super(xEncryptor); this.filter = filter; diff --git a/src/main/java/io/xjar/boot/XBoot.java b/src/main/java/io/xjar/boot/XBoot.java index 5dbf470..87a3a29 100644 --- a/src/main/java/io/xjar/boot/XBoot.java +++ b/src/main/java/io/xjar/boot/XBoot.java @@ -1,11 +1,6 @@ package io.xjar.boot; -import io.xjar.*; -import io.xjar.key.XKey; -import org.apache.commons.compress.archivers.jar.JarArchiveEntry; - -import java.io.*; -import java.util.zip.Deflater; +import io.xjar.XConstants; /** * Spring-Boot JAR包加解密工具类,在不提供过滤器的情况下会加密BOOT-INF/下的所有资源,及包括项目本身的资源和依赖jar资源。 @@ -14,971 +9,5 @@ * 2018/11/26 11:11 */ public class XBoot implements XConstants { - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey) throws Exception { - encrypt(new File(src), new File(dest), xKey); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey, int mode) throws Exception { - encrypt(new File(src), new File(dest), xKey, mode); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey); - } - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey, int mode) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey, mode); - } - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey) throws Exception { - XBootEncryptor xBootEncryptor = new XBootEncryptor(new XJdkEncryptor(xKey.getAlgorithm())); - xBootEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @param mode 加密模式 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey, int mode) throws Exception { - XBootEncryptor xBootEncryptor = new XBootEncryptor(new XJdkEncryptor(xKey.getAlgorithm()), Deflater.DEFLATED, mode); - xBootEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey, XEntryFilter filter) throws Exception { - encrypt(new File(src), new File(dest), xKey, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey, int mode, XEntryFilter filter) throws Exception { - encrypt(new File(src), new File(dest), xKey, mode, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey, filter); - } - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey, int mode, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey, mode, filter); - } - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey, XEntryFilter filter) throws Exception { - XBootEncryptor xBootEncryptor = new XBootEncryptor(new XJdkEncryptor(xKey.getAlgorithm()), filter); - xBootEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @param mode 加密模式 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey, int mode, XEntryFilter filter) throws Exception { - XBootEncryptor xBootEncryptor = new XBootEncryptor(new XJdkEncryptor(xKey.getAlgorithm()), Deflater.DEFLATED, mode, filter); - xBootEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - encrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, password, algorithm, keysize, ivsize); - } - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password) throws Exception { - encrypt(in, out, password, DEFAULT_ALGORITHM); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm) throws Exception { - encrypt(in, out, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize) throws Exception { - encrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize) throws Exception { - XBootEncryptor xBootEncryptor = new XBootEncryptor(new XJdkEncryptor(algorithm)); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xBootEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - encrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, password, algorithm, keysize, ivsize, filter); - } - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, XEntryFilter filter) throws Exception { - encrypt(in, out, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, XEntryFilter filter) throws Exception { - encrypt(in, out, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - encrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 加密 Spring-Boot JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - XBootEncryptor xBootEncryptor = new XBootEncryptor(new XJdkEncryptor(algorithm), filter); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xBootEncryptor.encrypt(xKey, in, out); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, XKey xKey) throws Exception { - decrypt(new File(src), new File(dest), xKey); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, XKey xKey) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, xKey); - } - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param xKey 密钥 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, XKey xKey) throws Exception { - XBootDecryptor xBootDecryptor = new XBootDecryptor(new XJdkDecryptor(xKey.getAlgorithm())); - xBootDecryptor.decrypt(xKey, in, out); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, XKey xKey, XEntryFilter filter) throws Exception { - decrypt(new File(src), new File(dest), xKey, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, XKey xKey, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, xKey, filter); - } - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, XKey xKey, XEntryFilter filter) throws Exception { - XBootDecryptor xBootDecryptor = new XBootDecryptor(new XJdkDecryptor(xKey.getAlgorithm()), filter); - xBootDecryptor.decrypt(xKey, in, out); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - decrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, password, algorithm, keysize, ivsize); - } - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password) throws Exception { - decrypt(in, out, password, DEFAULT_ALGORITHM); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm) throws Exception { - decrypt(in, out, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize) throws Exception { - decrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize) throws Exception { - XBootDecryptor xBootDecryptor = new XBootDecryptor(new XJdkDecryptor(algorithm)); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xBootDecryptor.decrypt(xKey, in, out); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - decrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, password, algorithm, keysize, ivsize, filter); - } - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, XEntryFilter filter) throws Exception { - decrypt(in, out, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, XEntryFilter filter) throws Exception { - decrypt(in, out, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - decrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 解密 Spring-Boot JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - XBootDecryptor xBootDecryptor = new XBootDecryptor(new XJdkDecryptor(algorithm), filter); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xBootDecryptor.decrypt(xKey, in, out); - } } diff --git a/src/main/java/io/xjar/boot/XBootDecryptor.java b/src/main/java/io/xjar/boot/XBootDecryptor.java index 9c1bd72..c81aed1 100644 --- a/src/main/java/io/xjar/boot/XBootDecryptor.java +++ b/src/main/java/io/xjar/boot/XBootDecryptor.java @@ -24,19 +24,7 @@ public class XBootDecryptor extends XArchiveDecryptor implements XDecryptor, XConstants { private final int level; - public XBootDecryptor(XDecryptor xEncryptor) { - this(xEncryptor, new XJarAllEntryFilter()); - } - - public XBootDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { - this(xDecryptor, Deflater.DEFLATED, filter); - } - - public XBootDecryptor(XDecryptor xEncryptor, int level) { - this(xEncryptor, level, new XJarAllEntryFilter()); - } - - public XBootDecryptor(XDecryptor xDecryptor, int level, XEntryFilter filter) { + public XBootDecryptor(XDecryptor xDecryptor, XEntryFilter filter, int level) { super(xDecryptor, filter); this.level = level; } @@ -51,7 +39,7 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); - XJarDecryptor xJarDecryptor = new XJarDecryptor(xDecryptor, level, filter); + XJarDecryptor xJarDecryptor = new XJarDecryptor(xDecryptor, filter, level); JarArchiveEntry entry; while ((entry = zis.getNextJarEntry()) != null) { if (entry.getName().startsWith(XJAR_SRC_DIR) @@ -124,4 +112,26 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { } } + public static XBootDecryptorBuilder builder() { + return new XBootDecryptorBuilder(); + } + + public static class XBootDecryptorBuilder extends XArchiveDecryptorBuilder { + private int level = Deflater.DEFLATED; + + { + decryptor(new XSmtDecryptor()); + filter(new XJarAllEntryFilter()); + } + + public XBootDecryptorBuilder level(int level) { + this.level = level; + return this; + } + + @Override + public XBootDecryptor build() { + return new XBootDecryptor(decryptor, filter, level); + } + } } diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index e4bc95f..c8b579b 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -38,32 +38,10 @@ public class XBootEncryptor extends XArchiveEncryptor implement } private final int level; - private final int mode; - public XBootEncryptor(XEncryptor xEncryptor) { - this(xEncryptor, new XJarAllEntryFilter()); - } - - public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { - this(xEncryptor, Deflater.DEFLATED, filter); - } - - public XBootEncryptor(XEncryptor xEncryptor, int level) { - this(xEncryptor, level, new XJarAllEntryFilter()); - } - - public XBootEncryptor(XEncryptor xEncryptor, int level, XEntryFilter filter) { - this(xEncryptor, level, MODE_NORMAL, filter); - } - - public XBootEncryptor(XEncryptor xEncryptor, int level, int mode) { - this(xEncryptor, level, mode, new XJarAllEntryFilter()); - } - - public XBootEncryptor(XEncryptor xEncryptor, int level, int mode, XEntryFilter filter) { + public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { super(xEncryptor, filter); this.level = level; - this.mode = mode; } @Override @@ -77,7 +55,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); - XJarEncryptor xJarEncryptor = new XJarEncryptor(xEncryptor, level, filter); + XJarEncryptor xJarEncryptor = new XJarEncryptor(xEncryptor, filter, level); JarArchiveEntry entry; Manifest manifest = null; while ((entry = zis.getNextJarEntry()) != null) { @@ -102,9 +80,6 @@ else if (entry.getName().equals(META_INF_MANIFEST)) { attributes.putValue("Boot-Main-Class", mainClass); attributes.putValue("Main-Class", map.get(mainClass)); } - if ((mode & FLAG_DANGER) == FLAG_DANGER) { - XKit.retainKey(key, attributes); - } JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); jarArchiveEntry.setTime(entry.getTime()); zos.putArchiveEntry(jarArchiveEntry); @@ -177,4 +152,26 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { } } + public static XBootEncryptorBuilder builder() { + return new XBootEncryptorBuilder(); + } + + public static class XBootEncryptorBuilder extends XArchiveEncryptorBuilder { + private int level = Deflater.DEFLATED; + + { + encryptor(new XSmtEncryptor()); + filter(new XJarAllEntryFilter()); + } + + public XBootEncryptorBuilder level(int level) { + this.level = level; + return this; + } + + @Override + public XBootEncryptor build() { + return new XBootEncryptor(encryptor, filter, level); + } + } } diff --git a/src/main/java/io/xjar/jar/XJar.java b/src/main/java/io/xjar/jar/XJar.java index b0fb7af..18e6e4d 100644 --- a/src/main/java/io/xjar/jar/XJar.java +++ b/src/main/java/io/xjar/jar/XJar.java @@ -1,11 +1,6 @@ package io.xjar.jar; -import io.xjar.*; -import io.xjar.key.XKey; -import org.apache.commons.compress.archivers.jar.JarArchiveEntry; - -import java.io.*; -import java.util.zip.Deflater; +import io.xjar.XConstants; /** * 普通JAR包加解密工具类 @@ -14,971 +9,5 @@ * 2018/11/26 11:11 */ public class XJar implements XConstants { - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey) throws Exception { - encrypt(new File(src), new File(dest), xKey); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey, int mode) throws Exception { - encrypt(new File(src), new File(dest), xKey, mode); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey); - } - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey, int mode) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey, mode); - } - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey) throws Exception { - XJarEncryptor xJarEncryptor = new XJarEncryptor(new XJdkEncryptor(xKey.getAlgorithm())); - xJarEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @param mode 加密模式 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey, int mode) throws Exception { - XJarEncryptor xJarEncryptor = new XJarEncryptor(new XJdkEncryptor(xKey.getAlgorithm()), Deflater.DEFLATED, mode); - xJarEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey, XEntryFilter filter) throws Exception { - encrypt(new File(src), new File(dest), xKey, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, XKey xKey, int mode, XEntryFilter filter) throws Exception { - encrypt(new File(src), new File(dest), xKey, mode, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey, filter); - } - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param xKey 密钥 - * @param mode 加密模式 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, XKey xKey, int mode, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, xKey, mode, filter); - } - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey, XEntryFilter filter) throws Exception { - XJarEncryptor xJarEncryptor = new XJarEncryptor(new XJdkEncryptor(xKey.getAlgorithm()), filter); - xJarEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param xKey 密钥 - * @param mode 加密模式 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, XKey xKey, int mode, XEntryFilter filter) throws Exception { - XJarEncryptor xJarEncryptor = new XJarEncryptor(new XJdkEncryptor(xKey.getAlgorithm()), Deflater.DEFLATED, mode, filter); - xJarEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - encrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, password, algorithm, keysize, ivsize); - } - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password) throws Exception { - encrypt(in, out, password, DEFAULT_ALGORITHM); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm) throws Exception { - encrypt(in, out, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize) throws Exception { - encrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize) throws Exception { - XJarEncryptor xJarEncryptor = new XJarEncryptor(new XJdkEncryptor(algorithm)); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xJarEncryptor.encrypt(xKey, in, out); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - encrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - encrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param src 原文包 - * @param dest 加密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - encrypt(in, out, password, algorithm, keysize, ivsize, filter); - } - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, XEntryFilter filter) throws Exception { - encrypt(in, out, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, XEntryFilter filter) throws Exception { - encrypt(in, out, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - encrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 加密 普通 JAR 包 - * - * @param in 原文包输入流 - * @param out 加密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 加密异常 - */ - public static void encrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - XJarEncryptor xJarEncryptor = new XJarEncryptor(new XJdkEncryptor(algorithm), filter); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xJarEncryptor.encrypt(xKey, in, out); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, XKey xKey) throws Exception { - decrypt(new File(src), new File(dest), xKey); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, XKey xKey) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, xKey); - } - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param xKey 密钥 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, XKey xKey) throws Exception { - XJarDecryptor xJarDecryptor = new XJarDecryptor(new XJdkDecryptor(xKey.getAlgorithm())); - xJarDecryptor.decrypt(xKey, in, out); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, XKey xKey, XEntryFilter filter) throws Exception { - decrypt(new File(src), new File(dest), xKey, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, XKey xKey, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, xKey, filter); - } - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param xKey 密钥 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, XKey xKey, XEntryFilter filter) throws Exception { - XJarDecryptor xJarDecryptor = new XJarDecryptor(new XJdkDecryptor(xKey.getAlgorithm()), filter); - xJarDecryptor.decrypt(xKey, in, out); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - decrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, password, algorithm, keysize, ivsize); - } - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password) throws Exception { - decrypt(in, out, password, DEFAULT_ALGORITHM); - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm) throws Exception { - decrypt(in, out, password, algorithm, DEFAULT_KEYSIZE); - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize) throws Exception { - decrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE); - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize) throws Exception { - XJarDecryptor xJarDecryptor = new XJarDecryptor(new XJdkDecryptor(algorithm)); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xJarDecryptor.decrypt(xKey, in, out); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(String src, String dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - decrypt(new File(src), new File(dest), password, algorithm, keysize, ivsize, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - decrypt(src, dest, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param src 加密包 - * @param dest 解密包 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(File src, File dest, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - try ( - InputStream in = new FileInputStream(src); - OutputStream out = new FileOutputStream(dest) - ) { - decrypt(in, out, password, algorithm, keysize, ivsize, filter); - } - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, XEntryFilter filter) throws Exception { - decrypt(in, out, password, DEFAULT_ALGORITHM, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, XEntryFilter filter) throws Exception { - decrypt(in, out, password, algorithm, DEFAULT_KEYSIZE, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, XEntryFilter filter) throws Exception { - decrypt(in, out, password, algorithm, keysize, DEFAULT_IVSIZE, filter); - } - - /** - * 解密 普通 JAR 包 - * - * @param in 加密包输入流 - * @param out 解密包输出流 - * @param password 密码 - * @param algorithm 加密算法 - * @param keysize 密钥长度 - * @param ivsize 向量长度 - * @param filter 过滤器 - * @throws Exception 解密异常 - */ - public static void decrypt(InputStream in, OutputStream out, String password, String algorithm, int keysize, int ivsize, XEntryFilter filter) throws Exception { - XJarDecryptor xJarDecryptor = new XJarDecryptor(new XJdkDecryptor(algorithm), filter); - XKey xKey = XKit.key(algorithm, keysize, ivsize, password); - xJarDecryptor.decrypt(xKey, in, out); - } } diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index 76909e3..28867cc 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -22,19 +22,7 @@ public class XJarDecryptor extends XArchiveDecryptor implements XDecryptor, XConstants { private final int level; - public XJarDecryptor(XDecryptor xDecryptor) { - this(xDecryptor, new XJarAllEntryFilter()); - } - - public XJarDecryptor(XDecryptor xDecryptor, XEntryFilter filter) { - this(xDecryptor, Deflater.DEFLATED, filter); - } - - public XJarDecryptor(XDecryptor xDecryptor, int level) { - this(xDecryptor, level, new XJarAllEntryFilter()); - } - - public XJarDecryptor(XDecryptor xDecryptor, int level, XEntryFilter filter) { + public XJarDecryptor(XDecryptor xDecryptor, XEntryFilter filter, int level) { super(xDecryptor, filter); this.level = level; } @@ -113,7 +101,7 @@ public XJarDecryptorBuilder level(int level) { @Override public XJarDecryptor build() { - return new XJarDecryptor(decryptor, level, filter); + return new XJarDecryptor(decryptor, filter, level); } } } diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index a498021..27817c7 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -23,32 +23,10 @@ */ public class XJarEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final int level; - private final int mode; - public XJarEncryptor(XEncryptor xEncryptor) { - this(xEncryptor, new XJarAllEntryFilter()); - } - - public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter) { - this(xEncryptor, Deflater.DEFLATED, filter); - } - - public XJarEncryptor(XEncryptor xEncryptor, int level) { - this(xEncryptor, level, new XJarAllEntryFilter()); - } - - public XJarEncryptor(XEncryptor xEncryptor, int level, XEntryFilter filter) { - this(xEncryptor, level, MODE_NORMAL, filter); - } - - public XJarEncryptor(XEncryptor xEncryptor, int level, int mode) { - this(xEncryptor, level, mode, new XJarAllEntryFilter()); - } - - public XJarEncryptor(XEncryptor xEncryptor, int level, int mode, XEntryFilter filter) { + public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { super(xEncryptor, filter); this.level = level; - this.mode = mode; } @Override @@ -83,9 +61,6 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti attributes.putValue("Jar-Main-Class", mainClass); attributes.putValue("Main-Class", "io.xjar.jar.XJarLauncher"); } - if ((mode & FLAG_DANGER) == FLAG_DANGER) { - XKit.retainKey(key, attributes); - } JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); jarArchiveEntry.setTime(entry.getTime()); zos.putArchiveEntry(jarArchiveEntry); @@ -140,7 +115,6 @@ public static XJarEncryptorBuilder builder() { public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; - private int mode = MODE_NORMAL; { encryptor(new XSmtEncryptor()); @@ -152,14 +126,9 @@ public XJarEncryptorBuilder level(int level) { return this; } - public XJarEncryptorBuilder mode(int mode) { - this.mode = mode; - return this; - } - @Override public XJarEncryptor build() { - return new XJarEncryptor(encryptor, level, mode, filter); + return new XJarEncryptor(encryptor, filter, level); } } } From 4adee62dccdb96ced66cb3fb18c1102f023bb0d4 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Mon, 8 Jul 2019 15:02:41 +0800 Subject: [PATCH 13/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XConstants.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/io/xjar/XConstants.java b/src/main/java/io/xjar/XConstants.java index 349f756..8e73027 100644 --- a/src/main/java/io/xjar/XConstants.java +++ b/src/main/java/io/xjar/XConstants.java @@ -10,9 +10,6 @@ public interface XConstants { String BOOT_INF_CLASSES = "BOOT-INF/classes/"; String BOOT_INF_LIB = "BOOT-INF/lib/"; - String WEB_INF_CLASSES = "WEB-INF/classes/"; - String WEB_INF_LIB = "WEB-INF/lib/"; - String META_INF_MANIFEST = "META-INF/MANIFEST.MF"; String XJAR_SRC_DIR = XConstants.class.getPackage().getName().replace('.', '/') + "/"; String XJAR_INF_DIR = "XJAR-INF/"; @@ -40,11 +37,4 @@ public interface XConstants { int DEFAULT_KEYSIZE = 128; int DEFAULT_IVSIZE = 128; - // 保留密钥在 META-INF/MANIFEST.MF 中,启动时无需输入密钥。 - int FLAG_DANGER = 1; - // 危险模式:保留密钥 - int MODE_DANGER = FLAG_DANGER; - // 普通模式 - int MODE_NORMAL = 0; - } From c62fd3c5206cde424b7cd3d334ba95c991ff16e9 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Mon, 8 Jul 2019 15:54:49 +0800 Subject: [PATCH 14/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XInjector.java | 2 +- src/main/java/io/xjar/XJdkDecryptor.java | 4 +- src/main/java/io/xjar/XJdkEncryptor.java | 4 +- src/main/java/io/xjar/XKit.java | 268 +++--------------- src/main/java/io/xjar/XLauncher.java | 4 +- src/main/java/io/xjar/XNopDecryptor.java | 2 +- src/main/java/io/xjar/XNopEncryptor.java | 2 +- src/main/java/io/xjar/XTool.java | 243 ++++++++++++++++ src/main/java/io/xjar/boot/XBoot.java | 21 +- .../java/io/xjar/boot/XBootClassLoader.java | 4 +- .../java/io/xjar/boot/XBootDecryptor.java | 11 +- .../java/io/xjar/boot/XBootEncryptor.java | 10 +- src/main/java/io/xjar/jar/XJar.java | 22 +- .../java/io/xjar/jar/XJarClassLoader.java | 4 +- src/main/java/io/xjar/jar/XJarDecryptor.java | 7 +- src/main/java/io/xjar/jar/XJarEncryptor.java | 6 +- src/main/java/io/xjar/key/XKey.java | 4 +- src/main/java/io/xjar/zip/XZipDecryptor.java | 6 +- src/main/java/io/xjar/zip/XZipEncryptor.java | 6 +- src/test/java/test/io/xjar/XJarTest.java | 6 +- 20 files changed, 356 insertions(+), 280 deletions(-) create mode 100644 src/main/java/io/xjar/XTool.java diff --git a/src/main/java/io/xjar/XInjector.java b/src/main/java/io/xjar/XInjector.java index 8e9f801..775ed5a 100644 --- a/src/main/java/io/xjar/XInjector.java +++ b/src/main/java/io/xjar/XInjector.java @@ -43,7 +43,7 @@ public static void inject(JarArchiveOutputStream zos, String ant) throws IOExcep xJarEntry.setTime(System.currentTimeMillis()); zos.putArchiveEntry(xJarEntry); try (InputStream ris = resource.getInputStream()) { - XKit.transfer(ris, zos); + XTool.transfer(ris, zos); } zos.closeArchiveEntry(); } diff --git a/src/main/java/io/xjar/XJdkDecryptor.java b/src/main/java/io/xjar/XJdkDecryptor.java index 6a85902..0b2e606 100644 --- a/src/main/java/io/xjar/XJdkDecryptor.java +++ b/src/main/java/io/xjar/XJdkDecryptor.java @@ -47,11 +47,11 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getDecryptKey(), algorithm)); cis = new CipherInputStream(in, cipher); - XKit.transfer(cis, out); + XTool.transfer(cis, out); } catch (Exception e) { throw new IOException(e); } finally { - XKit.close(cis); + XTool.close(cis); } } diff --git a/src/main/java/io/xjar/XJdkEncryptor.java b/src/main/java/io/xjar/XJdkEncryptor.java index 861480a..ee50eee 100644 --- a/src/main/java/io/xjar/XJdkEncryptor.java +++ b/src/main/java/io/xjar/XJdkEncryptor.java @@ -47,11 +47,11 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getEncryptKey(), algorithm)); cis = new CipherInputStream(in, cipher); - XKit.transfer(cis, out); + XTool.transfer(cis, out); } catch (Exception e) { throw new IOException(e); } finally { - XKit.close(cis); + XTool.close(cis); } } diff --git a/src/main/java/io/xjar/XKit.java b/src/main/java/io/xjar/XKit.java index e69a6e2..d20f22c 100644 --- a/src/main/java/io/xjar/XKit.java +++ b/src/main/java/io/xjar/XKit.java @@ -1,209 +1,27 @@ package io.xjar; -import io.xjar.filter.XAllEntryFilter; -import io.xjar.filter.XAnyEntryFilter; -import io.xjar.filter.XNotEntryFilter; +import io.xjar.filter.*; +import io.xjar.jar.XJarAntEntryFilter; +import io.xjar.jar.XJarRegexEntryFilter; import io.xjar.key.XKey; import io.xjar.key.XSecureRandom; import io.xjar.key.XSymmetricSecureKey; +import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; -import java.io.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.jar.Attributes; /** - * XJar 工具类,包含I/O,密钥,过滤器的工具方法。 + * 工具类 + * + * @author Payne 646742615@qq.com + * 2019/7/8 15:52 */ public abstract class XKit implements XConstants { - /** - * 从输入流中读取一行字节码 - * - * @param in 输入流 - * @return 最前面的一行字节码 - * @throws IOException I/O 异常 - */ - public static byte[] readln(InputStream in) throws IOException { - int b = in.read(); - if (b == -1) { - return null; - } - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - while (b != -1) { - switch (b) { - case '\r': - break; - case '\n': - return bos.toByteArray(); - default: - bos.write(b); - break; - } - b = in.read(); - } - return bos.toByteArray(); - } - - /** - * 往输出流中写入一行字节码 - * - * @param out 输出流 - * @param line 一行字节码 - * @throws IOException I/O 异常 - */ - public static void writeln(OutputStream out, byte[] line) throws IOException { - if (line == null) { - return; - } - out.write(line); - out.write('\r'); - out.write('\n'); - } - - /** - * 关闭资源,等效于XKit.close(closeable, true); - * - * @param closeable 资源 - */ - public static void close(Closeable closeable) { - try { - close(closeable, true); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - /** - * 关闭资源 - * - * @param closeable 资源 - * @param quietly 是否安静关闭,即捕获到关闭异常时是否忽略 - * @throws IOException 当quietly == false, 时捕获到的I/O异常将会往外抛 - */ - public static void close(Closeable closeable, boolean quietly) throws IOException { - if (closeable == null) return; - try { - closeable.close(); - } catch (IOException e) { - if (!quietly) throw e; - } - } - - /** - * 输入流传输到输出流 - * - * @param in 输入流 - * @param out 输出流 - * @return 传输长度 - * @throws IOException I/O 异常 - */ - public static long transfer(InputStream in, OutputStream out) throws IOException { - long total = 0; - byte[] buffer = new byte[4096]; - int length; - while ((length = in.read(buffer)) != -1) { - out.write(buffer, 0, length); - total += length; - } - out.flush(); - return total; - } - - /** - * reader传输到writer - * - * @param reader reader - * @param writer writer - * @return 传输长度 - * @throws IOException I/O 异常 - */ - public static long transfer(Reader reader, Writer writer) throws IOException { - long total = 0; - char[] buffer = new char[4096]; - int length; - while ((length = reader.read(buffer)) != -1) { - writer.write(buffer, 0, length); - total += length; - } - writer.flush(); - return total; - } - - /** - * 输入流传输到文件 - * - * @param in 输入流 - * @param file 文件 - * @return 传输长度 - * @throws IOException I/O 异常 - */ - public static long transfer(InputStream in, File file) throws IOException { - OutputStream out = null; - try { - out = new FileOutputStream(file); - return transfer(in, out); - } finally { - close(out); - } - } - - /** - * reader传输到文件 - * - * @param reader reader - * @param file 文件 - * @return 传输长度 - * @throws IOException I/O 异常 - */ - public static long transfer(Reader reader, File file) throws IOException { - OutputStream out = null; - Writer writer = null; - try { - out = new FileOutputStream(file); - writer = new OutputStreamWriter(out); - return transfer(reader, writer); - } finally { - close(writer); - close(out); - } - } - - /** - * 删除文件,如果是目录将不递归删除子文件或目录,等效于delete(file, false); - * - * @param file 文件/目录 - * @return 是否删除成功 - */ - public static boolean delete(File file) { - return delete(file, false); - } - - /** - * 删除文件,如果是目录将递归删除子文件或目录 - * - * @param file 文件/目录 - * @return 是否删除成功 - */ - public static boolean delete(File file, boolean recursively) { - if (file.isDirectory() && recursively) { - boolean deleted = true; - File[] files = file.listFiles(); - for (int i = 0; files != null && i < files.length; i++) { - deleted &= delete(files[i], true); - } - return deleted && file.delete(); - } else { - return file.delete(); - } - } - /** * 根据密码生成密钥 * @@ -262,26 +80,12 @@ public static XKey key(String algorithm, int keysize, int ivsize, String passwor return new XSymmetricSecureKey(algorithm, keysize, ivsize, password, key.getEncoded(), iv.getEncoded()); } - public static void retainKey(XKey key, Attributes attributes) { - attributes.putValue(XJAR_ALGORITHM_KEY, key.getAlgorithm()); - attributes.putValue(XJAR_KEYSIZE_KEY, String.valueOf(key.getKeysize())); - attributes.putValue(XJAR_IVSIZE_KEY, String.valueOf(key.getIvsize())); - attributes.putValue(XJAR_PASSWORD_KEY, key.getPassword()); - } - - public static void removeKey(Attributes attributes) { - attributes.remove(new Attributes.Name(XJAR_ALGORITHM_KEY)); - attributes.remove(new Attributes.Name(XJAR_KEYSIZE_KEY)); - attributes.remove(new Attributes.Name(XJAR_IVSIZE_KEY)); - attributes.remove(new Attributes.Name(XJAR_PASSWORD_KEY)); - } - /** * 创建多个子过滤器AND连接的混合过滤器 * * @return 多个子过滤器AND连接的混合过滤器 */ - public static XAllEntryFilter all() { + public static XAllEntryFilter all() { return new XAllEntryFilter<>(); } @@ -291,7 +95,7 @@ public static XAllEntryFilter all() { * @param filters 子过滤器 * @return 多个子过滤器AND连接的混合过滤器 */ - public static XAllEntryFilter all(Collection> filters) { + public static XAllEntryFilter all(Collection> filters) { return new XAllEntryFilter<>(filters); } @@ -300,7 +104,7 @@ public static XAllEntryFilter all(Collection> f * * @return 多个子过滤器AND连接的混合过滤器 */ - public static XAllEntryFilter and() { + public static XAllEntryFilter and() { return new XAllEntryFilter<>(); } @@ -310,7 +114,7 @@ public static XAllEntryFilter and() { * @param filters 子过滤器 * @return 多个子过滤器AND连接的混合过滤器 */ - public static XAllEntryFilter and(Collection> filters) { + public static XAllEntryFilter and(Collection> filters) { return new XAllEntryFilter<>(filters); } @@ -319,7 +123,7 @@ public static XAllEntryFilter and(Collection> f * * @return 多个子过滤器OR连接的混合过滤器 */ - public static XAnyEntryFilter any() { + public static XAnyEntryFilter any() { return new XAnyEntryFilter<>(); } @@ -329,7 +133,7 @@ public static XAnyEntryFilter any() { * @param filters 子过滤器 * @return 多个子过滤器OR连接的混合过滤器 */ - public static XAnyEntryFilter any(Collection> filters) { + public static XAnyEntryFilter any(Collection> filters) { return new XAnyEntryFilter<>(filters); } @@ -338,7 +142,7 @@ public static XAnyEntryFilter any(Collection> f * * @return 多个子过滤器OR连接的混合过滤器 */ - public static XAnyEntryFilter or() { + public static XAnyEntryFilter or() { return new XAnyEntryFilter<>(); } @@ -348,7 +152,7 @@ public static XAnyEntryFilter or() { * @param filters 子过滤器 * @return 多个子过滤器OR连接的混合过滤器 */ - public static XAnyEntryFilter or(Collection> filters) { + public static XAnyEntryFilter or(Collection> filters) { return new XAnyEntryFilter<>(filters); } @@ -356,36 +160,30 @@ public static XAnyEntryFilter or(Collection> fi * 创建非门逻辑运算过滤器,实际上就是将委派过滤器的过滤结果取反 * * @param filter 委派过滤器 - * @param 记录类型 * @return 非门逻辑过滤器 */ - public static XEntryFilter not(XEntryFilter filter) { + public static XEntryFilter not(XEntryFilter filter) { return new XNotEntryFilter<>(filter); } - public static boolean isRelative(String path) { - return !isAbsolute(path); - } - - public static boolean isAbsolute(String path) { - if (path.startsWith("/")) { - return true; - } - Set roots = new HashSet<>(); - Collections.addAll(roots, File.listRoots()); - File root = new File(path); - while (root.getParentFile() != null) { - root = root.getParentFile(); - } - return roots.contains(root); - } - - public static String toAbsolute(String path) { - return toStandard(isAbsolute(path) ? path : System.getProperty("user.dir") + File.separator + path); + /** + * 创建JAR ANT 表达式过滤器 + * + * @param ant ANT 表达式 + * @return JAR ANT 表达式过滤器 + */ + public static XAntEntryFilter ant(String ant) { + return new XJarAntEntryFilter(ant); } - public static String toStandard(String path) { - return path.replaceAll("[/\\\\]+", "/"); + /** + * 创建JAR 正则表达式过滤器 + * + * @param regex 正则表达式 + * @return JAR 正则表达式过滤器 + */ + public static XRegexEntryFilter regex(String regex) { + return new XJarRegexEntryFilter(regex); } } diff --git a/src/main/java/io/xjar/XLauncher.java b/src/main/java/io/xjar/XLauncher.java index b197f74..f2cb1e0 100644 --- a/src/main/java/io/xjar/XLauncher.java +++ b/src/main/java/io/xjar/XLauncher.java @@ -77,7 +77,7 @@ public XLauncher(String... args) throws Exception { Properties key = null; File keyfile = null; if (keypath != null) { - String path = XKit.toAbsolute(keypath); + String path = XTool.toAbsolute(keypath); File file = new File(path); if (file.exists() && file.isFile()) { keyfile = file; @@ -89,7 +89,7 @@ public XLauncher(String... args) throws Exception { throw new FileNotFoundException("could not find key file at path: " + file.getCanonicalPath()); } } else { - String path = XKit.toAbsolute("xjar.key"); + String path = XTool.toAbsolute("xjar.key"); File file = new File(path); if (file.exists() && file.isFile()) { keyfile = file; diff --git a/src/main/java/io/xjar/XNopDecryptor.java b/src/main/java/io/xjar/XNopDecryptor.java index 1b5ef5d..bf6e513 100644 --- a/src/main/java/io/xjar/XNopDecryptor.java +++ b/src/main/java/io/xjar/XNopDecryptor.java @@ -29,7 +29,7 @@ public void decrypt(XKey key, File src, File dest) throws IOException { @Override public void decrypt(XKey key, InputStream in, OutputStream out) throws IOException { - XKit.transfer(in, out); + XTool.transfer(in, out); } @Override diff --git a/src/main/java/io/xjar/XNopEncryptor.java b/src/main/java/io/xjar/XNopEncryptor.java index 6e3fd32..17526b2 100644 --- a/src/main/java/io/xjar/XNopEncryptor.java +++ b/src/main/java/io/xjar/XNopEncryptor.java @@ -29,7 +29,7 @@ public void encrypt(XKey key, File src, File dest) throws IOException { @Override public void encrypt(XKey key, InputStream in, OutputStream out) throws IOException { - XKit.transfer(in, out); + XTool.transfer(in, out); } @Override diff --git a/src/main/java/io/xjar/XTool.java b/src/main/java/io/xjar/XTool.java new file mode 100644 index 0000000..3c8a2b3 --- /dev/null +++ b/src/main/java/io/xjar/XTool.java @@ -0,0 +1,243 @@ +package io.xjar; + +import java.io.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +/** + * XJar 工具类,包含I/O,密钥,过滤器的工具方法。 + */ +public abstract class XTool implements XConstants { + + /** + * 从输入流中读取一行字节码 + * + * @param in 输入流 + * @return 最前面的一行字节码 + * @throws IOException I/O 异常 + */ + public static byte[] readln(InputStream in) throws IOException { + int b = in.read(); + if (b == -1) { + return null; + } + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + while (b != -1) { + switch (b) { + case '\r': + break; + case '\n': + return bos.toByteArray(); + default: + bos.write(b); + break; + } + b = in.read(); + } + return bos.toByteArray(); + } + + /** + * 往输出流中写入一行字节码 + * + * @param out 输出流 + * @param line 一行字节码 + * @throws IOException I/O 异常 + */ + public static void writeln(OutputStream out, byte[] line) throws IOException { + if (line == null) { + return; + } + out.write(line); + out.write('\r'); + out.write('\n'); + } + + /** + * 关闭资源,等效于XKit.close(closeable, true); + * + * @param closeable 资源 + */ + public static void close(Closeable closeable) { + try { + close(closeable, true); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + /** + * 关闭资源 + * + * @param closeable 资源 + * @param quietly 是否安静关闭,即捕获到关闭异常时是否忽略 + * @throws IOException 当quietly == false, 时捕获到的I/O异常将会往外抛 + */ + public static void close(Closeable closeable, boolean quietly) throws IOException { + if (closeable == null) return; + try { + closeable.close(); + } catch (IOException e) { + if (!quietly) throw e; + } + } + + /** + * 输入流传输到输出流 + * + * @param in 输入流 + * @param out 输出流 + * @return 传输长度 + * @throws IOException I/O 异常 + */ + public static long transfer(InputStream in, OutputStream out) throws IOException { + long total = 0; + byte[] buffer = new byte[4096]; + int length; + while ((length = in.read(buffer)) != -1) { + out.write(buffer, 0, length); + total += length; + } + out.flush(); + return total; + } + + /** + * reader传输到writer + * + * @param reader reader + * @param writer writer + * @return 传输长度 + * @throws IOException I/O 异常 + */ + public static long transfer(Reader reader, Writer writer) throws IOException { + long total = 0; + char[] buffer = new char[4096]; + int length; + while ((length = reader.read(buffer)) != -1) { + writer.write(buffer, 0, length); + total += length; + } + writer.flush(); + return total; + } + + /** + * 输入流传输到文件 + * + * @param in 输入流 + * @param file 文件 + * @return 传输长度 + * @throws IOException I/O 异常 + */ + public static long transfer(InputStream in, File file) throws IOException { + OutputStream out = null; + try { + out = new FileOutputStream(file); + return transfer(in, out); + } finally { + close(out); + } + } + + /** + * reader传输到文件 + * + * @param reader reader + * @param file 文件 + * @return 传输长度 + * @throws IOException I/O 异常 + */ + public static long transfer(Reader reader, File file) throws IOException { + OutputStream out = null; + Writer writer = null; + try { + out = new FileOutputStream(file); + writer = new OutputStreamWriter(out); + return transfer(reader, writer); + } finally { + close(writer); + close(out); + } + } + + /** + * 删除文件,如果是目录将不递归删除子文件或目录,等效于delete(file, false); + * + * @param file 文件/目录 + * @return 是否删除成功 + */ + public static boolean delete(File file) { + return delete(file, false); + } + + /** + * 删除文件,如果是目录将递归删除子文件或目录 + * + * @param file 文件/目录 + * @return 是否删除成功 + */ + public static boolean delete(File file, boolean recursively) { + if (file.isDirectory() && recursively) { + boolean deleted = true; + File[] files = file.listFiles(); + for (int i = 0; files != null && i < files.length; i++) { + deleted &= delete(files[i], true); + } + return deleted && file.delete(); + } else { + return file.delete(); + } + } + + /** + * 判断路径是否为相对路径 + * + * @param path 路径 + * @return {@code true}: 是相对路径,{@code false}: 非相对路径 + */ + public static boolean isRelative(String path) { + return !isAbsolute(path); + } + + /** + * 判断路径是否为绝对路径 + * + * @param path 路径 + * @return {@code true}: 是绝对路径,{@code false}: 非绝对路径 + */ + public static boolean isAbsolute(String path) { + if (path.startsWith("/")) { + return true; + } + Set roots = new HashSet<>(); + Collections.addAll(roots, File.listRoots()); + File root = new File(path); + while (root.getParentFile() != null) { + root = root.getParentFile(); + } + return roots.contains(root); + } + + /** + * 转换成绝对路径 + * + * @param path 路径 + * @return 绝对路径 + */ + public static String toAbsolute(String path) { + return toStandard(isAbsolute(path) ? path : System.getProperty("user.dir") + File.separator + path); + } + + /** + * 转换标准路径 + * + * @param path 路径 + * @return 标准路径 + */ + public static String toStandard(String path) { + return path.replaceAll("[/\\\\]+", "/"); + } + +} diff --git a/src/main/java/io/xjar/boot/XBoot.java b/src/main/java/io/xjar/boot/XBoot.java index 87a3a29..4b143ce 100644 --- a/src/main/java/io/xjar/boot/XBoot.java +++ b/src/main/java/io/xjar/boot/XBoot.java @@ -1,6 +1,6 @@ package io.xjar.boot; -import io.xjar.XConstants; +import io.xjar.XKit; /** * Spring-Boot JAR包加解密工具类,在不提供过滤器的情况下会加密BOOT-INF/下的所有资源,及包括项目本身的资源和依赖jar资源。 @@ -8,6 +8,23 @@ * @author Payne 646742615@qq.com * 2018/11/26 11:11 */ -public class XBoot implements XConstants { +public class XBoot extends XKit { + /** + * 获取加密器建造器 + * + * @return 加密器建造器 + */ + public static XBootEncryptor.XBootEncryptorBuilder encryptor() { + return new XBootEncryptor.XBootEncryptorBuilder(); + } + + /** + * 获取解密器建造器 + * + * @return 解密器建造器 + */ + public static XBootDecryptor.XBootDecryptorBuilder decryptor() { + return new XBootDecryptor.XBootDecryptorBuilder(); + } } diff --git a/src/main/java/io/xjar/boot/XBootClassLoader.java b/src/main/java/io/xjar/boot/XBootClassLoader.java index aa2be80..b2ecb02 100644 --- a/src/main/java/io/xjar/boot/XBootClassLoader.java +++ b/src/main/java/io/xjar/boot/XBootClassLoader.java @@ -2,7 +2,7 @@ import io.xjar.XDecryptor; import io.xjar.XEncryptor; -import io.xjar.XKit; +import io.xjar.XTool; import io.xjar.key.XKey; import org.springframework.boot.loader.LaunchedURLClassLoader; @@ -64,7 +64,7 @@ protected Class findClass(String name) throws ClassNotFoundException { } try (InputStream in = resource.openStream()) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XKit.transfer(in, bos); + XTool.transfer(in, bos); byte[] bytes = bos.toByteArray(); return defineClass(name, bytes, 0, bytes.length); } catch (Throwable t) { diff --git a/src/main/java/io/xjar/boot/XBootDecryptor.java b/src/main/java/io/xjar/boot/XBootDecryptor.java index c81aed1..f8d3202 100644 --- a/src/main/java/io/xjar/boot/XBootDecryptor.java +++ b/src/main/java/io/xjar/boot/XBootDecryptor.java @@ -63,7 +63,6 @@ else if (entry.getName().equals(META_INF_MANIFEST)) { attributes.putValue("Main-Class", mainClass); attributes.remove(new Attributes.Name("Boot-Main-Class")); } - XKit.removeKey(attributes); JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); jarArchiveEntry.setTime(entry.getTime()); zos.putArchiveEntry(jarArchiveEntry); @@ -78,7 +77,7 @@ else if (entry.getName().startsWith(BOOT_INF_CLASSES)) { boolean filtered = filtrate(xBootJarArchiveEntry); XDecryptor decryptor = filtered ? xDecryptor : xNopDecryptor; try (OutputStream eos = decryptor.decrypt(key, nos)) { - XKit.transfer(nis, eos); + XTool.transfer(nis, eos); } } // BOOT-INF/lib/** @@ -93,22 +92,22 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { jar.setCrc(cos.getChecksum().getValue()); zos.putArchiveEntry(jar); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - XKit.transfer(bis, nos); + XTool.transfer(bis, nos); } // OTHER else { JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); jarArchiveEntry.setTime(entry.getTime()); zos.putArchiveEntry(jarArchiveEntry); - XKit.transfer(nis, nos); + XTool.transfer(nis, nos); } zos.closeArchiveEntry(); } zos.finish(); } finally { - XKit.close(zis); - XKit.close(zos); + XTool.close(zis); + XTool.close(zos); } } diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index c8b579b..98863a6 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -97,7 +97,7 @@ else if (entry.getName().startsWith(BOOT_INF_CLASSES)) { } XEncryptor encryptor = filtered ? xEncryptor : xNopEncryptor; try (OutputStream eos = encryptor.encrypt(key, nos)) { - XKit.transfer(nis, eos); + XTool.transfer(nis, eos); } } // BOOT-INF/lib/** @@ -112,14 +112,14 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { jar.setCrc(cos.getChecksum().getValue()); zos.putArchiveEntry(jar); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - XKit.transfer(bis, nos); + XTool.transfer(bis, nos); } // OTHER else { JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); jarArchiveEntry.setTime(entry.getTime()); zos.putArchiveEntry(jarArchiveEntry); - XKit.transfer(nis, nos); + XTool.transfer(nis, nos); } zos.closeArchiveEntry(); } @@ -147,8 +147,8 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { zos.finish(); } finally { - XKit.close(zis); - XKit.close(zos); + XTool.close(zis); + XTool.close(zos); } } diff --git a/src/main/java/io/xjar/jar/XJar.java b/src/main/java/io/xjar/jar/XJar.java index 18e6e4d..faa3c61 100644 --- a/src/main/java/io/xjar/jar/XJar.java +++ b/src/main/java/io/xjar/jar/XJar.java @@ -1,6 +1,6 @@ package io.xjar.jar; -import io.xjar.XConstants; +import io.xjar.XKit; /** * 普通JAR包加解密工具类 @@ -8,6 +8,24 @@ * @author Payne 646742615@qq.com * 2018/11/26 11:11 */ -public class XJar implements XConstants { +public class XJar extends XKit { + + /** + * 获取加密器建造器 + * + * @return 加密器建造器 + */ + public static XJarEncryptor.XJarEncryptorBuilder encryptor() { + return new XJarEncryptor.XJarEncryptorBuilder(); + } + + /** + * 获取解密器建造器 + * + * @return 解密器建造器 + */ + public static XJarDecryptor.XJarDecryptorBuilder decryptor() { + return new XJarDecryptor.XJarDecryptorBuilder(); + } } diff --git a/src/main/java/io/xjar/jar/XJarClassLoader.java b/src/main/java/io/xjar/jar/XJarClassLoader.java index 6c329c0..3f0afd8 100644 --- a/src/main/java/io/xjar/jar/XJarClassLoader.java +++ b/src/main/java/io/xjar/jar/XJarClassLoader.java @@ -2,7 +2,7 @@ import io.xjar.XDecryptor; import io.xjar.XEncryptor; -import io.xjar.XKit; +import io.xjar.XTool; import io.xjar.key.XKey; import java.io.ByteArrayOutputStream; @@ -64,7 +64,7 @@ protected Class findClass(String name) throws ClassNotFoundException { } try (InputStream in = resource.openStream()) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XKit.transfer(in, bos); + XTool.transfer(in, bos); byte[] bytes = bos.toByteArray(); return defineClass(name, bytes, 0, bytes.length); } catch (Throwable t) { diff --git a/src/main/java/io/xjar/jar/XJarDecryptor.java b/src/main/java/io/xjar/jar/XJarDecryptor.java index 28867cc..d8a7485 100644 --- a/src/main/java/io/xjar/jar/XJarDecryptor.java +++ b/src/main/java/io/xjar/jar/XJarDecryptor.java @@ -57,7 +57,6 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti attributes.putValue("Main-Class", mainClass); attributes.remove(new Attributes.Name("Jar-Main-Class")); } - XKit.removeKey(attributes); JarArchiveEntry jarArchiveEntry = new JarArchiveEntry(entry.getName()); jarArchiveEntry.setTime(entry.getTime()); zos.putArchiveEntry(jarArchiveEntry); @@ -69,7 +68,7 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti boolean filtered = filtrate(entry); XDecryptor decryptor = filtered ? xDecryptor : xNopDecryptor; try (OutputStream eos = decryptor.decrypt(key, nos)) { - XKit.transfer(nis, eos); + XTool.transfer(nis, eos); } } zos.closeArchiveEntry(); @@ -77,8 +76,8 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.finish(); } finally { - XKit.close(zis); - XKit.close(zos); + XTool.close(zis); + XTool.close(zos); } } diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 27817c7..32f3e37 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -75,7 +75,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti } XEncryptor encryptor = filtered ? xEncryptor : xNopEncryptor; try (OutputStream eos = encryptor.encrypt(key, nos)) { - XKit.transfer(nis, eos); + XTool.transfer(nis, eos); } } zos.closeArchiveEntry(); @@ -104,8 +104,8 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.finish(); } finally { - XKit.close(zis); - XKit.close(zos); + XTool.close(zis); + XTool.close(zos); } } diff --git a/src/main/java/io/xjar/key/XKey.java b/src/main/java/io/xjar/key/XKey.java index 3e3601e..48f9260 100644 --- a/src/main/java/io/xjar/key/XKey.java +++ b/src/main/java/io/xjar/key/XKey.java @@ -1,12 +1,14 @@ package io.xjar.key; +import io.xjar.XConstants; + /** * 密钥 * * @author 杨昌沛 646742615@qq.com * 2018-11-22 14:54:10 */ -public interface XKey { +public interface XKey extends XConstants { /** * @return 密钥算法名称 diff --git a/src/main/java/io/xjar/zip/XZipDecryptor.java b/src/main/java/io/xjar/zip/XZipDecryptor.java index a3169fe..1e41847 100644 --- a/src/main/java/io/xjar/zip/XZipDecryptor.java +++ b/src/main/java/io/xjar/zip/XZipDecryptor.java @@ -54,14 +54,14 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.putArchiveEntry(new ZipArchiveEntry(entry.getName())); XDecryptor decryptor = filtrate(entry) ? this : xNopDecryptor; try (OutputStream eos = decryptor.decrypt(key, nos)) { - XKit.transfer(zis, eos); + XTool.transfer(zis, eos); } zos.closeArchiveEntry(); } zos.finish(); } finally { - XKit.close(zis); - XKit.close(zos); + XTool.close(zis); + XTool.close(zos); } } diff --git a/src/main/java/io/xjar/zip/XZipEncryptor.java b/src/main/java/io/xjar/zip/XZipEncryptor.java index fd0cbe9..460a850 100644 --- a/src/main/java/io/xjar/zip/XZipEncryptor.java +++ b/src/main/java/io/xjar/zip/XZipEncryptor.java @@ -54,14 +54,14 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.putArchiveEntry(new ZipArchiveEntry(entry.getName())); XEncryptor encryptor = filtrate(entry) ? this : xNopEncryptor; try (OutputStream eos = encryptor.encrypt(key, nos)) { - XKit.transfer(zis, eos); + XTool.transfer(zis, eos); } zos.closeArchiveEntry(); } zos.finish(); } finally { - XKit.close(zis); - XKit.close(zos); + XTool.close(zis); + XTool.close(zos); } } } diff --git a/src/test/java/test/io/xjar/XJarTest.java b/src/test/java/test/io/xjar/XJarTest.java index bcd5abc..ef8bbed 100644 --- a/src/test/java/test/io/xjar/XJarTest.java +++ b/src/test/java/test/io/xjar/XJarTest.java @@ -1,6 +1,6 @@ package test.io.xjar; -import io.xjar.XKit; +import io.xjar.XTool; import io.xjar.jar.XJarEncryptor; import org.junit.Test; @@ -11,11 +11,11 @@ public class XJarTest { @Test - public void test() throws Exception { + public void test() { XJarEncryptor.builder() .build() .encrypt( - XKit.key("io.xjar"), + XTool.key("io.xjar"), "C:\\Users\\Chang\\Downloads\\Android-SDK\\target\\nwhs-sdk-v5.3.0-alpha.2.jar", "C:\\Users\\Chang\\Downloads\\Android-SDK\\target\\nwhs-sdk-v5.3.0-alpha.2.xjar" ); From 21151d19d63ed07a0ba80e9b063544456cef1c3e Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Mon, 8 Jul 2019 15:57:31 +0800 Subject: [PATCH 15/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/boot/XBoot.java | 4 ++-- src/main/java/io/xjar/jar/XJar.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/xjar/boot/XBoot.java b/src/main/java/io/xjar/boot/XBoot.java index 4b143ce..da51afc 100644 --- a/src/main/java/io/xjar/boot/XBoot.java +++ b/src/main/java/io/xjar/boot/XBoot.java @@ -16,7 +16,7 @@ public class XBoot extends XKit { * @return 加密器建造器 */ public static XBootEncryptor.XBootEncryptorBuilder encryptor() { - return new XBootEncryptor.XBootEncryptorBuilder(); + return XBootEncryptor.builder(); } /** @@ -25,6 +25,6 @@ public static XBootEncryptor.XBootEncryptorBuilder encryptor() { * @return 解密器建造器 */ public static XBootDecryptor.XBootDecryptorBuilder decryptor() { - return new XBootDecryptor.XBootDecryptorBuilder(); + return XBootDecryptor.builder(); } } diff --git a/src/main/java/io/xjar/jar/XJar.java b/src/main/java/io/xjar/jar/XJar.java index faa3c61..cd2d720 100644 --- a/src/main/java/io/xjar/jar/XJar.java +++ b/src/main/java/io/xjar/jar/XJar.java @@ -16,7 +16,7 @@ public class XJar extends XKit { * @return 加密器建造器 */ public static XJarEncryptor.XJarEncryptorBuilder encryptor() { - return new XJarEncryptor.XJarEncryptorBuilder(); + return XJarEncryptor.builder(); } /** @@ -25,7 +25,7 @@ public static XJarEncryptor.XJarEncryptorBuilder encryptor() { * @return 解密器建造器 */ public static XJarDecryptor.XJarDecryptorBuilder decryptor() { - return new XJarDecryptor.XJarDecryptorBuilder(); + return XJarDecryptor.builder(); } } From 4348205e73f8cdd3c5594a26f16e56419665a2fc Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 9 Jul 2019 11:16:21 +0800 Subject: [PATCH 16/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XDigestFactory.java | 11 +++- .../java/io/xjar/boot/XBootEncryptor.java | 23 +++++++-- .../io/xjar/digest/XJdkDigestFactory.java | 4 +- .../xjar/digest/XRecycledDigestFactory.java | 51 +++++++++++++++++++ src/main/java/io/xjar/jar/XJarEncryptor.java | 31 +++++++++-- 5 files changed, 111 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/xjar/digest/XRecycledDigestFactory.java diff --git a/src/main/java/io/xjar/XDigestFactory.java b/src/main/java/io/xjar/XDigestFactory.java index 8e2b028..e028e17 100644 --- a/src/main/java/io/xjar/XDigestFactory.java +++ b/src/main/java/io/xjar/XDigestFactory.java @@ -15,7 +15,16 @@ public interface XDigestFactory { * * @param algorithm 摘要算法 * @return 摘要算法对象 + * @throws NoSuchAlgorithmException 摘要算法不支持 */ - XDigest produce(String algorithm) throws NoSuchAlgorithmException; + XDigest acquire(String algorithm) throws NoSuchAlgorithmException; + + /** + * 回收摘要算法对象 + * + * @param algorithm 摘要算法 + * @param digest 摘要算法对象 + */ + void release(String algorithm, XDigest digest); } diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index 98863a6..e94d078 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -1,6 +1,7 @@ package io.xjar.boot; import io.xjar.*; +import io.xjar.digest.XJdkDigestFactory; import io.xjar.jar.XJarAllEntryFilter; import io.xjar.jar.XJarEncryptor; import io.xjar.key.XKey; @@ -38,10 +39,14 @@ public class XBootEncryptor extends XArchiveEncryptor implement } private final int level; + private final XDigestFactory digestFactory; + private final String digestAlgorithm; - public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { + public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, XDigestFactory digestFactory, String digestAlgorithm) { super(xEncryptor, filter); this.level = level; + this.digestFactory = digestFactory; + this.digestAlgorithm = digestAlgorithm; } @Override @@ -55,7 +60,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); - XJarEncryptor xJarEncryptor = new XJarEncryptor(xEncryptor, filter, level); + XJarEncryptor xJarEncryptor = new XJarEncryptor(xEncryptor, filter, level, digestFactory, digestAlgorithm); JarArchiveEntry entry; Manifest manifest = null; while ((entry = zis.getNextJarEntry()) != null) { @@ -158,6 +163,8 @@ public static XBootEncryptorBuilder builder() { public static class XBootEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; + private XDigestFactory digestFactory = new XJdkDigestFactory(); + private String digestAlgorithm = "MD5"; { encryptor(new XSmtEncryptor()); @@ -169,9 +176,19 @@ public XBootEncryptorBuilder level(int level) { return this; } + public XBootEncryptorBuilder digestFactory(XDigestFactory digestFactory) { + this.digestFactory = digestFactory; + return this; + } + + public XBootEncryptorBuilder digestAlgorithm(String digestAlgorithm) { + this.digestAlgorithm = digestAlgorithm; + return this; + } + @Override public XBootEncryptor build() { - return new XBootEncryptor(encryptor, filter, level); + return new XBootEncryptor(encryptor, filter, level, digestFactory, digestAlgorithm); } } } diff --git a/src/main/java/io/xjar/digest/XJdkDigestFactory.java b/src/main/java/io/xjar/digest/XJdkDigestFactory.java index c04f0a9..d5df286 100644 --- a/src/main/java/io/xjar/digest/XJdkDigestFactory.java +++ b/src/main/java/io/xjar/digest/XJdkDigestFactory.java @@ -11,10 +11,10 @@ * @author Payne 646742615@qq.com * 2019/7/5 10:22 */ -public class XJdkDigestFactory implements XDigestFactory { +public class XJdkDigestFactory extends XRecycledDigestFactory implements XDigestFactory { @Override - public XDigest produce(String algorithm) throws NoSuchAlgorithmException { + protected XDigest produce(String algorithm) throws NoSuchAlgorithmException { return new XJdkDigest(algorithm); } } diff --git a/src/main/java/io/xjar/digest/XRecycledDigestFactory.java b/src/main/java/io/xjar/digest/XRecycledDigestFactory.java new file mode 100644 index 0000000..e981c16 --- /dev/null +++ b/src/main/java/io/xjar/digest/XRecycledDigestFactory.java @@ -0,0 +1,51 @@ +package io.xjar.digest; + +import io.xjar.XDigest; +import io.xjar.XDigestFactory; + +import java.security.NoSuchAlgorithmException; +import java.util.Queue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.ConcurrentMap; + +/** + * 重复利用的摘要算法对象工厂 + * + * @author Payne 646742615@qq.com + * 2019/7/9 10:50 + */ +public abstract class XRecycledDigestFactory implements XDigestFactory { + private final ConcurrentMap> pool = new ConcurrentHashMap<>(); + + /** + * 生产一个新的摘要算法对象 + * + * @param algorithm 摘要算法 + * @return 摘要算法对象 + * @throws NoSuchAlgorithmException 摘要算法不支持 + */ + protected abstract XDigest produce(String algorithm) throws NoSuchAlgorithmException; + + @Override + public XDigest acquire(String algorithm) throws NoSuchAlgorithmException { + Queue queue = pool.get(algorithm); + XDigest digest = queue != null ? queue.poll() : null; + if (digest == null) { + digest = produce(algorithm); + } + digest.resume(); + return digest; + } + + @Override + public void release(String algorithm, XDigest digest) { + digest.resume(); + Queue queue = new ConcurrentLinkedQueue<>(); + queue.offer(digest); + Queue old = pool.putIfAbsent(algorithm, queue); + if (old != null) { + old.offer(digest); + } + } +} diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 32f3e37..90a88e1 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -1,6 +1,7 @@ package io.xjar.jar; import io.xjar.*; +import io.xjar.digest.XJdkDigestFactory; import io.xjar.key.XKey; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; @@ -23,10 +24,14 @@ */ public class XJarEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final int level; + private final XDigestFactory digestFactory; + private final String digestAlgorithm; - public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { + public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, XDigestFactory digestFactory, String digestAlgorithm) { super(xEncryptor, filter); this.level = level; + this.digestFactory = digestFactory; + this.digestAlgorithm = digestAlgorithm; } @Override @@ -36,7 +41,8 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti Set indexes = new LinkedHashSet<>(); try { zis = new JarArchiveInputStream(in); - zos = new JarArchiveOutputStream(out); + XDigest xDigest = digestFactory.acquire(digestAlgorithm); + zos = new XDigestedOutputStream(out, xDigest); zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); @@ -102,7 +108,14 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti XInjector.inject(zos, "io/xjar/**"); } + byte[] digest = xDigest.finish(); + System.out.write(digest); + zos.finish(); + } catch (IOException ex) { + throw ex; + } catch (Exception ex) { + throw new IOException(ex); } finally { XTool.close(zis); XTool.close(zos); @@ -115,6 +128,8 @@ public static XJarEncryptorBuilder builder() { public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; + private XDigestFactory digestFactory = new XJdkDigestFactory(); + private String digestAlgorithm = "MD5"; { encryptor(new XSmtEncryptor()); @@ -126,9 +141,19 @@ public XJarEncryptorBuilder level(int level) { return this; } + public XJarEncryptorBuilder digestFactory(XDigestFactory digestFactory) { + this.digestFactory = digestFactory; + return this; + } + + public XJarEncryptorBuilder digestAlgorithm(String digestAlgorithm) { + this.digestAlgorithm = digestAlgorithm; + return this; + } + @Override public XJarEncryptor build() { - return new XJarEncryptor(encryptor, filter, level); + return new XJarEncryptor(encryptor, filter, level, digestFactory, digestAlgorithm); } } } From 07c95f33bf233b1acc6260976a69aa7ddd9792f3 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 9 Jul 2019 17:13:55 +0800 Subject: [PATCH 17/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/jar/XJarEncryptor.java | 31 ++------------------ 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 90a88e1..32f3e37 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -1,7 +1,6 @@ package io.xjar.jar; import io.xjar.*; -import io.xjar.digest.XJdkDigestFactory; import io.xjar.key.XKey; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; @@ -24,14 +23,10 @@ */ public class XJarEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final int level; - private final XDigestFactory digestFactory; - private final String digestAlgorithm; - public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, XDigestFactory digestFactory, String digestAlgorithm) { + public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { super(xEncryptor, filter); this.level = level; - this.digestFactory = digestFactory; - this.digestAlgorithm = digestAlgorithm; } @Override @@ -41,8 +36,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti Set indexes = new LinkedHashSet<>(); try { zis = new JarArchiveInputStream(in); - XDigest xDigest = digestFactory.acquire(digestAlgorithm); - zos = new XDigestedOutputStream(out, xDigest); + zos = new JarArchiveOutputStream(out); zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); @@ -108,14 +102,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti XInjector.inject(zos, "io/xjar/**"); } - byte[] digest = xDigest.finish(); - System.out.write(digest); - zos.finish(); - } catch (IOException ex) { - throw ex; - } catch (Exception ex) { - throw new IOException(ex); } finally { XTool.close(zis); XTool.close(zos); @@ -128,8 +115,6 @@ public static XJarEncryptorBuilder builder() { public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; - private XDigestFactory digestFactory = new XJdkDigestFactory(); - private String digestAlgorithm = "MD5"; { encryptor(new XSmtEncryptor()); @@ -141,19 +126,9 @@ public XJarEncryptorBuilder level(int level) { return this; } - public XJarEncryptorBuilder digestFactory(XDigestFactory digestFactory) { - this.digestFactory = digestFactory; - return this; - } - - public XJarEncryptorBuilder digestAlgorithm(String digestAlgorithm) { - this.digestAlgorithm = digestAlgorithm; - return this; - } - @Override public XJarEncryptor build() { - return new XJarEncryptor(encryptor, filter, level, digestFactory, digestAlgorithm); + return new XJarEncryptor(encryptor, filter, level); } } } From 9e625c4a152a96e19e79fc83555a39eaee0ed42b Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Wed, 10 Jul 2019 17:02:03 +0800 Subject: [PATCH 18/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XLauncher.java | 134 +----------------- .../java/io/xjar/boot/XBootEncryptor.java | 23 +-- src/main/java/io/xjar/jar/XJarEncryptor.java | 50 ++++++- src/main/java/io/xjar/jni/XJni.java | 105 ++++++++++++++ src/main/resources/io/xjar/jni/XJni.cpp | 10 ++ src/main/resources/io/xjar/jni/XJni.h | 21 +++ src/test/java/test/io/xjar/XJarTest.java | 49 +++++-- 7 files changed, 234 insertions(+), 158 deletions(-) create mode 100644 src/main/java/io/xjar/jni/XJni.java create mode 100644 src/main/resources/io/xjar/jni/XJni.cpp create mode 100644 src/main/resources/io/xjar/jni/XJni.h diff --git a/src/main/java/io/xjar/XLauncher.java b/src/main/java/io/xjar/XLauncher.java index f2cb1e0..bcaf918 100644 --- a/src/main/java/io/xjar/XLauncher.java +++ b/src/main/java/io/xjar/XLauncher.java @@ -1,19 +1,8 @@ package io.xjar; +import io.xjar.jni.XJni; import io.xjar.key.XKey; -import java.io.*; -import java.net.URI; -import java.security.CodeSource; -import java.security.ProtectionDomain; -import java.util.Arrays; -import java.util.Properties; -import java.util.Scanner; -import java.util.Set; -import java.util.jar.Attributes; -import java.util.jar.JarFile; -import java.util.jar.Manifest; - /** * Spring-Boot 启动器 * @@ -26,125 +15,14 @@ public class XLauncher implements XConstants { public final XEncryptor xEncryptor; public final XKey xKey; - public XLauncher(String... args) throws Exception { + public XLauncher(String... args) { this.args = args; - String algorithm = DEFAULT_ALGORITHM; - int keysize = DEFAULT_KEYSIZE; - int ivsize = DEFAULT_IVSIZE; - String password = null; - String keypath = null; - for (String arg : args) { - if (arg.toLowerCase().startsWith(XJAR_ALGORITHM)) { - algorithm = arg.substring(XJAR_ALGORITHM.length()); - } - if (arg.toLowerCase().startsWith(XJAR_KEYSIZE)) { - keysize = Integer.valueOf(arg.substring(XJAR_KEYSIZE.length())); - } - if (arg.toLowerCase().startsWith(XJAR_IVSIZE)) { - ivsize = Integer.valueOf(arg.substring(XJAR_IVSIZE.length())); - } - if (arg.toLowerCase().startsWith(XJAR_PASSWORD)) { - password = arg.substring(XJAR_PASSWORD.length()); - } - if (arg.toLowerCase().startsWith(XJAR_KEYFILE)) { - keypath = arg.substring(XJAR_KEYFILE.length()); - } - } - - ProtectionDomain domain = this.getClass().getProtectionDomain(); - CodeSource source = domain.getCodeSource(); - URI location = (source == null ? null : source.getLocation().toURI()); - String filepath = (location == null ? null : location.getSchemeSpecificPart()); - if (filepath != null) { - File file = new File(filepath); - JarFile jar = new JarFile(file, false); - Manifest manifest = jar.getManifest(); - Attributes attributes = manifest.getMainAttributes(); - if (attributes.getValue(XJAR_ALGORITHM_KEY) != null) { - algorithm = attributes.getValue(XJAR_ALGORITHM_KEY); - } - if (attributes.getValue(XJAR_KEYSIZE_KEY) != null) { - keysize = Integer.valueOf(attributes.getValue(XJAR_KEYSIZE_KEY)); - } - if (attributes.getValue(XJAR_IVSIZE_KEY) != null) { - ivsize = Integer.valueOf(attributes.getValue(XJAR_IVSIZE_KEY)); - } - if (attributes.getValue(XJAR_PASSWORD_KEY) != null) { - password = attributes.getValue(XJAR_PASSWORD_KEY); - } - } - - Properties key = null; - File keyfile = null; - if (keypath != null) { - String path = XTool.toAbsolute(keypath); - File file = new File(path); - if (file.exists() && file.isFile()) { - keyfile = file; - try (InputStream in = new FileInputStream(file)) { - key = new Properties(); - key.load(in); - } - } else { - throw new FileNotFoundException("could not find key file at path: " + file.getCanonicalPath()); - } - } else { - String path = XTool.toAbsolute("xjar.key"); - File file = new File(path); - if (file.exists() && file.isFile()) { - keyfile = file; - try (InputStream in = new FileInputStream(file)) { - key = new Properties(); - key.load(in); - } - } - } - - String hold = null; - if (key != null) { - Set names = key.stringPropertyNames(); - for (String name : names) { - switch (name.toLowerCase()) { - case XJAR_KEY_ALGORITHM: - algorithm = key.getProperty(name); - break; - case XJAR_KEY_KEYSIZE: - keysize = Integer.valueOf(key.getProperty(name)); - break; - case XJAR_KEY_IVSIZE: - ivsize = Integer.valueOf(key.getProperty(name)); - break; - case XJAR_KEY_PASSWORD: - password = key.getProperty(name); - break; - case XJAR_KEY_HOLD: - hold = key.getProperty(name); - default: - break; - } - } - } - - // 不保留密钥文件 - if (hold == null || !Arrays.asList("true", "1", "yes", "y").contains(hold.trim().toLowerCase())) { - if (keyfile != null && keyfile.exists() && !keyfile.delete() && keyfile.exists()) { - throw new IOException("could not delete key file: " + keyfile.getCanonicalPath()); - } - } - - if (password == null && System.console() != null) { - Console console = System.console(); - char[] chars = console.readPassword("password:"); - password = new String(chars); - } - if (password == null) { - System.out.print("password:"); - Scanner scanner = new Scanner(System.in); - password = scanner.nextLine(); - } + XJni xJni = XJni.getInstance(); + XKey xKey = xJni.key(); + String algorithm = xKey.getAlgorithm(); this.xDecryptor = new XJdkDecryptor(algorithm); this.xEncryptor = new XJdkEncryptor(algorithm); - this.xKey = XKit.key(algorithm, keysize, ivsize, password); + this.xKey = xKey; } } diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index e94d078..21c039b 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -1,7 +1,6 @@ package io.xjar.boot; import io.xjar.*; -import io.xjar.digest.XJdkDigestFactory; import io.xjar.jar.XJarAllEntryFilter; import io.xjar.jar.XJarEncryptor; import io.xjar.key.XKey; @@ -39,14 +38,10 @@ public class XBootEncryptor extends XArchiveEncryptor implement } private final int level; - private final XDigestFactory digestFactory; - private final String digestAlgorithm; - public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, XDigestFactory digestFactory, String digestAlgorithm) { + public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { super(xEncryptor, filter); this.level = level; - this.digestFactory = digestFactory; - this.digestAlgorithm = digestAlgorithm; } @Override @@ -60,7 +55,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); - XJarEncryptor xJarEncryptor = new XJarEncryptor(xEncryptor, filter, level, digestFactory, digestAlgorithm); + XJarEncryptor xJarEncryptor = XJarEncryptor.builder().build(); JarArchiveEntry entry; Manifest manifest = null; while ((entry = zis.getNextJarEntry()) != null) { @@ -163,8 +158,6 @@ public static XBootEncryptorBuilder builder() { public static class XBootEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; - private XDigestFactory digestFactory = new XJdkDigestFactory(); - private String digestAlgorithm = "MD5"; { encryptor(new XSmtEncryptor()); @@ -176,19 +169,9 @@ public XBootEncryptorBuilder level(int level) { return this; } - public XBootEncryptorBuilder digestFactory(XDigestFactory digestFactory) { - this.digestFactory = digestFactory; - return this; - } - - public XBootEncryptorBuilder digestAlgorithm(String digestAlgorithm) { - this.digestAlgorithm = digestAlgorithm; - return this; - } - @Override public XBootEncryptor build() { - return new XBootEncryptor(encryptor, filter, level, digestFactory, digestAlgorithm); + return new XBootEncryptor(encryptor, filter, level); } } } diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 32f3e37..980f4c0 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -1,11 +1,14 @@ package io.xjar.jar; import io.xjar.*; +import io.xjar.digest.XJdkDigestFactory; +import io.xjar.jni.XJni; import io.xjar.key.XKey; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -23,10 +26,20 @@ */ public class XJarEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final int level; + private final String jdkLocation; + private final String gccLocation; + private final String tmpLocation; + private final XDigestFactory digestFactory; + private final String digestAlgorithm; - public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { + public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, String jdkLocation, String gccLocation, String tmpLocation, XDigestFactory digestFactory, String digestAlgorithm) { super(xEncryptor, filter); this.level = level; + this.jdkLocation = jdkLocation; + this.gccLocation = gccLocation; + this.tmpLocation = tmpLocation; + this.digestFactory = digestFactory; + this.digestAlgorithm = digestAlgorithm; } @Override @@ -100,6 +113,9 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti String mainClass = manifest != null && manifest.getMainAttributes() != null ? manifest.getMainAttributes().getValue("Main-Class") : null; if (mainClass != null) { XInjector.inject(zos, "io/xjar/**"); + XJni xJni = XJni.getInstance(); + xJni.compile(key); + } zos.finish(); @@ -115,6 +131,11 @@ public static XJarEncryptorBuilder builder() { public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; + private String jdkLocation = new File(System.getProperty("java.home")).getParent(); + private String gccLocation = "g++"; + private String tmpLocation = System.getProperty("java.io.tmpdir"); + private XDigestFactory digestFactory = new XJdkDigestFactory(); + private String digestAlgorithm = "MD5"; { encryptor(new XSmtEncryptor()); @@ -126,9 +147,34 @@ public XJarEncryptorBuilder level(int level) { return this; } + public XJarEncryptorBuilder jdkLocation(String jdkLocation) { + this.jdkLocation = jdkLocation; + return this; + } + + public XJarEncryptorBuilder gccLocation(String gccLocation) { + this.gccLocation = gccLocation; + return this; + } + + public XJarEncryptorBuilder tmpLocation(String tmpLocation) { + this.tmpLocation = tmpLocation; + return this; + } + + public XJarEncryptorBuilder digestFactory(XDigestFactory digestFactory) { + this.digestFactory = digestFactory; + return this; + } + + public XJarEncryptorBuilder digestAlgorithm(String digestAlgorithm) { + this.digestAlgorithm = digestAlgorithm; + return this; + } + @Override public XJarEncryptor build() { - return new XJarEncryptor(encryptor, filter, level); + return new XJarEncryptor(encryptor, filter, level, jdkLocation, gccLocation, tmpLocation, digestFactory, digestAlgorithm); } } } diff --git a/src/main/java/io/xjar/jni/XJni.java b/src/main/java/io/xjar/jni/XJni.java new file mode 100644 index 0000000..2b18fc1 --- /dev/null +++ b/src/main/java/io/xjar/jni/XJni.java @@ -0,0 +1,105 @@ +package io.xjar.jni; + +import io.xjar.XTool; +import io.xjar.key.XKey; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * JNI + * + * @author Payne 646742615@qq.com + * 2019/7/10 13:19 + */ +public class XJni { + private static volatile XJni instance; + + static { + System.load("C:\\Users\\Chang\\source\\repos\\Project1\\XJni.so"); + } + + private XJni() { + + } + + /** + * 获取单例对象 + * + * @return 单例对象 + */ + public static XJni getInstance() { + if (instance != null) { + return instance; + } + synchronized (XJni.class) { + if (instance != null) { + return instance; + } + instance = new XJni(); + } + return instance; + } + + /** + * 编码 + * + * @param xKey 密钥 + * @return 密钥编码数据 + */ + public byte[] encode(XKey xKey) { + ByteArrayOutputStream bos = null; + ObjectOutputStream oos = null; + try { + bos = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bos); + oos.writeObject(xKey); + return bos.toByteArray(); + } catch (Exception ex) { + throw new RuntimeException(ex); + } finally { + XTool.close(oos); + XTool.close(bos); + } + } + + /** + * 解码 + * + * @param xKey 密钥编码数据 + * @return 密钥 + */ + public XKey decode(byte[] xKey) { + ByteArrayInputStream bis = null; + ObjectInputStream ois = null; + try { + bis = new ByteArrayInputStream(xKey); + ois = new ObjectInputStream(bis); + Object xObj = ois.readObject(); + return (XKey) xObj; + } catch (Exception ex) { + throw new RuntimeException(ex); + } finally { + XTool.close(ois); + XTool.close(bis); + } + } + + public void compile(XKey xKey, String dir) { + + } + + public XKey execute() { + + } + + public native byte[] read(); + + public XKey key() { + byte[] xKey = read(); + return decode(xKey); + } + +} diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp new file mode 100644 index 0000000..2dd4f33 --- /dev/null +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -0,0 +1,10 @@ +#include "XJni.h" + +JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_read(JNIEnv* env, jobject thiz) +{ + const jbyte bytes[] = { ${key} }; + int len = sizeof(bytes) / sizeof(bytes[0]); + jbyteArray key = env->NewByteArray(len); + env->SetByteArrayRegion(key, 0, len, bytes); + return key; +} \ No newline at end of file diff --git a/src/main/resources/io/xjar/jni/XJni.h b/src/main/resources/io/xjar/jni/XJni.h new file mode 100644 index 0000000..f1ff744 --- /dev/null +++ b/src/main/resources/io/xjar/jni/XJni.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include "jni.h" +/* Header for class io_xjar_jni_XJni */ + +#ifndef _Included_io_xjar_jni_XJni +#define _Included_io_xjar_jni_XJni +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: io_xjar_jni_XJni + * Method: read + * Signature: ()[B + */ +JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_read + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/test/java/test/io/xjar/XJarTest.java b/src/test/java/test/io/xjar/XJarTest.java index ef8bbed..34c1a02 100644 --- a/src/test/java/test/io/xjar/XJarTest.java +++ b/src/test/java/test/io/xjar/XJarTest.java @@ -1,24 +1,57 @@ package test.io.xjar; import io.xjar.XTool; -import io.xjar.jar.XJarEncryptor; +import io.xjar.jar.XJar; +import io.xjar.jni.XJni; +import io.xjar.key.XKey; import org.junit.Test; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.util.Arrays; +import java.util.Properties; +import java.util.Set; + /** * @author Payne 646742615@qq.com * 2019/7/4 16:24 */ public class XJarTest { + @Test + public void encode() throws Exception { + + XKey xKey = XJar.key("io.xjar"); + byte[] bytes = XJni.getInstance().encode(xKey); + System.out.println(Arrays.toString(bytes)); + } + + @Test + public void decode() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + XTool.transfer(new FileInputStream("C:\\Users\\Chang\\source\\repos\\Project1\\XJni.so"), bos); + System.out.println(Arrays.toString(bos.toByteArray())); + +// System.load("C:\\Users\\Chang\\source\\repos\\Project1\\XJni.so"); +// byte[] read = XJni.getInstance().read(); +// System.out.println(Arrays.toString(read)); + } + @Test public void test() { - XJarEncryptor.builder() - .build() - .encrypt( - XTool.key("io.xjar"), - "C:\\Users\\Chang\\Downloads\\Android-SDK\\target\\nwhs-sdk-v5.3.0-alpha.2.jar", - "C:\\Users\\Chang\\Downloads\\Android-SDK\\target\\nwhs-sdk-v5.3.0-alpha.2.xjar" - ); + Properties properties = System.getProperties(); + Set names = properties.stringPropertyNames(); + for (String name : names) { + System.out.println(name + ":" + properties.getProperty(name)); + } + +// XJar.encryptor() +// .build() +// .encrypt( +// XJar.key("io.xjar"), +// "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.jar", +// "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.xjar" +// ); } } From 225a03e921efc76a0ad62a71ab53d64c7928a38b Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Thu, 11 Jul 2019 11:00:03 +0800 Subject: [PATCH 19/37] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=91=98=E8=A6=81?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XLauncher.java | 8 +- src/main/java/io/xjar/jar/XJarEncryptor.java | 4 - src/main/java/io/xjar/jni/XJni.java | 105 ------------- src/main/resources/io/xjar/jni/MD5.cpp | 155 +++++++++++++++++++ src/main/resources/io/xjar/jni/MD5.h | 28 ++++ src/main/resources/io/xjar/jni/XJni.cpp | 2 +- src/main/resources/io/xjar/jni/XJni.h | 5 +- 7 files changed, 190 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/io/xjar/jni/XJni.java create mode 100644 src/main/resources/io/xjar/jni/MD5.cpp create mode 100644 src/main/resources/io/xjar/jni/MD5.h diff --git a/src/main/java/io/xjar/XLauncher.java b/src/main/java/io/xjar/XLauncher.java index bcaf918..96725cc 100644 --- a/src/main/java/io/xjar/XLauncher.java +++ b/src/main/java/io/xjar/XLauncher.java @@ -1,8 +1,9 @@ package io.xjar; -import io.xjar.jni.XJni; import io.xjar.key.XKey; +import java.security.NoSuchAlgorithmException; + /** * Spring-Boot 启动器 * @@ -15,10 +16,9 @@ public class XLauncher implements XConstants { public final XEncryptor xEncryptor; public final XKey xKey; - public XLauncher(String... args) { + public XLauncher(String... args) throws NoSuchAlgorithmException { this.args = args; - XJni xJni = XJni.getInstance(); - XKey xKey = xJni.key(); + XKey xKey = XKit.key("io.xjar"); String algorithm = xKey.getAlgorithm(); this.xDecryptor = new XJdkDecryptor(algorithm); this.xEncryptor = new XJdkEncryptor(algorithm); diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 980f4c0..1ce8c5d 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -2,7 +2,6 @@ import io.xjar.*; import io.xjar.digest.XJdkDigestFactory; -import io.xjar.jni.XJni; import io.xjar.key.XKey; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; import org.apache.commons.compress.archivers.jar.JarArchiveInputStream; @@ -113,9 +112,6 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti String mainClass = manifest != null && manifest.getMainAttributes() != null ? manifest.getMainAttributes().getValue("Main-Class") : null; if (mainClass != null) { XInjector.inject(zos, "io/xjar/**"); - XJni xJni = XJni.getInstance(); - xJni.compile(key); - } zos.finish(); diff --git a/src/main/java/io/xjar/jni/XJni.java b/src/main/java/io/xjar/jni/XJni.java deleted file mode 100644 index 2b18fc1..0000000 --- a/src/main/java/io/xjar/jni/XJni.java +++ /dev/null @@ -1,105 +0,0 @@ -package io.xjar.jni; - -import io.xjar.XTool; -import io.xjar.key.XKey; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -/** - * JNI - * - * @author Payne 646742615@qq.com - * 2019/7/10 13:19 - */ -public class XJni { - private static volatile XJni instance; - - static { - System.load("C:\\Users\\Chang\\source\\repos\\Project1\\XJni.so"); - } - - private XJni() { - - } - - /** - * 获取单例对象 - * - * @return 单例对象 - */ - public static XJni getInstance() { - if (instance != null) { - return instance; - } - synchronized (XJni.class) { - if (instance != null) { - return instance; - } - instance = new XJni(); - } - return instance; - } - - /** - * 编码 - * - * @param xKey 密钥 - * @return 密钥编码数据 - */ - public byte[] encode(XKey xKey) { - ByteArrayOutputStream bos = null; - ObjectOutputStream oos = null; - try { - bos = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(bos); - oos.writeObject(xKey); - return bos.toByteArray(); - } catch (Exception ex) { - throw new RuntimeException(ex); - } finally { - XTool.close(oos); - XTool.close(bos); - } - } - - /** - * 解码 - * - * @param xKey 密钥编码数据 - * @return 密钥 - */ - public XKey decode(byte[] xKey) { - ByteArrayInputStream bis = null; - ObjectInputStream ois = null; - try { - bis = new ByteArrayInputStream(xKey); - ois = new ObjectInputStream(bis); - Object xObj = ois.readObject(); - return (XKey) xObj; - } catch (Exception ex) { - throw new RuntimeException(ex); - } finally { - XTool.close(ois); - XTool.close(bis); - } - } - - public void compile(XKey xKey, String dir) { - - } - - public XKey execute() { - - } - - public native byte[] read(); - - public XKey key() { - byte[] xKey = read(); - return decode(xKey); - } - -} diff --git a/src/main/resources/io/xjar/jni/MD5.cpp b/src/main/resources/io/xjar/jni/MD5.cpp new file mode 100644 index 0000000..ffd74e3 --- /dev/null +++ b/src/main/resources/io/xjar/jni/MD5.cpp @@ -0,0 +1,155 @@ +#include "MD5.h" + +unsigned char PADDING[] = { + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +void MD5Init(MD5_CTX* context) { + context->count[0] = 0; + context->count[1] = 0; + context->state[0] = 0x67452301;//���ӱ�����ע�������ֽ���������ڴ�����С���ֽ��� + context->state[1] = 0xEFCDAB89; + context->state[2] = 0x98BADCFE; + context->state[3] = 0x10325476; +} + +void MD5Update(MD5_CTX* context, unsigned char* input, unsigned int inputlen) { + unsigned int i = 0, index = 0, partlen = 0; + index = (context->count[0] >> 3) & 0x3F;//ģ64�ֽڵ���������һ�ε���Ϊ0 + partlen = 64 - index;//��һ��partlenΪ64 + //Ԥ����ǰ���64λ������ݳ��� + context->count[0] += inputlen << 3;//�ı��ܵ�λ��,��32λ + if (context->count[0] < (inputlen << 3)) context->count[1]++; + context->count[1] += inputlen >> 29;//������3,���λ��,������32λ�������32λ + + if (inputlen >= partlen) { + memcpy(&context->buffer[index], input, partlen); + MD5Transform(context->state, context->buffer); + //512λΪ1�� + for (i = partlen; i + 64 <= inputlen; i += 64) MD5Transform(context->state, &input[i]); + index = 0; + } + else i = 0; + memcpy(&context->buffer[index], &input[i], inputlen - i);//���ʣ�µ�һ���� +} + +//32λmd5 +void MD5Final(MD5_CTX* context, unsigned char digest[16]) { + unsigned int index = 0, padlen = 0; + unsigned char bits[8]; + index = (context->count[0] >> 3) & 0x3F;//ģ64�ֽڵ����� + padlen = (index < 56) ? (56 - index) : (120 - index);//��Ҫ���ij��ȣ�ע�ⳤ��ռ8�ֽ� + MD5Encode(bits, context->count, 8); + MD5Update(context, PADDING, padlen); + MD5Update(context, bits, 8);//�������ʱ,index=0 + MD5Encode(digest, context->state, 16); +} + +void MD5Encode(unsigned char* output, unsigned int* input, unsigned int len) { + unsigned int i = 0, j = 0;//len��char�ij��� + while (j < len) { + output[j] = input[i] & 0xFF; + output[j + 1] = (input[i] >> 8) & 0xFF; + output[j + 2] = (input[i] >> 16) & 0xFF; + output[j + 3] = (input[i] >> 24) & 0xFF; + i++; + j += 4; + } +} + +void MD5Decode(unsigned int* output, unsigned char* input, unsigned int len) { + unsigned int i = 0, j = 0; + while (j < len) { + output[i] = (input[j]) | + (input[j + 1] << 8) | + (input[j + 2] << 16) | + (input[j + 3] << 24); + i++; + j += 4; + } +} + +void MD5Transform(unsigned int state[4], unsigned char block[64]) { + unsigned int a = state[0]; + unsigned int b = state[1]; + unsigned int c = state[2]; + unsigned int d = state[3]; + unsigned int x[16]; + + MD5Decode(x, block, 64); + FF(a, b, c, d, x[0], 7, 0xd76aa478);//32λ���� + FF(d, a, b, c, x[1], 12, 0xe8c7b756); + FF(c, d, a, b, x[2], 17, 0x242070db); + FF(b, c, d, a, x[3], 22, 0xc1bdceee); + FF(a, b, c, d, x[4], 7, 0xf57c0faf); + FF(d, a, b, c, x[5], 12, 0x4787c62a); + FF(c, d, a, b, x[6], 17, 0xa8304613); + FF(b, c, d, a, x[7], 22, 0xfd469501); + FF(a, b, c, d, x[8], 7, 0x698098d8); + FF(d, a, b, c, x[9], 12, 0x8b44f7af); + FF(c, d, a, b, x[10], 17, 0xffff5bb1); + FF(b, c, d, a, x[11], 22, 0x895cd7be); + FF(a, b, c, d, x[12], 7, 0x6b901122); + FF(d, a, b, c, x[13], 12, 0xfd987193); + FF(c, d, a, b, x[14], 17, 0xa679438e); + FF(b, c, d, a, x[15], 22, 0x49b40821); + + + GG(a, b, c, d, x[1], 5, 0xf61e2562); + GG(d, a, b, c, x[6], 9, 0xc040b340); + GG(c, d, a, b, x[11], 14, 0x265e5a51); + GG(b, c, d, a, x[0], 20, 0xe9b6c7aa); + GG(a, b, c, d, x[5], 5, 0xd62f105d); + GG(d, a, b, c, x[10], 9, 0x2441453); + GG(c, d, a, b, x[15], 14, 0xd8a1e681); + GG(b, c, d, a, x[4], 20, 0xe7d3fbc8); + GG(a, b, c, d, x[9], 5, 0x21e1cde6); + GG(d, a, b, c, x[14], 9, 0xc33707d6); + GG(c, d, a, b, x[3], 14, 0xf4d50d87); + GG(b, c, d, a, x[8], 20, 0x455a14ed); + GG(a, b, c, d, x[13], 5, 0xa9e3e905); + GG(d, a, b, c, x[2], 9, 0xfcefa3f8); + GG(c, d, a, b, x[7], 14, 0x676f02d9); + GG(b, c, d, a, x[12], 20, 0x8d2a4c8a); + + + HH(a, b, c, d, x[5], 4, 0xfffa3942); + HH(d, a, b, c, x[8], 11, 0x8771f681); + HH(c, d, a, b, x[11], 16, 0x6d9d6122); + HH(b, c, d, a, x[14], 23, 0xfde5380c); + HH(a, b, c, d, x[1], 4, 0xa4beea44); + HH(d, a, b, c, x[4], 11, 0x4bdecfa9); + HH(c, d, a, b, x[7], 16, 0xf6bb4b60); + HH(b, c, d, a, x[10], 23, 0xbebfbc70); + HH(a, b, c, d, x[13], 4, 0x289b7ec6); + HH(d, a, b, c, x[0], 11, 0xeaa127fa); + HH(c, d, a, b, x[3], 16, 0xd4ef3085); + HH(b, c, d, a, x[6], 23, 0x4881d05); + HH(a, b, c, d, x[9], 4, 0xd9d4d039); + HH(d, a, b, c, x[12], 11, 0xe6db99e5); + HH(c, d, a, b, x[15], 16, 0x1fa27cf8); + HH(b, c, d, a, x[2], 23, 0xc4ac5665); + + + II(a, b, c, d, x[0], 6, 0xf4292244); + II(d, a, b, c, x[7], 10, 0x432aff97); + II(c, d, a, b, x[14], 15, 0xab9423a7); + II(b, c, d, a, x[5], 21, 0xfc93a039); + II(a, b, c, d, x[12], 6, 0x655b59c3); + II(d, a, b, c, x[3], 10, 0x8f0ccc92); + II(c, d, a, b, x[10], 15, 0xffeff47d); + II(b, c, d, a, x[1], 21, 0x85845dd1); + II(a, b, c, d, x[8], 6, 0x6fa87e4f); + II(d, a, b, c, x[15], 10, 0xfe2ce6e0); + II(c, d, a, b, x[6], 15, 0xa3014314); + II(b, c, d, a, x[13], 21, 0x4e0811a1); + II(a, b, c, d, x[4], 6, 0xf7537e82); + II(d, a, b, c, x[11], 10, 0xbd3af235); + II(c, d, a, b, x[2], 15, 0x2ad7d2bb); + II(b, c, d, a, x[9], 21, 0xeb86d391); + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; +} \ No newline at end of file diff --git a/src/main/resources/io/xjar/jni/MD5.h b/src/main/resources/io/xjar/jni/MD5.h new file mode 100644 index 0000000..c026204 --- /dev/null +++ b/src/main/resources/io/xjar/jni/MD5.h @@ -0,0 +1,28 @@ +#include +#include +#include +#include +#include +#include +typedef struct { + unsigned int count[2]; + unsigned int state[4]; + unsigned char buffer[64]; +}MD5_CTX; + +#define F(x,y,z) ((x&y)|(~x&z)) +#define G(x,y,z) ((x&z)|(y&~z)) +#define H(x,y,z) (x^y^z) +#define I(x,y,z) (y^(x|~z)) +#define ROTATE_LEFT(x,n) ((x<>(32-n))) +#define FF(a,b,c,d,x,s,ac) { a+=F(b,c,d)+x+ac; a=ROTATE_LEFT(a,s); a+=b;} +#define GG(a,b,c,d,x,s,ac) { a+=G(b,c,d)+x+ac; a=ROTATE_LEFT(a,s); a+=b;} +#define HH(a,b,c,d,x,s,ac) { a+=H(b,c,d)+x+ac; a=ROTATE_LEFT(a,s); a+=b;} +#define II(a,b,c,d,x,s,ac) { a+=I(b,c,d)+x+ac; a=ROTATE_LEFT(a,s); a+=b;} + +void MD5Init(MD5_CTX* context); +void MD5Update(MD5_CTX* context, unsigned char* input, unsigned int inputlen); +void MD5Final(MD5_CTX* context, unsigned char digest[16]); +void MD5Transform(unsigned int state[4], unsigned char block[64]); +void MD5Encode(unsigned char* output, unsigned int* input, unsigned int len); +void MD5Decode(unsigned int* output, unsigned char* input, unsigned int len); \ No newline at end of file diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp index 2dd4f33..3284da1 100644 --- a/src/main/resources/io/xjar/jni/XJni.cpp +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -1,6 +1,6 @@ #include "XJni.h" -JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_read(JNIEnv* env, jobject thiz) +JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_call(JNIEnv* env, jobject thiz) { const jbyte bytes[] = { ${key} }; int len = sizeof(bytes) / sizeof(bytes[0]); diff --git a/src/main/resources/io/xjar/jni/XJni.h b/src/main/resources/io/xjar/jni/XJni.h index f1ff744..3b6bd45 100644 --- a/src/main/resources/io/xjar/jni/XJni.h +++ b/src/main/resources/io/xjar/jni/XJni.h @@ -9,11 +9,10 @@ extern "C" { #endif /* * Class: io_xjar_jni_XJni - * Method: read + * Method: call * Signature: ()[B */ -JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_read - (JNIEnv *, jobject); +JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_call(JNIEnv *, jobject); #ifdef __cplusplus } From faeb6ec3a49e2b6f005f130b48702c35117de8df Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Mon, 15 Jul 2019 16:17:33 +0800 Subject: [PATCH 20/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + src/main/java/io/xjar/XCompiler.java | 26 ++++ src/main/java/io/xjar/XSignature.java | 49 +++++++ .../java/io/xjar/compiler/XStdCompiler.java | 131 ++++++++++++++++++ src/main/java/io/xjar/jar/XJarEncryptor.java | 59 ++++---- src/main/resources/io/xjar/jni/XJni.cpp | 9 +- src/test/java/test/io/xjar/XJarTest.java | 58 +++----- 7 files changed, 267 insertions(+), 70 deletions(-) create mode 100644 src/main/java/io/xjar/XCompiler.java create mode 100644 src/main/java/io/xjar/XSignature.java create mode 100644 src/main/java/io/xjar/compiler/XStdCompiler.java diff --git a/pom.xml b/pom.xml index 4c5f579..a3f9c6b 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,11 @@ 2.0.1.RELEASE provided + + com.ibeetl + beetl + 3.0.8.RELEASE + junit diff --git a/src/main/java/io/xjar/XCompiler.java b/src/main/java/io/xjar/XCompiler.java new file mode 100644 index 0000000..ccc0181 --- /dev/null +++ b/src/main/java/io/xjar/XCompiler.java @@ -0,0 +1,26 @@ +package io.xjar; + +import io.xjar.key.XKey; + +import java.io.File; +import java.io.IOException; + +/** + * 编译器 + * + * @author Payne 646742615@qq.com + * 2019/7/15 10:33 + */ +public interface XCompiler { + + /** + * 编译 + * + * @param xKey 密钥 + * @param xSignature 签名 + * @return 编译后文件 + * @throws IOException I/O异常 + */ + File compile(XKey xKey, XSignature xSignature) throws IOException; + +} diff --git a/src/main/java/io/xjar/XSignature.java b/src/main/java/io/xjar/XSignature.java new file mode 100644 index 0000000..52c4cf0 --- /dev/null +++ b/src/main/java/io/xjar/XSignature.java @@ -0,0 +1,49 @@ +package io.xjar; + +import java.util.Arrays; +import java.util.Objects; + +/** + * 签名 + * + * @author Payne 646742615@qq.com + * 2019/7/15 10:35 + */ +public class XSignature { + private final String algorithm; + private final byte[] signature; + + public XSignature(String algorithm, byte[] signature) { + this.algorithm = algorithm; + this.signature = signature; + } + + public String getAlgorithm() { + return algorithm; + } + + public byte[] getSignature() { + return signature; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + XSignature that = (XSignature) o; + return Objects.equals(algorithm, that.algorithm) && + Arrays.equals(signature, that.signature); + } + + @Override + public int hashCode() { + int result = Objects.hash(algorithm); + result = 31 * result + Arrays.hashCode(signature); + return result; + } + + @Override + public String toString() { + return String.valueOf(algorithm) + Arrays.toString(signature); + } +} diff --git a/src/main/java/io/xjar/compiler/XStdCompiler.java b/src/main/java/io/xjar/compiler/XStdCompiler.java new file mode 100644 index 0000000..b805427 --- /dev/null +++ b/src/main/java/io/xjar/compiler/XStdCompiler.java @@ -0,0 +1,131 @@ +package io.xjar.compiler; + +import io.loadkit.Loaders; +import io.loadkit.Resource; +import io.xjar.XCompiler; +import io.xjar.XSignature; +import io.xjar.key.XKey; +import org.beetl.core.Configuration; +import org.beetl.core.GroupTemplate; +import org.beetl.core.ResourceLoader; +import org.beetl.core.Template; +import org.beetl.core.resource.ClasspathResourceLoader; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.List; +import java.util.UUID; + +/** + * 标准编译器 + * + * @author Payne 646742615@qq.com + * 2019/7/15 10:42 + */ +public class XStdCompiler implements XCompiler { + private String srcRoot = "io/xjar/jni"; + private String gccRoot = ""; + private String jdkRoot = new File(System.getProperty("java.home")).getParent(); + private String tmpRoot = System.getProperty("java.io.tmpdir"); + + public XStdCompiler() { + } + + public XStdCompiler(String gccRoot) { + this.gccRoot = gccRoot; + } + + public XStdCompiler(String gccRoot, String jdkRoot) { + this.gccRoot = gccRoot; + this.jdkRoot = jdkRoot; + } + + public XStdCompiler(String gccRoot, String jdkRoot, String tmpRoot) { + this.gccRoot = gccRoot; + this.jdkRoot = jdkRoot; + this.tmpRoot = tmpRoot; + } + + @Override + public File compile(XKey xKey, XSignature xSignature) throws IOException { + // 创建目录 + File dir = new File(tmpRoot, UUID.randomUUID().toString()); + if (!dir.exists() && !dir.mkdirs() && !dir.exists()) { + throw new IOException("could not make directory: " + dir); + } + + // 渲染源码 + List srcs = new ArrayList<>(); + ResourceLoader resourceLoader = new ClasspathResourceLoader(); + Configuration configuration = Configuration.defaultConfiguration(); + GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, configuration); + Enumeration resources = Loaders.ant().load("io/xjar/jni/**/*"); + while (resources.hasMoreElements()) { + Resource src = resources.nextElement(); + String name = src.getName(); + srcs.add(name); + File file = new File(dir, name); + File folder = file.getParentFile(); + if (!folder.exists() && !folder.mkdirs() && !folder.exists()) { + throw new IOException("could not make directory: " + folder); + } + Template template = groupTemplate.getTemplate(name); + template.binding("xKey", xKey); + template.binding("xSignature", xSignature); + try (OutputStream out = new FileOutputStream(file)) { + template.renderTo(out); + } + } + + // 编译源码 + StringBuilder cmd = new StringBuilder(); + cmd.append(gccRoot); + cmd.append(" ").append("g++"); + cmd.append(" ").append("-fPIC -shared -o XJar.so"); + for (String src : srcs) { + cmd.append(" ").append(src); + } + cmd.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); + cmd.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("win32").append("\""); + + try { + Process process = Runtime.getRuntime().exec(cmd.toString(), null, dir); + int code = process.waitFor(); + if (code != 0) { + throw new IllegalStateException("error occurred while compiling c++ lib with code: " + code); + } + } catch (Exception e) { + throw new IOException(e); + } + + return null; + } + + public String getGccRoot() { + return gccRoot; + } + + public void setGccRoot(String gccRoot) { + this.gccRoot = gccRoot; + } + + public String getJdkRoot() { + return jdkRoot; + } + + public void setJdkRoot(String jdkRoot) { + this.jdkRoot = jdkRoot; + } + + public String getTmpRoot() { + return tmpRoot; + } + + public void setTmpRoot(String tmpRoot) { + this.tmpRoot = tmpRoot; + } +} diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 1ce8c5d..93f023e 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -1,6 +1,7 @@ package io.xjar.jar; import io.xjar.*; +import io.xjar.compiler.XStdCompiler; import io.xjar.digest.XJdkDigestFactory; import io.xjar.key.XKey; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; @@ -11,6 +12,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.security.NoSuchAlgorithmException; import java.util.LinkedHashSet; import java.util.Set; import java.util.jar.Attributes; @@ -25,20 +27,24 @@ */ public class XJarEncryptor extends XArchiveEncryptor implements XEncryptor, XConstants { private final int level; - private final String jdkLocation; - private final String gccLocation; - private final String tmpLocation; private final XDigestFactory digestFactory; private final String digestAlgorithm; + private final XCompiler compiler; - public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, String jdkLocation, String gccLocation, String tmpLocation, XDigestFactory digestFactory, String digestAlgorithm) { + public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, XDigestFactory digestFactory, String digestAlgorithm, XCompiler compiler) { super(xEncryptor, filter); this.level = level; - this.jdkLocation = jdkLocation; - this.gccLocation = gccLocation; - this.tmpLocation = tmpLocation; this.digestFactory = digestFactory; this.digestAlgorithm = digestAlgorithm; + this.compiler = compiler; + } + + public static void main(String... args) throws Exception { + XJarEncryptor.builder().build().encrypt( + XJar.key("io.xjar"), + "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.jar", + "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.xjar" + ); } @Override @@ -46,9 +52,11 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti JarArchiveInputStream zis = null; JarArchiveOutputStream zos = null; Set indexes = new LinkedHashSet<>(); + XDigest digest = null; try { + digest = digestFactory.acquire(digestAlgorithm); zis = new JarArchiveInputStream(in); - zos = new JarArchiveOutputStream(out); + zos = new XDigestedOutputStream(out, digest); zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); @@ -112,12 +120,21 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti String mainClass = manifest != null && manifest.getMainAttributes() != null ? manifest.getMainAttributes().getValue("Main-Class") : null; if (mainClass != null) { XInjector.inject(zos, "io/xjar/**"); + byte[] signature = digest.finish(); + XSignature xSignature = new XSignature(digestAlgorithm, signature); + File library = compiler.compile(key, xSignature); + } zos.finish(); + } catch (NoSuchAlgorithmException e) { + throw new IOException(e); } finally { XTool.close(zis); XTool.close(zos); + if (digest != null) { + digestFactory.release(digestAlgorithm, digest); + } } } @@ -127,11 +144,9 @@ public static XJarEncryptorBuilder builder() { public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; - private String jdkLocation = new File(System.getProperty("java.home")).getParent(); - private String gccLocation = "g++"; - private String tmpLocation = System.getProperty("java.io.tmpdir"); private XDigestFactory digestFactory = new XJdkDigestFactory(); private String digestAlgorithm = "MD5"; + private XCompiler compiler = new XStdCompiler(); { encryptor(new XSmtEncryptor()); @@ -143,21 +158,6 @@ public XJarEncryptorBuilder level(int level) { return this; } - public XJarEncryptorBuilder jdkLocation(String jdkLocation) { - this.jdkLocation = jdkLocation; - return this; - } - - public XJarEncryptorBuilder gccLocation(String gccLocation) { - this.gccLocation = gccLocation; - return this; - } - - public XJarEncryptorBuilder tmpLocation(String tmpLocation) { - this.tmpLocation = tmpLocation; - return this; - } - public XJarEncryptorBuilder digestFactory(XDigestFactory digestFactory) { this.digestFactory = digestFactory; return this; @@ -168,9 +168,14 @@ public XJarEncryptorBuilder digestAlgorithm(String digestAlgorithm) { return this; } + public XJarEncryptorBuilder compiler(XCompiler compiler) { + this.compiler = compiler; + return this; + } + @Override public XJarEncryptor build() { - return new XJarEncryptor(encryptor, filter, level, jdkLocation, gccLocation, tmpLocation, digestFactory, digestAlgorithm); + return new XJarEncryptor(encryptor, filter, level, digestFactory, digestAlgorithm, compiler); } } } diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp index 3284da1..25e33b6 100644 --- a/src/main/resources/io/xjar/jni/XJni.cpp +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -2,7 +2,14 @@ JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_call(JNIEnv* env, jobject thiz) { - const jbyte bytes[] = { ${key} }; + const jbyte bytes[] = {<% + for (k in xKey.decryptKey) { + print(k); + if (!kLP.last) { + print(', '); + } + } + %>}; int len = sizeof(bytes) / sizeof(bytes[0]); jbyteArray key = env->NewByteArray(len); env->SetByteArrayRegion(key, 0, len, bytes); diff --git a/src/test/java/test/io/xjar/XJarTest.java b/src/test/java/test/io/xjar/XJarTest.java index 34c1a02..e0c1b8e 100644 --- a/src/test/java/test/io/xjar/XJarTest.java +++ b/src/test/java/test/io/xjar/XJarTest.java @@ -1,16 +1,12 @@ package test.io.xjar; -import io.xjar.XTool; -import io.xjar.jar.XJar; -import io.xjar.jni.XJni; -import io.xjar.key.XKey; +import org.beetl.core.Configuration; +import org.beetl.core.GroupTemplate; +import org.beetl.core.Template; +import org.beetl.core.resource.ClasspathResourceLoader; import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.util.Arrays; -import java.util.Properties; -import java.util.Set; +import java.io.IOException; /** * @author Payne 646742615@qq.com @@ -19,39 +15,17 @@ public class XJarTest { @Test - public void encode() throws Exception { - - XKey xKey = XJar.key("io.xjar"); - byte[] bytes = XJni.getInstance().encode(xKey); - System.out.println(Arrays.toString(bytes)); - } - - @Test - public void decode() throws Exception { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XTool.transfer(new FileInputStream("C:\\Users\\Chang\\source\\repos\\Project1\\XJni.so"), bos); - System.out.println(Arrays.toString(bos.toByteArray())); - -// System.load("C:\\Users\\Chang\\source\\repos\\Project1\\XJni.so"); -// byte[] read = XJni.getInstance().read(); -// System.out.println(Arrays.toString(read)); - } - - @Test - public void test() { - Properties properties = System.getProperties(); - Set names = properties.stringPropertyNames(); - for (String name : names) { - System.out.println(name + ":" + properties.getProperty(name)); - } - -// XJar.encryptor() -// .build() -// .encrypt( -// XJar.key("io.xjar"), -// "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.jar", -// "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.xjar" -// ); + public void test() throws IOException { + //初始化代码 + ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(); + Configuration cfg = Configuration.defaultConfiguration(); + GroupTemplate gt = new GroupTemplate(resourceLoader, cfg); + //获取模板 + Template t = gt.getTemplate("io/xjar/jni/XJni.cpp"); + t.binding("key", new byte[]{1, 2, 3, 4}); + //渲染结果 + String str = t.render(); + System.out.println(str); } } From 97b2fbedda79f4bb141b8330172f5a02e5811a05 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Mon, 15 Jul 2019 16:53:32 +0800 Subject: [PATCH 21/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XTool.java | 23 +++++++ .../java/io/xjar/compiler/XStdCompiler.java | 69 ++++++++++--------- src/main/java/io/xjar/jar/XJarEncryptor.java | 8 ++- 3 files changed, 65 insertions(+), 35 deletions(-) diff --git a/src/main/java/io/xjar/XTool.java b/src/main/java/io/xjar/XTool.java index 3c8a2b3..f51ce7d 100644 --- a/src/main/java/io/xjar/XTool.java +++ b/src/main/java/io/xjar/XTool.java @@ -162,6 +162,29 @@ public static long transfer(Reader reader, File file) throws IOException { } } + public static long transfer(File file, OutputStream out) throws IOException { + InputStream in = null; + try { + in = new FileInputStream(file); + return transfer(in, out); + } finally { + close(in); + } + } + + public static long transfer(File file, Writer writer) throws IOException { + InputStream in = null; + Reader reader = null; + try { + in = new FileInputStream(file); + reader = new InputStreamReader(in); + return transfer(reader, writer); + } finally { + close(reader); + close(in); + } + } + /** * 删除文件,如果是目录将不递归删除子文件或目录,等效于delete(file, false); * diff --git a/src/main/java/io/xjar/compiler/XStdCompiler.java b/src/main/java/io/xjar/compiler/XStdCompiler.java index b805427..3a41e11 100644 --- a/src/main/java/io/xjar/compiler/XStdCompiler.java +++ b/src/main/java/io/xjar/compiler/XStdCompiler.java @@ -27,29 +27,12 @@ * 2019/7/15 10:42 */ public class XStdCompiler implements XCompiler { + private String gccPath = "g++"; + private String libName = "XJar.so"; private String srcRoot = "io/xjar/jni"; - private String gccRoot = ""; private String jdkRoot = new File(System.getProperty("java.home")).getParent(); private String tmpRoot = System.getProperty("java.io.tmpdir"); - public XStdCompiler() { - } - - public XStdCompiler(String gccRoot) { - this.gccRoot = gccRoot; - } - - public XStdCompiler(String gccRoot, String jdkRoot) { - this.gccRoot = gccRoot; - this.jdkRoot = jdkRoot; - } - - public XStdCompiler(String gccRoot, String jdkRoot, String tmpRoot) { - this.gccRoot = gccRoot; - this.jdkRoot = jdkRoot; - this.tmpRoot = tmpRoot; - } - @Override public File compile(XKey xKey, XSignature xSignature) throws IOException { // 创建目录 @@ -63,7 +46,7 @@ public File compile(XKey xKey, XSignature xSignature) throws IOException { ResourceLoader resourceLoader = new ClasspathResourceLoader(); Configuration configuration = Configuration.defaultConfiguration(); GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, configuration); - Enumeration resources = Loaders.ant().load("io/xjar/jni/**/*"); + Enumeration resources = Loaders.ant().load(srcRoot + "/**/*"); while (resources.hasMoreElements()) { Resource src = resources.nextElement(); String name = src.getName(); @@ -81,19 +64,23 @@ public File compile(XKey xKey, XSignature xSignature) throws IOException { } } + File lib = new File(dir, libName); + // 编译源码 - StringBuilder cmd = new StringBuilder(); - cmd.append(gccRoot); - cmd.append(" ").append("g++"); - cmd.append(" ").append("-fPIC -shared -o XJar.so"); + StringBuilder command = new StringBuilder(); + command.append("\"").append(gccPath).append("\""); + command.append(" ").append("-fPIC"); + command.append(" ").append("-shared"); + command.append(" ").append("-o"); + command.append(" ").append("\"").append(dir.toURI().relativize(lib.toURI())).append("\""); for (String src : srcs) { - cmd.append(" ").append(src); + command.append(" ").append("\"").append(src).append("\""); } - cmd.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); - cmd.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("win32").append("\""); + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("win32").append("\""); try { - Process process = Runtime.getRuntime().exec(cmd.toString(), null, dir); + Process process = Runtime.getRuntime().exec(command.toString(), null, dir); int code = process.waitFor(); if (code != 0) { throw new IllegalStateException("error occurred while compiling c++ lib with code: " + code); @@ -102,15 +89,31 @@ public File compile(XKey xKey, XSignature xSignature) throws IOException { throw new IOException(e); } - return null; + return lib; + } + + public String getGccPath() { + return gccPath; + } + + public void setGccPath(String gccPath) { + this.gccPath = gccPath; + } + + public String getLibName() { + return libName; + } + + public void setLibName(String libName) { + this.libName = libName; } - public String getGccRoot() { - return gccRoot; + public String getSrcRoot() { + return srcRoot; } - public void setGccRoot(String gccRoot) { - this.gccRoot = gccRoot; + public void setSrcRoot(String srcRoot) { + this.srcRoot = srcRoot; } public String getJdkRoot() { diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 93f023e..f820fdb 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -122,8 +122,12 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti XInjector.inject(zos, "io/xjar/**"); byte[] signature = digest.finish(); XSignature xSignature = new XSignature(digestAlgorithm, signature); - File library = compiler.compile(key, xSignature); - + File lib = compiler.compile(key, xSignature); + JarArchiveEntry xLibEntry = new JarArchiveEntry("XJar.so"); + xLibEntry.setTime(System.currentTimeMillis()); + zos.putArchiveEntry(xLibEntry); + XTool.transfer(lib, zos); + zos.closeArchiveEntry(); } zos.finish(); From 5e7a3f2a969ad856a22dcba5bd5ca3439360842c Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 16 Jul 2019 11:23:31 +0800 Subject: [PATCH 22/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/xjar/compiler/XAwareCompiler.java | 42 +++++++ .../java/io/xjar/compiler/XLinuxCompiler.java | 17 +++ .../java/io/xjar/compiler/XMacCompiler.java | 17 +++ ...tdCompiler.java => XPlatformCompiler.java} | 110 ++++++++---------- .../io/xjar/compiler/XWindowsCompiler.java | 31 +++++ src/main/java/io/xjar/jar/XJarEncryptor.java | 4 +- 6 files changed, 160 insertions(+), 61 deletions(-) create mode 100644 src/main/java/io/xjar/compiler/XAwareCompiler.java create mode 100644 src/main/java/io/xjar/compiler/XLinuxCompiler.java create mode 100644 src/main/java/io/xjar/compiler/XMacCompiler.java rename src/main/java/io/xjar/compiler/{XStdCompiler.java => XPlatformCompiler.java} (52%) create mode 100644 src/main/java/io/xjar/compiler/XWindowsCompiler.java diff --git a/src/main/java/io/xjar/compiler/XAwareCompiler.java b/src/main/java/io/xjar/compiler/XAwareCompiler.java new file mode 100644 index 0000000..27f07f6 --- /dev/null +++ b/src/main/java/io/xjar/compiler/XAwareCompiler.java @@ -0,0 +1,42 @@ +package io.xjar.compiler; + +import io.xjar.XCompiler; +import io.xjar.XSignature; +import io.xjar.key.XKey; + +import java.io.File; +import java.io.IOException; + +/** + * 自知 编译器 + * + * @author Payne 646742615@qq.com + * 2019/7/16 11:10 + */ +public class XAwareCompiler implements XCompiler { + private static final String WINDOWS = "WINDOWS"; + private static final String LINUX = "LINUX"; + private static final String MAC = "MAC"; + + private final XCompiler delegate; + + public XAwareCompiler() { + String os = System.getProperty("os.name"); + if (os == null) { + throw new IllegalStateException("unknown current operation system"); + } else if (os.toUpperCase().startsWith(WINDOWS)) { + this.delegate = new XWindowsCompiler(); + } else if (os.toUpperCase().startsWith(LINUX)) { + this.delegate = new XLinuxCompiler(); + } else if (os.toUpperCase().startsWith(MAC)) { + this.delegate = new XMacCompiler(); + } else { + throw new IllegalStateException("unsupported operation system: " + os); + } + } + + @Override + public File compile(XKey xKey, XSignature xSignature) throws IOException { + return delegate.compile(xKey, xSignature); + } +} diff --git a/src/main/java/io/xjar/compiler/XLinuxCompiler.java b/src/main/java/io/xjar/compiler/XLinuxCompiler.java new file mode 100644 index 0000000..0f0bfef --- /dev/null +++ b/src/main/java/io/xjar/compiler/XLinuxCompiler.java @@ -0,0 +1,17 @@ +package io.xjar.compiler; + +import java.util.List; + +/** + * Linux 编译器 + * + * @author Payne 646742615@qq.com + * 2019/7/16 10:47 + */ +public class XLinuxCompiler extends XPlatformCompiler { + + @Override + protected String convert(List src, String lib) { + return null; + } +} diff --git a/src/main/java/io/xjar/compiler/XMacCompiler.java b/src/main/java/io/xjar/compiler/XMacCompiler.java new file mode 100644 index 0000000..9424a92 --- /dev/null +++ b/src/main/java/io/xjar/compiler/XMacCompiler.java @@ -0,0 +1,17 @@ +package io.xjar.compiler; + +import java.util.List; + +/** + * Mac 编译器 + * + * @author Payne 646742615@qq.com + * 2019/7/16 10:48 + */ +public class XMacCompiler extends XPlatformCompiler { + + @Override + protected String convert(List src, String lib) { + return null; + } +} diff --git a/src/main/java/io/xjar/compiler/XStdCompiler.java b/src/main/java/io/xjar/compiler/XPlatformCompiler.java similarity index 52% rename from src/main/java/io/xjar/compiler/XStdCompiler.java rename to src/main/java/io/xjar/compiler/XPlatformCompiler.java index 3a41e11..fa70b0c 100644 --- a/src/main/java/io/xjar/compiler/XStdCompiler.java +++ b/src/main/java/io/xjar/compiler/XPlatformCompiler.java @@ -26,31 +26,65 @@ * @author Payne 646742615@qq.com * 2019/7/15 10:42 */ -public class XStdCompiler implements XCompiler { - private String gccPath = "g++"; - private String libName = "XJar.so"; - private String srcRoot = "io/xjar/jni"; - private String jdkRoot = new File(System.getProperty("java.home")).getParent(); - private String tmpRoot = System.getProperty("java.io.tmpdir"); +public abstract class XPlatformCompiler implements XCompiler { + protected String libName = "xjar.so"; + protected String srcRoot = "io/xjar/jni"; + protected String tmpRoot = System.getProperty("java.io.tmpdir"); @Override public File compile(XKey xKey, XSignature xSignature) throws IOException { - // 创建目录 - File dir = new File(tmpRoot, UUID.randomUUID().toString()); - if (!dir.exists() && !dir.mkdirs() && !dir.exists()) { - throw new IOException("could not make directory: " + dir); + try { + File dir = new File(tmpRoot, UUID.randomUUID().toString()); + if (!dir.exists() && !dir.mkdirs() && !dir.exists()) { + throw new IOException("could not make directory: " + dir); + } + + List src = render(xKey, xSignature, srcRoot, dir); + String cmd = convert(src, libName); + + Process process = Runtime.getRuntime().exec(cmd, null, dir); + int code = process.waitFor(); + if (code != 0) { + throw new IllegalStateException("error occurred while compiling c++ lib with code: " + code); + } + + return new File(dir, libName); + } catch (IOException ex) { + throw ex; + } catch (Exception ex) { + throw new IOException(ex); } + } - // 渲染源码 - List srcs = new ArrayList<>(); + /** + * 转换编译命令 + * + * @param src 源码列表 + * @param lib 目标类库 + * @return 编译命令 + */ + protected abstract String convert(List src, String lib); + + /** + * 渲染源码文件 + * + * @param xKey 密钥 + * @param xSignature 签名 + * @param path 资源根路径 + * @param dir 目录 + * @return 源码文件相对路径列表 + * @throws IOException I/O异常 + */ + protected List render(XKey xKey, XSignature xSignature, String path, File dir) throws IOException { + List sources = new ArrayList<>(); ResourceLoader resourceLoader = new ClasspathResourceLoader(); Configuration configuration = Configuration.defaultConfiguration(); GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, configuration); - Enumeration resources = Loaders.ant().load(srcRoot + "/**/*"); + Enumeration resources = Loaders.ant().load(path + "/**/*"); while (resources.hasMoreElements()) { - Resource src = resources.nextElement(); - String name = src.getName(); - srcs.add(name); + Resource resource = resources.nextElement(); + String name = resource.getName(); + sources.add(name); File file = new File(dir, name); File folder = file.getParentFile(); if (!folder.exists() && !folder.mkdirs() && !folder.exists()) { @@ -63,41 +97,7 @@ public File compile(XKey xKey, XSignature xSignature) throws IOException { template.renderTo(out); } } - - File lib = new File(dir, libName); - - // 编译源码 - StringBuilder command = new StringBuilder(); - command.append("\"").append(gccPath).append("\""); - command.append(" ").append("-fPIC"); - command.append(" ").append("-shared"); - command.append(" ").append("-o"); - command.append(" ").append("\"").append(dir.toURI().relativize(lib.toURI())).append("\""); - for (String src : srcs) { - command.append(" ").append("\"").append(src).append("\""); - } - command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); - command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("win32").append("\""); - - try { - Process process = Runtime.getRuntime().exec(command.toString(), null, dir); - int code = process.waitFor(); - if (code != 0) { - throw new IllegalStateException("error occurred while compiling c++ lib with code: " + code); - } - } catch (Exception e) { - throw new IOException(e); - } - - return lib; - } - - public String getGccPath() { - return gccPath; - } - - public void setGccPath(String gccPath) { - this.gccPath = gccPath; + return sources; } public String getLibName() { @@ -116,14 +116,6 @@ public void setSrcRoot(String srcRoot) { this.srcRoot = srcRoot; } - public String getJdkRoot() { - return jdkRoot; - } - - public void setJdkRoot(String jdkRoot) { - this.jdkRoot = jdkRoot; - } - public String getTmpRoot() { return tmpRoot; } diff --git a/src/main/java/io/xjar/compiler/XWindowsCompiler.java b/src/main/java/io/xjar/compiler/XWindowsCompiler.java new file mode 100644 index 0000000..06e096b --- /dev/null +++ b/src/main/java/io/xjar/compiler/XWindowsCompiler.java @@ -0,0 +1,31 @@ +package io.xjar.compiler; + +import java.io.File; +import java.util.List; + +/** + * Windows 编译器 + * + * @author Payne 646742615@qq.com + * 2019/7/16 10:39 + */ +public class XWindowsCompiler extends XPlatformCompiler { + protected String gccPath = "g++"; + protected String jdkRoot = new File(System.getProperty("java.home")).getParent(); + + @Override + protected String convert(List src, String lib) { + StringBuilder command = new StringBuilder(); + command.append("\"").append(gccPath).append("\""); + command.append(" ").append("-fPIC"); + command.append(" ").append("-shared"); + command.append(" ").append("-o"); + command.append(" ").append("\"").append(lib).append("\""); + for (String path : src) { + command.append(" ").append("\"").append(path).append("\""); + } + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("win32").append("\""); + return command.toString(); + } +} diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index f820fdb..5e65ace 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -1,7 +1,7 @@ package io.xjar.jar; import io.xjar.*; -import io.xjar.compiler.XStdCompiler; +import io.xjar.compiler.XAwareCompiler; import io.xjar.digest.XJdkDigestFactory; import io.xjar.key.XKey; import org.apache.commons.compress.archivers.jar.JarArchiveEntry; @@ -150,7 +150,7 @@ public static class XJarEncryptorBuilder extends XArchiveEncryptorBuilder Date: Tue, 16 Jul 2019 13:54:50 +0800 Subject: [PATCH 23/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XJni.java | 53 +++++++++++++++++++ src/main/java/io/xjar/XLauncher.java | 6 +-- .../io/xjar/compiler/XPlatformCompiler.java | 2 +- src/main/java/io/xjar/jar/XJarEncryptor.java | 2 +- src/main/resources/io/xjar/jni/XJni.cpp | 2 +- src/main/resources/io/xjar/jni/XJni.h | 10 ++-- 6 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 src/main/java/io/xjar/XJni.java diff --git a/src/main/java/io/xjar/XJni.java b/src/main/java/io/xjar/XJni.java new file mode 100644 index 0000000..e8ca1c9 --- /dev/null +++ b/src/main/java/io/xjar/XJni.java @@ -0,0 +1,53 @@ +package io.xjar; + +import io.xjar.key.XKey; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +/** + * JNI 接口 + * + * @author Payne 646742615@qq.com + * 2019/7/16 13:22 + */ +public class XJni { + private static volatile XJni instance; + + private XJni() throws IOException { + final ClassLoader classLoader = this.getClass().getClassLoader(); + final URL url = classLoader.getResource("XJAR.SO"); + if (url == null) { + throw new IllegalStateException("xjar library not found"); + } + final File lib = File.createTempFile("XJAR", ".SO"); + try (InputStream in = url.openStream()) { + XTool.transfer(in, lib); + } + System.load(lib.getCanonicalPath()); + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + XTool.delete(lib); + } + }); + } + + public static XJni getInstance() throws IOException { + if (instance != null) { + return instance; + } + synchronized (XJni.class) { + if (instance != null) { + return instance; + } + instance = new XJni(); + return instance; + } + } + + public native XKey call(); + +} diff --git a/src/main/java/io/xjar/XLauncher.java b/src/main/java/io/xjar/XLauncher.java index 96725cc..4ebcf84 100644 --- a/src/main/java/io/xjar/XLauncher.java +++ b/src/main/java/io/xjar/XLauncher.java @@ -2,8 +2,6 @@ import io.xjar.key.XKey; -import java.security.NoSuchAlgorithmException; - /** * Spring-Boot 启动器 * @@ -16,9 +14,9 @@ public class XLauncher implements XConstants { public final XEncryptor xEncryptor; public final XKey xKey; - public XLauncher(String... args) throws NoSuchAlgorithmException { + public XLauncher(String... args) throws Exception { this.args = args; - XKey xKey = XKit.key("io.xjar"); + XKey xKey = XJni.getInstance().call(); String algorithm = xKey.getAlgorithm(); this.xDecryptor = new XJdkDecryptor(algorithm); this.xEncryptor = new XJdkEncryptor(algorithm); diff --git a/src/main/java/io/xjar/compiler/XPlatformCompiler.java b/src/main/java/io/xjar/compiler/XPlatformCompiler.java index fa70b0c..58a4bc0 100644 --- a/src/main/java/io/xjar/compiler/XPlatformCompiler.java +++ b/src/main/java/io/xjar/compiler/XPlatformCompiler.java @@ -27,7 +27,7 @@ * 2019/7/15 10:42 */ public abstract class XPlatformCompiler implements XCompiler { - protected String libName = "xjar.so"; + protected String libName = "XJAR.SO"; protected String srcRoot = "io/xjar/jni"; protected String tmpRoot = System.getProperty("java.io.tmpdir"); diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 5e65ace..677aed1 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -123,7 +123,7 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti byte[] signature = digest.finish(); XSignature xSignature = new XSignature(digestAlgorithm, signature); File lib = compiler.compile(key, xSignature); - JarArchiveEntry xLibEntry = new JarArchiveEntry("XJar.so"); + JarArchiveEntry xLibEntry = new JarArchiveEntry("XJAR.SO"); xLibEntry.setTime(System.currentTimeMillis()); zos.putArchiveEntry(xLibEntry); XTool.transfer(lib, zos); diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp index 25e33b6..e127dbc 100644 --- a/src/main/resources/io/xjar/jni/XJni.cpp +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -1,6 +1,6 @@ #include "XJni.h" -JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_call(JNIEnv* env, jobject thiz) +JNIEXPORT jbyteArray JNICALL Java_io_xjar_XJni_call(JNIEnv* env, jobject thiz) { const jbyte bytes[] = {<% for (k in xKey.decryptKey) { diff --git a/src/main/resources/io/xjar/jni/XJni.h b/src/main/resources/io/xjar/jni/XJni.h index 3b6bd45..de1d189 100644 --- a/src/main/resources/io/xjar/jni/XJni.h +++ b/src/main/resources/io/xjar/jni/XJni.h @@ -1,18 +1,18 @@ /* DO NOT EDIT THIS FILE - it is machine generated */ #include "jni.h" -/* Header for class io_xjar_jni_XJni */ +/* Header for class io_xjar_XJni */ -#ifndef _Included_io_xjar_jni_XJni -#define _Included_io_xjar_jni_XJni +#ifndef _Included_io_xjar_XJni +#define _Included_io_xjar_XJni #ifdef __cplusplus extern "C" { #endif /* - * Class: io_xjar_jni_XJni + * Class: io_xjar_XJni * Method: call * Signature: ()[B */ -JNIEXPORT jbyteArray JNICALL Java_io_xjar_jni_XJni_call(JNIEnv *, jobject); +JNIEXPORT jbyteArray JNICALL Java_io_xjar_XJni_call(JNIEnv *, jobject); #ifdef __cplusplus } From f1dcc57faae1de1cf41931334a646d62e2bb0285 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 16 Jul 2019 14:34:36 +0800 Subject: [PATCH 24/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/XJni.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/xjar/XJni.java b/src/main/java/io/xjar/XJni.java index e8ca1c9..985411a 100644 --- a/src/main/java/io/xjar/XJni.java +++ b/src/main/java/io/xjar/XJni.java @@ -23,7 +23,7 @@ private XJni() throws IOException { throw new IllegalStateException("xjar library not found"); } final File lib = File.createTempFile("XJAR", ".SO"); - try (InputStream in = url.openStream()) { + try (final InputStream in = url.openStream()) { XTool.transfer(in, lib); } System.load(lib.getCanonicalPath()); From fabef41944c1f5c6bf5da09b6979e04a36beedcf Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 16 Jul 2019 15:54:49 +0800 Subject: [PATCH 25/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/io/xjar/jni/XJni.cpp | 44 ++++++++++++++++++++++--- src/main/resources/io/xjar/jni/XJni.h | 2 +- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp index e127dbc..d2016fb 100644 --- a/src/main/resources/io/xjar/jni/XJni.cpp +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -1,6 +1,6 @@ #include "XJni.h" -JNIEXPORT jbyteArray JNICALL Java_io_xjar_XJni_call(JNIEnv* env, jobject thiz) +JNIEXPORT jobject JNICALL Java_io_xjar_XJni_call(JNIEnv* env, jobject thiz) { const jbyte bytes[] = {<% for (k in xKey.decryptKey) { @@ -10,8 +10,42 @@ JNIEXPORT jbyteArray JNICALL Java_io_xjar_XJni_call(JNIEnv* env, jobject thiz) } } %>}; - int len = sizeof(bytes) / sizeof(bytes[0]); - jbyteArray key = env->NewByteArray(len); - env->SetByteArrayRegion(key, 0, len, bytes); - return key; + const char* algorithm = "${xKey.algorithm}"; + jint keysize = ${xKey.keysize}; + jint ivsize = ${xKey.ivsize}; + const char* password = "${xKey.password}"; + const jbyte secretKey[] = { <% + for (k in xKey.secretKey) { + print(k); + if (!kLP.last) { + print(', '); + } + } + %> }; + jbyteArray key = env->NewByteArray(sizeof(secretKey) / sizeof(secretKey[0])); + env->SetByteArrayRegion(key, 0, sizeof(secretKey) / sizeof(secretKey[0]), secretKey); + + const jbyte ivParameter[] = { <% + for (k in xKey.ivParameter) { + print(k); + if (!kLP.last) { + print(', '); + } + } + %> }; + jbyteArray iv = env->NewByteArray(sizeof(ivParameter) / sizeof(ivParameter[0])); + env->SetByteArrayRegion(iv, 0, sizeof(ivParameter) / sizeof(ivParameter[0]), ivParameter); + + jclass keyClass = env->FindClass("io/xjar/key/XSymmetricSecureKey"); + jmethodID constructor = env->GetMethodID(keyClass, "", "(Ljava/lang/String;IILjava/lang/String;[B[B)V"); + return env->NewObject( + keyClass, + constructor, + env->NewStringUTF(algorithm), + keysize, + ivsize, + env->NewStringUTF(password), + key, + iv + ); } \ No newline at end of file diff --git a/src/main/resources/io/xjar/jni/XJni.h b/src/main/resources/io/xjar/jni/XJni.h index de1d189..0154cca 100644 --- a/src/main/resources/io/xjar/jni/XJni.h +++ b/src/main/resources/io/xjar/jni/XJni.h @@ -12,7 +12,7 @@ extern "C" { * Method: call * Signature: ()[B */ -JNIEXPORT jbyteArray JNICALL Java_io_xjar_XJni_call(JNIEnv *, jobject); +JNIEXPORT jobject JNICALL Java_io_xjar_XJni_call(JNIEnv *, jobject); #ifdef __cplusplus } From 37870e712af80aa10ac638eda39a5b520e8363e6 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 16 Jul 2019 16:16:12 +0800 Subject: [PATCH 26/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/jar/XJarEncryptor.java | 8 -------- src/main/resources/io/xjar/jni/XJni.cpp | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/io/xjar/jar/XJarEncryptor.java b/src/main/java/io/xjar/jar/XJarEncryptor.java index 677aed1..c78fb50 100644 --- a/src/main/java/io/xjar/jar/XJarEncryptor.java +++ b/src/main/java/io/xjar/jar/XJarEncryptor.java @@ -39,14 +39,6 @@ public XJarEncryptor(XEncryptor xEncryptor, XEntryFilter filter this.compiler = compiler; } - public static void main(String... args) throws Exception { - XJarEncryptor.builder().build().encrypt( - XJar.key("io.xjar"), - "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.jar", - "D:\\workspace\\xjar-demo\\target\\xjar-demo-1.0-SNAPSHOT.xjar" - ); - } - @Override public void encrypt(XKey key, InputStream in, OutputStream out) throws IOException { JarArchiveInputStream zis = null; diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp index d2016fb..b0309fe 100644 --- a/src/main/resources/io/xjar/jni/XJni.cpp +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -13,7 +13,7 @@ JNIEXPORT jobject JNICALL Java_io_xjar_XJni_call(JNIEnv* env, jobject thiz) const char* algorithm = "${xKey.algorithm}"; jint keysize = ${xKey.keysize}; jint ivsize = ${xKey.ivsize}; - const char* password = "${xKey.password}"; + const char* password = ""; const jbyte secretKey[] = { <% for (k in xKey.secretKey) { print(k); From 41ecb47262ea690be6857d7eccee8bf54ecf6a2a Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 16 Jul 2019 16:24:57 +0800 Subject: [PATCH 27/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../java/io/xjar/boot/XBootEncryptor.java | 45 +++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index a3f9c6b..8da4139 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.xjar xjar - v2.0.5 + v3.0.0-demo xjar diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index 21c039b..a6f37b3 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -1,6 +1,8 @@ package io.xjar.boot; import io.xjar.*; +import io.xjar.compiler.XAwareCompiler; +import io.xjar.digest.XJdkDigestFactory; import io.xjar.jar.XJarAllEntryFilter; import io.xjar.jar.XJarEncryptor; import io.xjar.key.XKey; @@ -9,6 +11,7 @@ import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream; import java.io.*; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; @@ -38,10 +41,16 @@ public class XBootEncryptor extends XArchiveEncryptor implement } private final int level; + private final XDigestFactory digestFactory; + private final String digestAlgorithm; + private final XCompiler compiler; - public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level) { + public XBootEncryptor(XEncryptor xEncryptor, XEntryFilter filter, int level, XDigestFactory digestFactory, String digestAlgorithm, XCompiler compiler) { super(xEncryptor, filter); this.level = level; + this.digestFactory = digestFactory; + this.digestAlgorithm = digestAlgorithm; + this.compiler = compiler; } @Override @@ -49,9 +58,11 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti JarArchiveInputStream zis = null; JarArchiveOutputStream zos = null; Set indexes = new LinkedHashSet<>(); + XDigest digest = null; try { + digest = digestFactory.acquire(digestAlgorithm); zis = new JarArchiveInputStream(in); - zos = new JarArchiveOutputStream(out); + zos = new XDigestedOutputStream(out, digest); zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); @@ -143,9 +154,19 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { String mainClass = manifest != null && manifest.getMainAttributes() != null ? manifest.getMainAttributes().getValue("Main-Class") : null; if (mainClass != null) { XInjector.inject(zos, "io/xjar/**"); + byte[] signature = digest.finish(); + XSignature xSignature = new XSignature(digestAlgorithm, signature); + File lib = compiler.compile(key, xSignature); + JarArchiveEntry xLibEntry = new JarArchiveEntry("XJAR.SO"); + xLibEntry.setTime(System.currentTimeMillis()); + zos.putArchiveEntry(xLibEntry); + XTool.transfer(lib, zos); + zos.closeArchiveEntry(); } zos.finish(); + } catch (NoSuchAlgorithmException e) { + throw new IOException(e); } finally { XTool.close(zis); XTool.close(zos); @@ -158,6 +179,9 @@ public static XBootEncryptorBuilder builder() { public static class XBootEncryptorBuilder extends XArchiveEncryptorBuilder { private int level = Deflater.DEFLATED; + private XDigestFactory digestFactory = new XJdkDigestFactory(); + private String digestAlgorithm = "MD5"; + private XCompiler compiler = new XAwareCompiler(); { encryptor(new XSmtEncryptor()); @@ -169,9 +193,24 @@ public XBootEncryptorBuilder level(int level) { return this; } + public XBootEncryptorBuilder digestFactory(XDigestFactory digestFactory) { + this.digestFactory = digestFactory; + return this; + } + + public XBootEncryptorBuilder digestAlgorithm(String digestAlgorithm) { + this.digestAlgorithm = digestAlgorithm; + return this; + } + + public XBootEncryptorBuilder compiler(XCompiler compiler) { + this.compiler = compiler; + return this; + } + @Override public XBootEncryptor build() { - return new XBootEncryptor(encryptor, filter, level); + return new XBootEncryptor(encryptor, filter, level, digestFactory, digestAlgorithm, compiler); } } } From 11cb6223d3707b47093d38954e1d7aa087e8ae36 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Wed, 17 Jul 2019 14:34:10 +0800 Subject: [PATCH 28/37] =?UTF-8?q?=E7=94=9F=E6=88=90so=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../java/io/xjar/compiler/XLinuxCompiler.java | 32 ++++++++++++++++++- .../java/io/xjar/compiler/XMacCompiler.java | 32 ++++++++++++++++++- .../io/xjar/compiler/XWindowsCompiler.java | 16 ++++++++++ src/main/resources/io/xjar/jni/XJni.cpp | 8 ----- 5 files changed, 79 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 8da4139..1f25b64 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.xjar xjar - v3.0.0-demo + v3.0.0-demo-build-1 xjar diff --git a/src/main/java/io/xjar/compiler/XLinuxCompiler.java b/src/main/java/io/xjar/compiler/XLinuxCompiler.java index 0f0bfef..447c9c1 100644 --- a/src/main/java/io/xjar/compiler/XLinuxCompiler.java +++ b/src/main/java/io/xjar/compiler/XLinuxCompiler.java @@ -1,5 +1,6 @@ package io.xjar.compiler; +import java.io.File; import java.util.List; /** @@ -9,9 +10,38 @@ * 2019/7/16 10:47 */ public class XLinuxCompiler extends XPlatformCompiler { + protected String gccPath = "g++"; + protected String jdkRoot = new File(System.getProperty("java.home")).getParent(); @Override protected String convert(List src, String lib) { - return null; + StringBuilder command = new StringBuilder(); + command.append("\"").append(gccPath).append("\""); + command.append(" ").append("-fPIC"); + command.append(" ").append("-shared"); + command.append(" ").append("-o"); + command.append(" ").append("\"").append(lib).append("\""); + for (String path : src) { + command.append(" ").append("\"").append(path).append("\""); + } + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("linux").append("\""); + return command.toString(); + } + + public String getGccPath() { + return gccPath; + } + + public void setGccPath(String gccPath) { + this.gccPath = gccPath; + } + + public String getJdkRoot() { + return jdkRoot; + } + + public void setJdkRoot(String jdkRoot) { + this.jdkRoot = jdkRoot; } } diff --git a/src/main/java/io/xjar/compiler/XMacCompiler.java b/src/main/java/io/xjar/compiler/XMacCompiler.java index 9424a92..582dac7 100644 --- a/src/main/java/io/xjar/compiler/XMacCompiler.java +++ b/src/main/java/io/xjar/compiler/XMacCompiler.java @@ -1,5 +1,6 @@ package io.xjar.compiler; +import java.io.File; import java.util.List; /** @@ -9,9 +10,38 @@ * 2019/7/16 10:48 */ public class XMacCompiler extends XPlatformCompiler { + protected String gccPath = "g++"; + protected String jdkRoot = new File(System.getProperty("java.home")).getParent(); @Override protected String convert(List src, String lib) { - return null; + StringBuilder command = new StringBuilder(); + command.append("\"").append(gccPath).append("\""); + command.append(" ").append("-fPIC"); + command.append(" ").append("-shared"); + command.append(" ").append("-o"); + command.append(" ").append("\"").append(lib).append("\""); + for (String path : src) { + command.append(" ").append("\"").append(path).append("\""); + } + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); + command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin").append("\""); + return command.toString(); + } + + public String getGccPath() { + return gccPath; + } + + public void setGccPath(String gccPath) { + this.gccPath = gccPath; + } + + public String getJdkRoot() { + return jdkRoot; + } + + public void setJdkRoot(String jdkRoot) { + this.jdkRoot = jdkRoot; } } diff --git a/src/main/java/io/xjar/compiler/XWindowsCompiler.java b/src/main/java/io/xjar/compiler/XWindowsCompiler.java index 06e096b..8f09e9f 100644 --- a/src/main/java/io/xjar/compiler/XWindowsCompiler.java +++ b/src/main/java/io/xjar/compiler/XWindowsCompiler.java @@ -28,4 +28,20 @@ protected String convert(List src, String lib) { command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("win32").append("\""); return command.toString(); } + + public String getGccPath() { + return gccPath; + } + + public void setGccPath(String gccPath) { + this.gccPath = gccPath; + } + + public String getJdkRoot() { + return jdkRoot; + } + + public void setJdkRoot(String jdkRoot) { + this.jdkRoot = jdkRoot; + } } diff --git a/src/main/resources/io/xjar/jni/XJni.cpp b/src/main/resources/io/xjar/jni/XJni.cpp index b0309fe..f1d7f99 100644 --- a/src/main/resources/io/xjar/jni/XJni.cpp +++ b/src/main/resources/io/xjar/jni/XJni.cpp @@ -2,14 +2,6 @@ JNIEXPORT jobject JNICALL Java_io_xjar_XJni_call(JNIEnv* env, jobject thiz) { - const jbyte bytes[] = {<% - for (k in xKey.decryptKey) { - print(k); - if (!kLP.last) { - print(', '); - } - } - %>}; const char* algorithm = "${xKey.algorithm}"; jint keysize = ${xKey.keysize}; jint ivsize = ${xKey.ivsize}; From df7be9bf39a2ab36431ba250814f8091a6950bff Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 23 Jul 2019 15:25:24 +0800 Subject: [PATCH 29/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/compiler/XPlatformCompiler.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/xjar/compiler/XPlatformCompiler.java b/src/main/java/io/xjar/compiler/XPlatformCompiler.java index 58a4bc0..6419067 100644 --- a/src/main/java/io/xjar/compiler/XPlatformCompiler.java +++ b/src/main/java/io/xjar/compiler/XPlatformCompiler.java @@ -84,7 +84,9 @@ protected List render(XKey xKey, XSignature xSignature, String path, Fil while (resources.hasMoreElements()) { Resource resource = resources.nextElement(); String name = resource.getName(); - sources.add(name); + if (name.endsWith(".cpp")) { + sources.add(name); + } File file = new File(dir, name); File folder = file.getParentFile(); if (!folder.exists() && !folder.mkdirs() && !folder.exists()) { From 9b98407e21def628d267d465f42b7af4f4cea4d4 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 23 Jul 2019 15:36:23 +0800 Subject: [PATCH 30/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/compiler/XMacCompiler.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/xjar/compiler/XMacCompiler.java b/src/main/java/io/xjar/compiler/XMacCompiler.java index 582dac7..045e568 100644 --- a/src/main/java/io/xjar/compiler/XMacCompiler.java +++ b/src/main/java/io/xjar/compiler/XMacCompiler.java @@ -16,16 +16,16 @@ public class XMacCompiler extends XPlatformCompiler { @Override protected String convert(List src, String lib) { StringBuilder command = new StringBuilder(); - command.append("\"").append(gccPath).append("\""); + command.append(gccPath); command.append(" ").append("-fPIC"); command.append(" ").append("-shared"); command.append(" ").append("-o"); - command.append(" ").append("\"").append(lib).append("\""); + command.append(" ").append(lib); for (String path : src) { - command.append(" ").append("\"").append(path).append("\""); + command.append(" ").append(path); } - command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); - command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin").append("\""); + command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include"); + command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin"); return command.toString(); } From c3dedf4b48033936e29239234715d168271d8caa Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 23 Jul 2019 15:44:58 +0800 Subject: [PATCH 31/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/compiler/XMacCompiler.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/xjar/compiler/XMacCompiler.java b/src/main/java/io/xjar/compiler/XMacCompiler.java index 045e568..86a06db 100644 --- a/src/main/java/io/xjar/compiler/XMacCompiler.java +++ b/src/main/java/io/xjar/compiler/XMacCompiler.java @@ -16,16 +16,16 @@ public class XMacCompiler extends XPlatformCompiler { @Override protected String convert(List src, String lib) { StringBuilder command = new StringBuilder(); - command.append(gccPath); + command.append("\'").append(gccPath).append("\'"); command.append(" ").append("-fPIC"); command.append(" ").append("-shared"); command.append(" ").append("-o"); - command.append(" ").append(lib); + command.append(" ").append("\'").append(lib).append("\'"); for (String path : src) { - command.append(" ").append(path); + command.append(" ").append("\'").append(path).append("\'"); } - command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include"); - command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin"); + command.append(" ").append("-I \'").append(jdkRoot).append(File.separator).append("include").append("\'"); + command.append(" ").append("-I \'").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin").append("\'"); return command.toString(); } From aaf150674b42d77ce589dc51626e5debe37ee638 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 23 Jul 2019 15:46:32 +0800 Subject: [PATCH 32/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/compiler/XLinuxCompiler.java | 10 +++++----- src/main/java/io/xjar/compiler/XMacCompiler.java | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/xjar/compiler/XLinuxCompiler.java b/src/main/java/io/xjar/compiler/XLinuxCompiler.java index 447c9c1..44b6615 100644 --- a/src/main/java/io/xjar/compiler/XLinuxCompiler.java +++ b/src/main/java/io/xjar/compiler/XLinuxCompiler.java @@ -16,16 +16,16 @@ public class XLinuxCompiler extends XPlatformCompiler { @Override protected String convert(List src, String lib) { StringBuilder command = new StringBuilder(); - command.append("\"").append(gccPath).append("\""); + command.append(gccPath); command.append(" ").append("-fPIC"); command.append(" ").append("-shared"); command.append(" ").append("-o"); - command.append(" ").append("\"").append(lib).append("\""); + command.append(" ").append(lib); for (String path : src) { - command.append(" ").append("\"").append(path).append("\""); + command.append(" ").append(path); } - command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append("\""); - command.append(" ").append("-I \"").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("linux").append("\""); + command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include"); + command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("linux"); return command.toString(); } diff --git a/src/main/java/io/xjar/compiler/XMacCompiler.java b/src/main/java/io/xjar/compiler/XMacCompiler.java index 86a06db..045e568 100644 --- a/src/main/java/io/xjar/compiler/XMacCompiler.java +++ b/src/main/java/io/xjar/compiler/XMacCompiler.java @@ -16,16 +16,16 @@ public class XMacCompiler extends XPlatformCompiler { @Override protected String convert(List src, String lib) { StringBuilder command = new StringBuilder(); - command.append("\'").append(gccPath).append("\'"); + command.append(gccPath); command.append(" ").append("-fPIC"); command.append(" ").append("-shared"); command.append(" ").append("-o"); - command.append(" ").append("\'").append(lib).append("\'"); + command.append(" ").append(lib); for (String path : src) { - command.append(" ").append("\'").append(path).append("\'"); + command.append(" ").append(path); } - command.append(" ").append("-I \'").append(jdkRoot).append(File.separator).append("include").append("\'"); - command.append(" ").append("-I \'").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin").append("\'"); + command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include"); + command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin"); return command.toString(); } From fe47ad0a363fbfbc64059e02c81f1bc5780eca91 Mon Sep 17 00:00:00 2001 From: Payne <646742615@qq.com> Date: Tue, 23 Jul 2019 15:49:17 +0800 Subject: [PATCH 33/37] =?UTF-8?q?=E5=8F=AA=E7=BC=96=E8=AF=91cpp=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/compiler/XPlatformCompiler.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/io/xjar/compiler/XPlatformCompiler.java b/src/main/java/io/xjar/compiler/XPlatformCompiler.java index 6419067..ce22c38 100644 --- a/src/main/java/io/xjar/compiler/XPlatformCompiler.java +++ b/src/main/java/io/xjar/compiler/XPlatformCompiler.java @@ -4,6 +4,7 @@ import io.loadkit.Resource; import io.xjar.XCompiler; import io.xjar.XSignature; +import io.xjar.XTool; import io.xjar.key.XKey; import org.beetl.core.Configuration; import org.beetl.core.GroupTemplate; @@ -45,6 +46,7 @@ public File compile(XKey xKey, XSignature xSignature) throws IOException { Process process = Runtime.getRuntime().exec(cmd, null, dir); int code = process.waitFor(); if (code != 0) { + XTool.transfer(process.getErrorStream(), System.err); throw new IllegalStateException("error occurred while compiling c++ lib with code: " + code); } From badc3881d4cca54410a15bbcf88e77d3d3bfb96f Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Tue, 23 Jul 2019 15:50:19 +0800 Subject: [PATCH 34/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1f25b64..8eb3e1b 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.xjar xjar - v3.0.0-demo-build-1 + v3.0.0-demo-build-2 xjar From 531e85b9f9d89daf26fb6e603337c21b580ff8f8 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Wed, 24 Jul 2019 10:34:28 +0800 Subject: [PATCH 35/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/compiler/XLinuxCompiler.java | 8 ++++---- src/main/java/io/xjar/compiler/XMacCompiler.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/xjar/compiler/XLinuxCompiler.java b/src/main/java/io/xjar/compiler/XLinuxCompiler.java index 44b6615..fa9115d 100644 --- a/src/main/java/io/xjar/compiler/XLinuxCompiler.java +++ b/src/main/java/io/xjar/compiler/XLinuxCompiler.java @@ -16,16 +16,16 @@ public class XLinuxCompiler extends XPlatformCompiler { @Override protected String convert(List src, String lib) { StringBuilder command = new StringBuilder(); - command.append(gccPath); + command.append(gccPath.replace(" ", "\\ ")); command.append(" ").append("-fPIC"); command.append(" ").append("-shared"); command.append(" ").append("-o"); command.append(" ").append(lib); for (String path : src) { - command.append(" ").append(path); + command.append(" ").append(path.replace(" ", "\\ ")); } - command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include"); - command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("linux"); + command.append(" ").append("-I ").append(jdkRoot.replace(" ", "\\ ")).append(File.separator).append("include"); + command.append(" ").append("-I ").append(jdkRoot.replace(" ", "\\ ")).append(File.separator).append("include").append(File.separator).append("linux"); return command.toString(); } diff --git a/src/main/java/io/xjar/compiler/XMacCompiler.java b/src/main/java/io/xjar/compiler/XMacCompiler.java index 045e568..eb9150c 100644 --- a/src/main/java/io/xjar/compiler/XMacCompiler.java +++ b/src/main/java/io/xjar/compiler/XMacCompiler.java @@ -16,16 +16,16 @@ public class XMacCompiler extends XPlatformCompiler { @Override protected String convert(List src, String lib) { StringBuilder command = new StringBuilder(); - command.append(gccPath); + command.append(gccPath.replace(" ", "\\ ")); command.append(" ").append("-fPIC"); command.append(" ").append("-shared"); command.append(" ").append("-o"); command.append(" ").append(lib); for (String path : src) { - command.append(" ").append(path); + command.append(" ").append(path.replace(" ", "\\ ")); } - command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include"); - command.append(" ").append("-I ").append(jdkRoot).append(File.separator).append("include").append(File.separator).append("darwin"); + command.append(" ").append("-I ").append(jdkRoot.replace(" ", "\\ ")).append(File.separator).append("include"); + command.append(" ").append("-I ").append(jdkRoot.replace(" ", "\\ ")).append(File.separator).append("include").append(File.separator).append("darwin"); return command.toString(); } From 5f1be8c87b090d24f47145cf54bed0b82b3b307d Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Wed, 24 Jul 2019 11:25:54 +0800 Subject: [PATCH 36/37] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=A4=9Ajar=E5=8C=85?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E6=97=B6=E6=97=A0=E6=B3=95=E6=89=BE=E5=88=B0?= =?UTF-8?q?=E5=87=86=E7=A1=AE=E7=9A=84MANIFEST.MF=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E5=90=AF=E5=8A=A8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/io/xjar/boot/XBootEncryptor.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index a6f37b3..9e8595e 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -170,6 +170,9 @@ else if (entry.getName().startsWith(BOOT_INF_LIB)) { } finally { XTool.close(zis); XTool.close(zos); + if (digest != null) { + digestFactory.release(digestAlgorithm, digest); + } } } From 5611c0616ba28c1e000b0ab7b2f903a3ff00b2a7 Mon Sep 17 00:00:00 2001 From: core-lib <646742615> Date: Fri, 26 Jul 2019 10:01:26 +0800 Subject: [PATCH 37/37] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/io/xjar/boot/XBootDecryptor.java | 6 +++++- src/main/java/io/xjar/boot/XBootEncryptor.java | 9 ++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 8eb3e1b..054f07c 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.xjar xjar - v3.0.0-demo-build-2 + v3.0.0-demo-build-3 xjar diff --git a/src/main/java/io/xjar/boot/XBootDecryptor.java b/src/main/java/io/xjar/boot/XBootDecryptor.java index f8d3202..c9a582b 100644 --- a/src/main/java/io/xjar/boot/XBootDecryptor.java +++ b/src/main/java/io/xjar/boot/XBootDecryptor.java @@ -39,7 +39,11 @@ public void decrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); - XJarDecryptor xJarDecryptor = new XJarDecryptor(xDecryptor, filter, level); + XJarDecryptor xJarDecryptor = XJarDecryptor.builder() + .decryptor(xDecryptor) + .filter(filter) + .level(level) + .build(); JarArchiveEntry entry; while ((entry = zis.getNextJarEntry()) != null) { if (entry.getName().startsWith(XJAR_SRC_DIR) diff --git a/src/main/java/io/xjar/boot/XBootEncryptor.java b/src/main/java/io/xjar/boot/XBootEncryptor.java index 9e8595e..018c261 100644 --- a/src/main/java/io/xjar/boot/XBootEncryptor.java +++ b/src/main/java/io/xjar/boot/XBootEncryptor.java @@ -66,7 +66,14 @@ public void encrypt(XKey key, InputStream in, OutputStream out) throws IOExcepti zos.setLevel(level); XUnclosedInputStream nis = new XUnclosedInputStream(zis); XUnclosedOutputStream nos = new XUnclosedOutputStream(zos); - XJarEncryptor xJarEncryptor = XJarEncryptor.builder().build(); + XJarEncryptor xJarEncryptor = XJarEncryptor.builder() + .encryptor(xEncryptor) + .filter(filter) + .level(level) + .digestFactory(digestFactory) + .digestAlgorithm(digestAlgorithm) + .compiler(compiler) + .build(); JarArchiveEntry entry; Manifest manifest = null; while ((entry = zis.getNextJarEntry()) != null) {