Skip to content

Commit a6242c0

Browse files
committed
vue sample update
1 parent 52b99d5 commit a6242c0

File tree

5 files changed

+321
-79
lines changed

5 files changed

+321
-79
lines changed

core/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@
281281
<version>3.25.0-GA</version>
282282
<optional>true</optional>
283283
</dependency>
284+
<dependency>
285+
<groupId>org.springframework.boot</groupId>
286+
<artifactId>spring-boot-loader</artifactId>
287+
<version>${spring.boot.version}</version>
288+
<optional>true</optional>
289+
</dependency>
284290
</dependencies>
285291

286292

core/src/main/java/com/robin/core/encrypt/CipherUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public static byte[] decryptByte(byte[] bytes, byte[] key) {
111111

112112
public static void decryptByte(byte[] key, InputStream is, OutputStream os) {
113113
try {
114-
Cipher cipher = Cipher.getInstance(DEFAULTALGORITHM);
114+
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
115115
cipher.init(Cipher.DECRYPT_MODE, toKey(key));
116116
//CipherInputStream cis=new CipherInputStream(is, cipher);
117117
CipherOutputStream out = new CipherOutputStream(os, cipher);

core/src/main/java/com/robin/core/encrypt/EncryptClassLoader.java

Lines changed: 73 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,34 @@
22

33
import org.apache.commons.lang3.tuple.Pair;
44

5-
import java.io.ByteArrayOutputStream;
6-
import java.io.DataInputStream;
7-
import java.io.File;
8-
import java.io.InputStream;
5+
import javax.crypto.Cipher;
6+
import javax.crypto.CipherOutputStream;
7+
import javax.crypto.SecretKey;
8+
import javax.crypto.SecretKeyFactory;
9+
import javax.crypto.spec.SecretKeySpec;
10+
import java.io.*;
911
import java.net.URL;
1012
import java.net.URLClassLoader;
11-
import java.util.HashMap;
12-
import java.util.Map;
13+
import java.security.NoSuchAlgorithmException;
14+
import java.security.spec.InvalidKeySpecException;
15+
import java.util.*;
1316
import java.util.stream.Collectors;
1417
import java.util.stream.Stream;
1518

1619

1720
public class EncryptClassLoader extends URLClassLoader {
1821
private ClassLoader superloader;
19-
private byte[] key = null;
2022
private Map<String, Class> loadedClassPool = new HashMap<>();
2123
private Map<String, Pair<String, String>> encryptKeyMap = new HashMap<>();
2224
private String machineCode;
2325
private Long expireTs;
26+
private static final String DEFAULTALGORITHM = "AES";
27+
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
28+
public static final byte[] m_datapadding = {0x7F};
29+
public static final byte[] m_ending = {0x00};
30+
private static final String[] CONFUSEDSTRS = {"i", "I", "l", "O", "0", "1"};
2431

32+
public static final byte[] mzHeader = {0x4D, 0x5A, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2533
public EncryptClassLoader(URL[] urls, ClassLoader parent) {
2634
super(urls, parent);
2735
this.superloader = parent;
@@ -36,13 +44,13 @@ public EncryptClassLoader(ClassLoader loader) {
3644

3745
private void init() {
3846
try (DataInputStream dInput = new DataInputStream(superloader.getResourceAsStream("META-INF/config.bin"))) {
39-
dInput.read(CipherUtil.mzHeader);
47+
dInput.read(mzHeader);
4048
byte[] paddingbyte = new byte[1];
4149
dInput.read(paddingbyte);
4250
checkPadding(paddingbyte);
4351
byte[] machineCodeByte = new byte[16];
4452
dInput.read(machineCodeByte);
45-
machineCode = CipherUtil.bytesToHexString(machineCodeByte);
53+
machineCode = bytesToHexString(machineCodeByte);
4654
expireTs = dInput.readLong();
4755
checkExpire();
4856
int classNameBytesLen = 0;
@@ -52,13 +60,13 @@ private void init() {
5260
classNameBytesLen = dInput.readInt();
5361
byte[] classNameEncryptBytes = new byte[classNameBytesLen];
5462
dInput.read(classNameEncryptBytes);
55-
byte[] decryptClassNameBytes = CipherUtil.decryptByte(classNameEncryptBytes, machineCode.getBytes());
63+
byte[] decryptClassNameBytes = decryptByte(classNameEncryptBytes, machineCode.getBytes());
5664
String className = new String(decryptClassNameBytes);
57-
String confusedName = CipherUtil.decodeConfusedNameByCode(String.valueOf(dInput.readLong()));
65+
String confusedName = decodeConfusedNameByCode(String.valueOf(dInput.readLong()));
5866
int keyLength = dInput.readInt();
5967
byte[] keyEncryptByte = new byte[keyLength];
6068
dInput.read(keyEncryptByte);
61-
byte[] keyDecryptByte = CipherUtil.decryptByte(keyEncryptByte, machineCode.getBytes());
69+
byte[] keyDecryptByte = decryptByte(keyEncryptByte, machineCode.getBytes());
6270
String key = new String(keyDecryptByte);
6371
encryptKeyMap.put(className, Pair.of(confusedName, key));
6472
}
@@ -68,7 +76,7 @@ private void init() {
6876
}
6977

7078
private void checkPadding(byte[] paddingbyte) {
71-
if (paddingbyte != CipherUtil.m_datapadding) {
79+
if (paddingbyte != m_datapadding) {
7280
System.err.println("jar package corrupted");
7381
System.exit(1);
7482
}
@@ -126,9 +134,9 @@ protected synchronized Class<?> loadClass(String name, boolean resolve)
126134
InputStream in = loadEncryptDataStream(encryptKeyMap.get(classname).getKey());
127135
if (in != null) {
128136
ByteArrayOutputStream out = new ByteArrayOutputStream();
129-
CipherUtil.decryptByte(machineCode.getBytes(), in, out);
137+
decryptByte(machineCode.getBytes(), in, out);
130138
byte[] bytes = out.toByteArray();
131-
byte[] decrypt=CipherUtil.decryptByte(bytes,encryptKeyMap.get(classname).getValue().getBytes());
139+
byte[] decrypt=decryptByte(bytes,encryptKeyMap.get(classname).getValue().getBytes());
132140
if (bytes != null) {
133141
clazz = defineClass(name, decrypt, 0, bytes.length);
134142
}
@@ -149,82 +157,75 @@ protected synchronized Class<?> loadClass(String name, boolean resolve)
149157
return null;
150158
}
151159

152-
private Class loadByEncryptClass(String name) {
153-
return null;
154160

161+
private InputStream loadEncryptDataStream(String confusedName) {
162+
return superloader.getResourceAsStream("META-INF/ext/" + confusedName);
155163
}
156164

157-
public byte[] encrptClass(String name) {
158-
try {
159-
return CipherUtil.encryptByte(loadClassData(name), key);
160-
} catch (Exception ex) {
161-
ex.printStackTrace();
165+
166+
private static void doCopy(InputStream is, OutputStream os) throws IOException {
167+
byte[] bytes = new byte[64];
168+
int numBytes;
169+
while ((numBytes = is.read(bytes)) != -1) {
170+
os.write(bytes, 0, numBytes);
162171
}
163-
return null;
172+
os.flush();
173+
os.close();
174+
is.close();
164175
}
165-
166-
public byte[] decrptClass(String name) {
176+
private static byte[] decryptByte(byte[] bytes, byte[] key) {
167177
try {
168-
return CipherUtil.decryptByte(loadClassData(name), key);
178+
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
179+
cipher.init(Cipher.DECRYPT_MODE, toKey(key));
180+
return cipher.doFinal(bytes);
169181
} catch (Exception ex) {
170182
ex.printStackTrace();
171183
}
172184
return null;
173185
}
174-
175-
private byte[] loadClassData(String name) {
176-
try {
177-
String tmpname = name;
178-
if (tmpname.contains(".")) {
179-
tmpname = tmpname.replaceAll("\\.", "/");
180-
}
181-
if (!tmpname.endsWith(".class")) {
182-
tmpname += ".class";
183-
}
184-
InputStream in = superloader.getResourceAsStream(tmpname);
185-
byte[] bytes = new byte[in.available()];
186-
in.read(bytes);
187-
return bytes;
188-
} catch (Exception ex) {
189-
System.out.println(name);
190-
ex.printStackTrace();
191-
return null;
186+
private static String bytesToHexString(byte[] bytes) {
187+
StringBuilder builder = new StringBuilder();
188+
for (byte b : bytes) {
189+
builder.append(String.format("%02X", b));
192190
}
191+
return builder.toString();
193192
}
194193

195-
private InputStream loadEncryptDataStream(String confusedName) {
196-
return superloader.getResourceAsStream("META-INF/ext/" + confusedName);
197-
}
198-
199-
private InputStream loadClassDataStream(String name) {
200-
InputStream in = null;
194+
private static void decryptByte(byte[] key, InputStream is, OutputStream os) {
201195
try {
202-
String tmpname = name;
203-
if (tmpname.contains(".")) {
204-
tmpname = tmpname.replaceAll("\\.", "/");
205-
}
206-
if (!tmpname.endsWith(".class")) {
207-
tmpname += ".class";
208-
}
209-
in = superloader.getResourceAsStream(tmpname);
196+
Cipher cipher = Cipher.getInstance(DEFAULTALGORITHM);
197+
cipher.init(Cipher.DECRYPT_MODE, toKey(key));
198+
//CipherInputStream cis=new CipherInputStream(is, cipher);
199+
CipherOutputStream out = new CipherOutputStream(os, cipher);
200+
doCopy(is, out);
210201
} catch (Exception ex) {
211-
System.out.println(name);
212202
ex.printStackTrace();
213-
214203
}
215-
return in;
216204
}
217-
218-
public static void main(String[] args) {
219-
220-
}
221-
222-
public byte[] getKey() {
223-
return key;
224-
}
225-
226-
public void setKey(byte[] key) {
227-
this.key = key;
205+
private static SecretKey toKey(byte[] keybyte) throws NoSuchAlgorithmException, InvalidKeySpecException {
206+
SecretKeySpec key = new SecretKeySpec(keybyte, DEFAULTALGORITHM);
207+
SecretKeyFactory skf = SecretKeyFactory.getInstance(DEFAULTALGORITHM);
208+
return skf.generateSecret(key);
209+
}
210+
private static List<String> getConfusedName(int length, Random random) {
211+
StringBuilder builder = new StringBuilder();
212+
StringBuilder builder1 = new StringBuilder();
213+
for (int i = 0; i < length; i++) {
214+
int pos = random.nextInt(CONFUSEDSTRS.length);
215+
builder1.append(pos);
216+
builder.append(CONFUSEDSTRS[pos]);
217+
}
218+
List<String> retList = new ArrayList<>();
219+
retList.add(builder.toString());
220+
retList.add(builder1.toString());
221+
return retList;
222+
}
223+
private static String decodeConfusedNameByCode(String code){
224+
StringBuilder builder = new StringBuilder();
225+
for(char input:code.toCharArray()){
226+
builder.append(CONFUSEDSTRS[Integer.parseInt(String.valueOf(input))]);
227+
}
228+
return builder.toString();
228229
}
229230

230231

core/src/main/java/com/robin/core/encrypt/JarRepackager.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javassist.ClassPool;
88
import lombok.extern.slf4j.Slf4j;
99
import org.apache.commons.io.IOUtils;
10+
import org.springframework.util.Assert;
1011

1112
import java.io.*;
1213
import java.math.BigInteger;
@@ -61,17 +62,29 @@ public static void repackage(String inputJarFiles,String outputJarFile,String ma
6162
dout.writeInt(classNameBytes.length);
6263
dout.write(classNameBytes);
6364
byte[] outbyte = CipherUtil.encryptByte(bytes, keystr.getBytes());
64-
outbyte=CipherUtil.encryptByte(outbyte,CipherUtil.getEncryptKey(machineId.toUpperCase().getBytes()));
65+
byte[] encrypted=CipherUtil.encryptByte(outbyte,CipherUtil.getEncryptKey(machineId.toUpperCase().getBytes()));
66+
ByteArrayOutputStream output1=new ByteArrayOutputStream();
67+
68+
CipherUtil.decryptByte(machineId.getBytes(),new ByteArrayInputStream(encrypted),output1);
69+
70+
byte[] origin=CipherUtil.decryptByte(output1.toByteArray(),keystr.getBytes());
71+
72+
6573
List<String> confusedNames=CipherUtil.getConfusedName(16,random);
6674
outputStream.putNextEntry(new JarEntry(basePath + confusedNames.get(0)));
6775
dout.writeLong(Long.valueOf(confusedNames.get(1)));
6876
dout.writeInt(keybytes.length);
6977
dout.write(keybytes);
70-
IOUtils.copy(new ByteArrayInputStream(outbyte), outputStream, 8094);
78+
IOUtils.write(encrypted, outputStream);
7179
//方法体清理
72-
byte[] clearBytes=JarMethodClearUtils.rewriteAllMethods(pool,packageName+"."+clazzName);
73-
outputStream.putNextEntry(new JarEntry(entry.getName()));
74-
IOUtils.write(clearBytes, outputStream);
80+
if(!packageName.equals("com.robin.spring")) {
81+
byte[] clearBytes = JarMethodClearUtils.rewriteAllMethods(pool, packageName + "." + clazzName);
82+
outputStream.putNextEntry(new JarEntry(entry.getName()));
83+
IOUtils.write(clearBytes, outputStream);
84+
}else{
85+
outputStream.putNextEntry(new JarEntry(entry.getName()));
86+
IOUtils.write(bytes, outputStream);
87+
}
7588
}else {
7689
outputStream.putNextEntry(new JarEntry(entry.getName()));
7790
IOUtils.copy(inputStream, outputStream, 1024);
@@ -92,7 +105,7 @@ private static byte[] getZipByte(ZipInputStream inputStream) throws IOException
92105
return bArray.toByteArray();
93106
}
94107
public static void main(String[] args){
95-
repackage("E:/dev/workspaceframe/JavaFramework/core/target/core-1.0.jar","e:/tmp/encrypt.jar","E:/dev/workspaceframe/JavaFramework/core","META-INF/ext/", MachineIdUtils.getMachineId().replace("-",""),System.currentTimeMillis()+365*3600*24*1000L);
108+
repackage("E:/dev/workspaceframe/JavaFramework/core/target/core-1.0_proguard_base.jar","e:/tmp/encrypt.jar","E:/dev/workspaceframe/JavaFramework/core","META-INF/ext/", MachineIdUtils.getMachineId().replace("-","").toUpperCase(),System.currentTimeMillis()+365*3600*24*1000L);
96109

97110
}
98111
}

0 commit comments

Comments
 (0)