├── .idea ├── PluginEmpowerSystem.iml ├── artifacts │ └── Empower.xml ├── compiler.xml ├── encodings.xml ├── misc.xml ├── uiDesigner.xml └── vcs.xml ├── Empower.jar ├── PluginEmpowerSystem.iml ├── README.md ├── Test ├── config.txt ├── empower-with-config.zip ├── hello.key ├── key.yml ├── pom.xml ├── src ├── main │ ├── java │ │ └── net │ │ │ └── noyark │ │ │ └── www │ │ │ └── utils │ │ │ ├── CodeReflectSet.java │ │ │ ├── Coder.java │ │ │ ├── DBTypes.java │ │ │ ├── DBUtils.java │ │ │ ├── DB_CONNECT.java │ │ │ ├── DevJar.java │ │ │ ├── JarEncode.java │ │ │ ├── Message.java │ │ │ ├── ReflectSet.java │ │ │ ├── SimpleLine.java │ │ │ ├── api │ │ │ ├── ClassCoder.java │ │ │ ├── Connector.java │ │ │ ├── DevTool.java │ │ │ └── Pool.java │ │ │ ├── command │ │ │ ├── Cckey.java │ │ │ ├── CommandBase.java │ │ │ ├── Compare.java │ │ │ ├── Connect.java │ │ │ ├── ConnectorCommand.java │ │ │ ├── Ctable.java │ │ │ ├── Dcclass.java │ │ │ ├── Dclass.java │ │ │ ├── DeclassAll.java │ │ │ ├── Dev.java │ │ │ ├── Ead.java │ │ │ ├── Echo.java │ │ │ ├── Eclass.java │ │ │ ├── EnclassAll.java │ │ │ ├── Exit.java │ │ │ ├── Gclass.java │ │ │ ├── Help.kt │ │ │ ├── Jar.java │ │ │ ├── Jarin.java │ │ │ ├── KeyFile.java │ │ │ ├── Random.java │ │ │ ├── Reboot.java │ │ │ ├── SPwd.java │ │ │ ├── SUser.java │ │ │ ├── Save.java │ │ │ ├── Sppf.java │ │ │ ├── Stable.java │ │ │ └── newcommand │ │ │ │ ├── Ackey.java │ │ │ │ ├── Adc.java │ │ │ │ ├── Adeclassall.java │ │ │ │ ├── Aec.java │ │ │ │ ├── Aenclassall.java │ │ │ │ ├── Akeyec.java │ │ │ │ └── SafeCommand.java │ │ │ ├── encode │ │ │ ├── DecryptStart.java │ │ │ ├── EncryptClasses.java │ │ │ ├── GenerateKey.java │ │ │ ├── SimpleClassCoder.java │ │ │ └── Util.java │ │ │ ├── ex │ │ │ ├── DBConnectException.java │ │ │ ├── ParseException.java │ │ │ └── ShutDownException.java │ │ │ ├── jar │ │ │ └── DecodeJar.java │ │ │ ├── safe │ │ │ └── encode │ │ │ │ ├── AESGenerateKey.java │ │ │ │ ├── CodeUtils.java │ │ │ │ ├── SafeClassCoder.java │ │ │ │ ├── SafeClassLoader.java │ │ │ │ └── SafeEncryptClasses.java │ │ │ └── stream │ │ │ ├── ClassStream.java │ │ │ └── CompareTo.java │ └── resources │ │ ├── META-INF │ │ └── MANIFEST.MF │ │ └── application.properties └── test │ └── java │ └── net │ └── noyark │ └── foundbug │ └── test │ ├── TestEncode.java │ └── TestYaml.java ├── test.jar └── testKey.key /.idea/PluginEmpowerSystem.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.idea/artifacts/Empower.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | $PROJECT_DIR$/../../PluginEmpowerSystem/out/artifacts/Empower 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | Android 21 | 22 | 23 | CorrectnessLintAndroid 24 | 25 | 26 | GSPGrailsGroovy 27 | 28 | 29 | General 30 | 31 | 32 | GrailsGroovy 33 | 34 | 35 | Groovy 36 | 37 | 38 | Kotlin 39 | 40 | 41 | LintAndroid 42 | 43 | 44 | Maven 45 | 46 | 47 | PerformanceLintAndroid 48 | 49 | 50 | Plugin DevKit 51 | 52 | 53 | XPath 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Empower.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLu550/edclass4j/6fde648becda5b00f7e07aff3e92e93bfbd2780f/Empower.jar -------------------------------------------------------------------------------- /PluginEmpowerSystem.iml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # edclass4j 2 | 这是一款加密jar包和远程端授权密匙的控制程序 3 | 4 | ###### 启动程序 5 | `java -jar Empower.jar` 6 | 7 | ###### 启动加密jar包 8 | `java -jar Empower.jar jar filename key_file` 9 | 10 | * filename默认相对路径为何Empower.jar同级 11 | 12 | * key_file如果要指向keyfile文件夹,则需要变量指向 13 | 14 | ###### 里面的指令 15 | 16 | 输入`help`可以查询命令 17 | 18 | ###### 建议 19 | * 启动加密程序时,不建议在程序内部输入指令 20 | ###### 默认的文件系统 21 | * keyfile文件夹 22 | * classes文件夹 23 | * application.properties 24 | ```properties 25 | 26 | 27 | #now:默认为当前jar包所在位置,这个为classpath 28 | # ,即class文件根目录 29 | #classpath=now:classes 30 | #指加密文件加密输出位置 31 | #this为默认覆盖原来的文件 32 | #encode.to=this 33 | #在解密时是否输出信息 34 | #建议关闭,否则会影响您的程序输出效果 35 | #decode.message=false 36 | 37 | ``` 38 | ###### 其他小特性 39 | * 内置变量 40 | * ${jarin} 本jar包所在的文件夹路径 41 | * ${keyfile} 生成key文件的文件夹 42 | * ${gclass} 打包的根目录 43 | * 声明变量 44 | * $name=value 45 | * 调用 46 | * ${name} 47 | 48 | 导入api 49 | ```xml 50 | 51 | 52 | net.noyark 53 | empower-classcoder 54 | 0.0.2 55 | 56 | 57 | 58 | 59 | nexus 60 | Team Neux Repositoryhttp://www.noyark.net:8081/nexus/content/groups/public/ 61 | 62 | 63 | 64 | 65 | nexus 66 | Team Neux Repository 67 | http://www.noyark.net:8081/nexus/content/groups/public/ 68 | 69 | 70 | 71 | 72 | ``` 73 | api都在api包里 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /Test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLu550/edclass4j/6fde648becda5b00f7e07aff3e92e93bfbd2780f/Test -------------------------------------------------------------------------------- /config.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLu550/edclass4j/6fde648becda5b00f7e07aff3e92e93bfbd2780f/config.txt -------------------------------------------------------------------------------- /empower-with-config.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLu550/edclass4j/6fde648becda5b00f7e07aff3e92e93bfbd2780f/empower-with-config.zip -------------------------------------------------------------------------------- /hello.key: -------------------------------------------------------------------------------- 1 | 1559021584535115 2 | -------------------------------------------------------------------------------- /key.yml: -------------------------------------------------------------------------------- 1 | key: helloworld -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | net.noyark 8 | PluginEmpowerSystem 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 1.3.31 13 | 14 | 15 | 16 | 17 | 18 | org.apache.commons 19 | commons-dbcp2 20 | 2.6.0 21 | 22 | 23 | 24 | 25 | mysql 26 | 27 | mysql-connector-java 28 | 29 | 5.1.25 30 | 31 | 32 | 33 | 34 | 35 | com.oracle 36 | ojdbc14 37 | 10.2.0.4.0 38 | 39 | 40 | 41 | 42 | com.microsoft.sqlserver 43 | mssql-jdbc 44 | 7.3.0.jre11-preview 45 | test 46 | 47 | 48 | 49 | 50 | org.yaml 51 | snakeyaml 52 | 1.24 53 | 54 | 55 | 56 | 57 | junit 58 | junit 59 | 4.12 60 | test 61 | 62 | 63 | 64 | commons-io 65 | commons-io 66 | 2.6 67 | 68 | 69 | 70 | org.jetbrains.kotlin 71 | kotlin-stdlib-jdk8 72 | ${kotlin.version} 73 | 74 | 75 | org.jetbrains.kotlin 76 | kotlin-test 77 | ${kotlin.version} 78 | test 79 | 80 | 81 | jline 82 | jline 83 | 2.9 84 | 85 | 86 | org.apache.commons 87 | commons-lang3 88 | 3.4 89 | 90 | 91 | org.apache.directory.studio 92 | org.apache.commons.codec 93 | 1.8 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | org.jetbrains.kotlin 102 | kotlin-maven-plugin 103 | ${kotlin.version} 104 | 105 | 106 | compile 107 | compile 108 | 109 | compile 110 | 111 | 112 | 113 | test-compile 114 | test-compile 115 | 116 | test-compile 117 | 118 | 119 | 120 | 121 | 1.8 122 | 123 | 124 | 125 | org.apache.maven.plugins 126 | maven-compiler-plugin 127 | 128 | 129 | compile 130 | compile 131 | 132 | compile 133 | 134 | 135 | 136 | testCompile 137 | test-compile 138 | 139 | testCompile 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/CodeReflectSet.java: -------------------------------------------------------------------------------- 1 | /*Culesberry technolegy Co. Ltd. (c) 2019-2020 2 | * 3 | * Stating that the software,the software belongs Gulesberry 4 | * noyark open source group,noyark has all the power to interpret 5 | * and copyright information for the software prohibit organizations 6 | * and individuals conduct their business practices and illegal practices, 7 | * projects of: magiclu,Chinese name *Changcun Lu*.The software has nothing 8 | * to do with current politics,free software is the purpose of noyark 9 | * 10 | * noyark-system info: 11 | * **************************************************** 12 | * www.noyark.net 13 | * **************************************************** 14 | * 15 | */ 16 | package net.noyark.www.utils; 17 | 18 | 19 | import net.noyark.www.utils.api.ClassCoder; 20 | 21 | import java.io.IOException; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | 26 | /** 27 | * This class is used to extensively load all Config 28 | * annotated classes and create corresponding configuration 29 | * file objects,then load the Root or Node annotations,load 30 | * them into hierarchy and key-value pairs,and various data types 31 | * to achieve the recommended configuration. 32 | *
How to load using this class 33 | *
if the config class in the a.b.c.d package,the packageFile 34 | * can be a or a.b or a.b.c or a.b.c.d 35 | *
 new CodeReflectSet("the parent package",this).loadAnnotation(); 36 | * @author magiclu550 37 | * @since JDK1.8 38 | * 39 | */ 40 | 41 | public class CodeReflectSet implements ReflectSet { 42 | 43 | private static CodeReflectSet DESReflectSet; 44 | 45 | static { 46 | DESReflectSet = new CodeReflectSet(); 47 | } 48 | 49 | /** 50 | * Used to load all the classes with Config annotation 51 | * under the specified package ,and parse it into a 52 | * configuration file,and generate a configuration file object 53 | */ 54 | 55 | public List> load(ClassCoder classCoder, String keyFile, boolean isDecode, String mainClass) { 56 | try { 57 | return scanPackage(classCoder,keyFile,isDecode,mainClass); 58 | } catch (IllegalArgumentException | IllegalAccessException | ClassNotFoundException | InstantiationException 59 | | IOException e) { 60 | e.printStackTrace(); 61 | } 62 | return null; 63 | } 64 | 65 | /** 66 | * Its role is to further parse 67 | * the obtained class and resolve it to 68 | * an instance; 69 | * @throws IllegalArgumentException 70 | * @throws IllegalAccessException 71 | * @throws IOException 72 | * @throws ClassNotFoundException 73 | * @throws InstantiationException 74 | */ 75 | private List> scanPackage(ClassCoder classCoder, String keyFile, boolean decode, String mainClass) throws IllegalArgumentException, IllegalAccessException, IOException, ClassNotFoundException, InstantiationException { 76 | String[] clzs = getAllClassName(); 77 | List> classes = new ArrayList<>(); 78 | if(!decode){ 79 | classCoder.encode(keyFile,clzs); 80 | }else{ 81 | for(String cname:clzs){ 82 | Class clazz; 83 | if(cname.replaceAll("/|\\\\",".").equals(mainClass)){ 84 | clazz = classCoder.decode(keyFile,cname,true); 85 | }else{ 86 | clazz = classCoder.decode(keyFile,cname); 87 | } 88 | classes.add(clazz); 89 | } 90 | } 91 | return classes; 92 | } 93 | 94 | public static CodeReflectSet getDESReflectSet() { 95 | return DESReflectSet; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/Coder.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | public interface Coder { 4 | 5 | enum DecryptMethod{ 6 | AES("AES"), 7 | DES("DES"); 8 | 9 | private String method; 10 | DecryptMethod(String method){ 11 | this.method = method; 12 | } 13 | 14 | public String getMethod() { 15 | return method; 16 | } 17 | } 18 | 19 | void encode(String fileName,String... classname); 20 | 21 | Class decode(String keyFile,String classname,boolean executeMain); 22 | //不执行main 23 | Class decode(String keyFile,String classname); 24 | 25 | Class getClassInJar(String jarFile,String classname,String keyFile); 26 | 27 | Class getClassInJar(String jarFile,String classname,String keyFile,ClassLoader loader); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/DBTypes.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | public enum DBTypes { 4 | 5 | MYSQL("com.mysql.jdbc.Driver","mysql",3306), 6 | ORACLE("oracle.jdbc.driver.OracleDriver","oracle",1521), 7 | SQLSERVER("com.microsoft.sqlserver.jdbc.SQLServerDriver","sqlserver",1433); 8 | 9 | private String driver; 10 | 11 | private String start; 12 | 13 | private int port; 14 | DBTypes(String driver,String start,int port){ 15 | this.driver = driver; 16 | this.start = start; 17 | this.port = port; 18 | } 19 | 20 | public String getDriver(){ 21 | return driver; 22 | } 23 | 24 | public String getStart() { 25 | return start; 26 | } 27 | 28 | public int getPort() { 29 | return port; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/DBUtils.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | import org.apache.commons.dbcp2.BasicDataSource; 4 | 5 | import java.sql.Connection; 6 | import java.sql.SQLException; 7 | 8 | public class DBUtils { 9 | 10 | BasicDataSource dataSource; 11 | 12 | public DBUtils(DBTypes types,String name,String password,String dbName,String ip,int port){ 13 | dataSource = new BasicDataSource(); 14 | 15 | dataSource.setDriverClassName(types.getDriver()); 16 | dataSource.setUsername(name); 17 | dataSource.setPassword(password); 18 | if(types.equals(DBTypes.SQLSERVER)){ 19 | //1433 20 | dataSource.setUrl("jdbc:"+types.getStart()+"://"+ip+":"+port+";DatabaseName="+dbName); 21 | }else if(types.equals(DBTypes.ORACLE)){ 22 | //1521 23 | dataSource.setUrl("jdbc:"+types.getStart()+":thin:@"+ip+":"+port+":"+dbName); 24 | }else if(types.equals(DBTypes.MYSQL)){ 25 | //3306 26 | dataSource.setUrl("jdbc:"+types.getStart()+"://"+ip+":"+port+"/"+dbName); 27 | } 28 | } 29 | 30 | public Connection getConnection() throws SQLException { 31 | return dataSource.getConnection(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/DB_CONNECT.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | import net.noyark.www.utils.ex.DBConnectException; 5 | import net.noyark.www.utils.ex.ParseException; 6 | import org.yaml.snakeyaml.Yaml; 7 | 8 | import java.io.FileInputStream; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.sql.Connection; 12 | import java.sql.PreparedStatement; 13 | import java.sql.ResultSet; 14 | import java.sql.SQLException; 15 | import java.util.Map; 16 | import java.util.UUID; 17 | 18 | /** 19 | * 这个类用于插件调用,连接远端的key服务器 20 | * 你必须设置userName和password 21 | * 22 | * 设置流程 23 | * 设置userName和password 24 | * connect 25 | * setTable 26 | * compareKey 返回值 27 | * 之后进行处理授权成功和失败的逻辑 28 | * 29 | * @author magiclu550 30 | */ 31 | 32 | public class DB_CONNECT implements Connector { 33 | 34 | 35 | 36 | 37 | 38 | private String userName; 39 | 40 | private String password; 41 | 42 | private Connection connection; 43 | 44 | private Yaml yaml; 45 | 46 | private String table; 47 | 48 | private DBTypes type; 49 | 50 | private String ip; 51 | 52 | private String dbName; 53 | 54 | public DB_CONNECT(){ 55 | yaml = new Yaml(); 56 | } 57 | 58 | /** 59 | * 不定端口 60 | * @param ip 61 | * @param dbName 62 | * @param port 63 | * @param types 64 | */ 65 | 66 | public void connect(String ip,String dbName,int port,DBTypes types){ 67 | DBUtils utils = new DBUtils(types,userName,password,dbName,ip,port); 68 | this.type = types; 69 | this.ip = ip; 70 | this.dbName = dbName; 71 | try{ 72 | connection = utils.getConnection(); 73 | }catch (Exception e){ 74 | throw new DBConnectException("connect error",e); 75 | } 76 | } 77 | 78 | /** 79 | * 默认连接mysql 80 | * @param ip 81 | * @param dbName 82 | */ 83 | 84 | public void connect(String ip,String dbName){ 85 | connect(ip,dbName,3306,DBTypes.MYSQL); 86 | } 87 | 88 | /** 89 | * 使用默认端口 90 | * @param ip 91 | * @param dbName 92 | * @param dbTypes 93 | */ 94 | 95 | public void connect(String ip,String dbName,DBTypes dbTypes){ 96 | connect(ip,dbName,dbTypes.getPort(),dbTypes); 97 | } 98 | 99 | public void setUserName(String userName) { 100 | this.userName = userName; 101 | } 102 | 103 | public void setPassword(String password) { 104 | this.password = password; 105 | } 106 | 107 | /** 108 | * 设置当前的表 109 | * @param table 110 | */ 111 | 112 | public void setTable(String table){ 113 | this.table = table; 114 | } 115 | 116 | /** 117 | * 创建表 118 | */ 119 | public boolean createKeyTable(String table){ 120 | try{ 121 | return connection.createStatement().execute("CREATE TABLE "+table+" (" + 122 | "id INT PRIMARY KEY AUTO_INCREMENT," + 123 | "keyName CHAR(200) UNIQUE," + 124 | "ip VARCHAR (50)," + 125 | "port CHAR " + 126 | ")"); 127 | }catch (SQLException e){ 128 | e.printStackTrace(); 129 | return false; 130 | } 131 | } 132 | 133 | 134 | 135 | /** 136 | * 制定配置文件和配置字段,查找序列号是否符合要求,前提是设置了制定ip地址 137 | * 表的结构要求是 138 | * id key ip port 139 | * primary unique key 140 | * int text varchar(50) varchar(10) 141 | * serverIp是指当前授权的ip地址 142 | * serverPort是指当前的授权port 143 | * 144 | * 返回是否已经授权 145 | */ 146 | public boolean compareKey(String keyFile,String keyName,String serverIp,int serverPort){ 147 | try{ 148 | return compareKey(new FileInputStream(keyFile),keyName,serverIp,serverPort); 149 | }catch (IOException e){ 150 | throw new ParseException("the yaml config is wrong",e); 151 | } 152 | } 153 | 154 | public boolean compareKey(InputStream in,String keyName,String serverIp,int serverPort){ 155 | try{ 156 | Map keyMapping = yaml.load(in); 157 | String key = keyMapping.get(keyName).toString(); 158 | return compareKey(key,serverIp,serverPort); 159 | }catch (Exception e){ 160 | throw new ParseException("the connection is wrong",e); 161 | } 162 | } 163 | 164 | /** 165 | * 前提连接了数据库 166 | * 生成随机序列码,插入数据库 167 | */ 168 | public void randomKeys(int count){ 169 | for(int i = 0;i vars = new HashMap<>(); 36 | 37 | private static Connector connector; 38 | 39 | public static final Map commandBaseMap; 40 | 41 | 42 | static { 43 | commandBaseMap = new HashMap<>(); 44 | connector = Pool.getConnector(); 45 | registerCommand(); 46 | vars.put("jarin",commandBaseMap.get("jarin").execute(new String[1]).toString()); 47 | vars.put("keyfile",commandBaseMap.get("keyfile").execute(new String[1]).toString()); 48 | vars.put("gclass",commandBaseMap.get("gclass").execute(new String[1]).toString()); 49 | } 50 | 51 | public static void main(String[] args){ 52 | if(args.length != 0){ 53 | runCommand(args); 54 | }else{ 55 | Message.info("启动PluginEmpowerSystem服务"); 56 | new Thread(new CommandThread()).start(); 57 | } 58 | } 59 | 60 | public static void registerCommand(){ 61 | commandBaseMap.put("suser",new SUser(connector)); 62 | commandBaseMap.put("spwd",new SPwd(connector)); 63 | commandBaseMap.put("exit",new Exit()); 64 | commandBaseMap.put("connect",new Connect(connector)); 65 | commandBaseMap.put("save",new Save(connector)); 66 | commandBaseMap.put("rkeys",new Random(connector)); 67 | commandBaseMap.put("stable",new Stable(connector)); 68 | commandBaseMap.put("ctable",new Ctable(connector));//创建表 69 | commandBaseMap.put("compare",new Compare(connector)); 70 | commandBaseMap.put("dcclass",new Dcclass(connector)); 71 | commandBaseMap.put("cckey",new Cckey());//cckey keyFileName 72 | commandBaseMap.put("eclass",new Eclass());//eclass keyFileName class1 class2 class3... 73 | commandBaseMap.put("dclass",new Dclass()); 74 | commandBaseMap.put("declassall",new DeclassAll()); 75 | commandBaseMap.put("enclassall",new EnclassAll()); 76 | commandBaseMap.put("sppf",new Sppf()); 77 | commandBaseMap.put("keyfile",new KeyFile()); 78 | commandBaseMap.put("jarin",new Jarin()); 79 | commandBaseMap.put("echo",new Echo()); 80 | commandBaseMap.put("dev",new Dev());//jar 81 | commandBaseMap.put("gclass",new Gclass());//get classes 82 | commandBaseMap.put("jar",new Jar()); 83 | commandBaseMap.put("ead",new Ead()); 84 | commandBaseMap.put("ackey",new Ackey()); 85 | commandBaseMap.put("adc",new Adc()); 86 | commandBaseMap.put("adeclassall",new Adeclassall()); 87 | commandBaseMap.put("aec",new Aec()); 88 | commandBaseMap.put("aenclassall",new Aenclassall()); 89 | commandBaseMap.put("akeyec",new Akeyec()); 90 | commandBaseMap.put("help",new Help(new HashMap<>(commandBaseMap))); 91 | } 92 | 93 | 94 | public static class CommandThread implements Runnable{ 95 | @Override 96 | public void run() { 97 | while(true){ 98 | try{ 99 | String cmd = Message.cmd(); 100 | String[] args = cmd.split(" "); 101 | runCommand(args); 102 | }catch (Exception e){ 103 | if (e instanceof ShutDownException){ 104 | Message.info("close"); 105 | System.exit(0); 106 | } 107 | e.printStackTrace(); 108 | Message.error(e.getMessage()); 109 | } 110 | } 111 | } 112 | } 113 | 114 | public static void runCommand(String[] args){ 115 | if(args[0].startsWith("$")){ 116 | String[] right_left = args[0].split("="); 117 | String value = right_left[1]; 118 | Set> set = commandBaseMap.entrySet(); 119 | //变量指令只支持jarin keyfile的 120 | value = value 121 | .replace("{jarin}",commandBaseMap.get("jarin").execute(new String[]{}).toString()) 122 | .replace("{keyfile}",commandBaseMap.get("keyfile").execute(new String[]{}).toString()) 123 | .replace("{gclass}",commandBaseMap.get("gclass").execute(new String[]{}).toString()); 124 | 125 | vars.put(right_left[0].replace("$",""),value); 126 | }else{ 127 | String[] alls = new String[args.length-1]; 128 | System.arraycopy(args,1,alls,0,alls.length); 129 | Set> set = vars.entrySet(); 130 | for(int i =0;i entry:set) { 132 | alls[i] = alls[i].replace("${"+entry.getKey()+"}",entry.getValue()); 133 | } 134 | } 135 | CommandBase commandInstance = commandBaseMap.get(args[0]); 136 | if(commandInstance != null){ 137 | Object o = commandInstance.execute(alls); 138 | Message.info(o==null?"null":o.toString()); 139 | }else { 140 | Message.error("no such command"); 141 | } 142 | } 143 | } 144 | 145 | } 146 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/Message.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | 4 | import jline.console.ConsoleReader; 5 | import java.io.IOException; 6 | 7 | 8 | public class Message { 9 | private static ConsoleReader lineReader; 10 | 11 | 12 | static { 13 | try{ 14 | lineReader = new ConsoleReader(); 15 | lineReader.addCompleter(new SimpleLine()); 16 | }catch (IOException e){ 17 | 18 | } 19 | } 20 | 21 | 22 | public static void info(String msg){ 23 | 24 | System.out.println("[INFO ]"+msg); 25 | } 26 | 27 | public static void error(String msg){ 28 | System.out.println("[ERROR ]"+msg); 29 | } 30 | 31 | public static String cmd(){ 32 | try{ 33 | return lineReader.readLine(">"); 34 | }catch (IOException e){ 35 | throw new RuntimeException("输入异常",e); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/ReflectSet.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | import net.noyark.www.utils.api.ClassCoder; 4 | import net.noyark.www.utils.encode.SimpleClassCoder; 5 | import net.noyark.www.utils.encode.Util; 6 | 7 | import java.io.File; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | public interface ReflectSet { 12 | 13 | List> load(ClassCoder classCoder, String keyFile, boolean isDecode, String mainClass); 14 | 15 | default String[] getAllClassName(){ 16 | List list = new ArrayList<>(); 17 | File file = new File(Util.getClassPath("")); 18 | List fileName = loadClass(file); 19 | for(File fn:fileName) { 20 | File[] fs = fn.listFiles(); 21 | if(fs!=null) { 22 | for(File f1:fs) { 23 | if(f1.getName().endsWith(".class")) { 24 | String classpath = f1.getPath(); 25 | classpath = classpath.substring(classpath.indexOf("classes")+"classes".length()+1,classpath.indexOf(".class")).replaceAll("/","."); 26 | list.add(classpath); 27 | } 28 | } 29 | } 30 | } 31 | return list.toArray(new String[list.size()]); 32 | } 33 | 34 | /** 35 | * It can get Package and Classes next the root package 36 | * @param file the classpath file 37 | * @return the file about classes and package 38 | */ 39 | 40 | private List loadClass(File file) { 41 | List allFiles = new ArrayList(); 42 | File[] files = file.listFiles(); 43 | if(files!=null) { 44 | for(File f:files) { 45 | if(f.getName().endsWith(".class")) { 46 | allFiles.add(f); 47 | } 48 | } 49 | } 50 | searchFile(files, allFiles); 51 | return allFiles; 52 | } 53 | 54 | /** 55 | * It can get all of the file about package 56 | * @param files the file objects 57 | * @param allFiles the package file 58 | */ 59 | 60 | private void searchFile(File[] files,List allFiles) { 61 | if(files!=null) { 62 | for(File f:files) { 63 | if(f.isDirectory()){ 64 | allFiles.add(f); 65 | File[] files2 = f.listFiles(); 66 | searchFile(files2, allFiles); 67 | } 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/SimpleLine.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils; 2 | 3 | import jline.console.completer.Completer; 4 | 5 | import java.util.List; 6 | import java.util.Set; 7 | import java.util.stream.Collectors; 8 | 9 | public class SimpleLine implements Completer { 10 | 11 | public int complete(String buffer, int index, List list) { 12 | Set entries = 13 | JarEncode.commandBaseMap 14 | .keySet() 15 | .stream() 16 | .filter(e->e.startsWith(buffer)).collect(Collectors.toSet()); 17 | list.addAll(entries); 18 | return index-buffer.length(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/api/ClassCoder.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.api; 2 | 3 | 4 | import net.noyark.www.utils.CodeReflectSet; 5 | import net.noyark.www.utils.Coder; 6 | import net.noyark.www.utils.encode.Util; 7 | 8 | import java.io.File; 9 | import java.util.List; 10 | public abstract class ClassCoder implements Coder { 11 | 12 | /** 设置读取classpath的路径,即包前面的根路径,如果不设置,默认为在项目资源文件 */ 13 | 14 | public void setApplicationFile(String file){ 15 | Util.setReadApplication(file); 16 | } 17 | /** 获取class文件输出路径,如果为this,则是覆盖现行的路径 */ 18 | public String getClassOut(){ 19 | return Util.getClassOut(); 20 | } 21 | /** 获取application classpath,以你定义的application.properties内容为准*/ 22 | public String getClassPath(String name){ 23 | return Util.getClassPath(name); 24 | } 25 | /** 获取keyfile文件夹所在的绝对路径 */ 26 | public File getKeyFile(){ 27 | return Util.getKeyFile(); 28 | } 29 | /** 获取jar包所在路径 */ 30 | public File jarInFile(){ 31 | return Util.getJarInFIle(); 32 | } 33 | 34 | //加密整个class系统 35 | public List> recursiveEncode(String mainClass,String keyFile){ 36 | return CodeReflectSet.getDESReflectSet().load(this,keyFile,false,mainClass); 37 | } 38 | //加密整个class系统 39 | public List> recursiveDecode(String mainClass,String keyFile){ 40 | return CodeReflectSet.getDESReflectSet().load(this,keyFile,true,mainClass); 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/api/Connector.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.api; 2 | 3 | import net.noyark.www.utils.DBTypes; 4 | 5 | import java.io.InputStream; 6 | import java.sql.SQLException; 7 | 8 | public interface Connector { 9 | 10 | /** 11 | * 不定端口 12 | * @param ip 13 | * @param dbName 14 | * @param port 15 | * @param types 16 | */ 17 | 18 | void connect(String ip, String dbName, int port, DBTypes types); 19 | 20 | /** 21 | * 默认连接mysql 22 | * @param ip 23 | * @param dbName 24 | */ 25 | 26 | void connect(String ip,String dbName); 27 | 28 | /** 29 | * 使用默认端口 30 | * @param ip 31 | * @param dbName 32 | * @param dbTypes 33 | */ 34 | 35 | void connect(String ip,String dbName,DBTypes dbTypes); 36 | 37 | /** 38 | * 设置用户名 39 | * @param userName 40 | */ 41 | 42 | void setUserName(String userName); 43 | 44 | /** 45 | * 设置密码 46 | * @param password 47 | */ 48 | 49 | void setPassword(String password); 50 | 51 | 52 | /** 53 | * 设置当前的表 54 | * @param table 55 | */ 56 | 57 | void setTable(String table); 58 | 59 | /** 60 | * 创建表 61 | */ 62 | boolean createKeyTable(String table); 63 | 64 | 65 | /** 66 | * 制定配置文件和配置字段,查找序列号是否符合要求,前提是设置了制定ip地址 67 | * 表的结构要求是 68 | * id key ip port 69 | * primary unique key 70 | * int text varchar(50) varchar(10) 71 | * serverIp是指当前授权的ip地址 72 | * serverPort是指当前的授权port 73 | * 74 | * 返回是否已经授权 75 | */ 76 | boolean compareKey(String keyFile,String keyName,String serverIp,int serverPort); 77 | 78 | boolean compareKey(InputStream in, String keyName, String serverIp, int serverPort); 79 | 80 | boolean compareKey(String key,String serverIp,int serverPort) throws SQLException; 81 | /** 82 | * 关闭db服务 83 | */ 84 | void close(); 85 | 86 | /** 87 | * 前提连接了数据库 88 | * 生成随机序列码,插入数据库 89 | */ 90 | void randomKeys(int count); 91 | 92 | /** 93 | * 生成单个随机序列码 94 | */ 95 | void randomKey(); 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/api/DevTool.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.api; 2 | 3 | import java.io.IOException; 4 | 5 | public interface DevTool { 6 | /** 7 | * 压缩jar 8 | * @param javaClassPath 9 | * @param targetPath 10 | * @param mainClass 11 | * @throws IOException 12 | */ 13 | void devJar(String javaClassPath,String targetPath,String mainClass) throws IOException; 14 | 15 | /** 16 | * 运行命令 17 | * @param command 18 | */ 19 | void runCommand(String command); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/api/Pool.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.api; 2 | 3 | import net.noyark.www.utils.DB_CONNECT; 4 | import net.noyark.www.utils.DevJar; 5 | import net.noyark.www.utils.encode.SimpleClassCoder; 6 | import net.noyark.www.utils.safe.encode.SafeClassCoder; 7 | 8 | public class Pool { 9 | 10 | static { 11 | connector = new DB_CONNECT(); 12 | classCoder = new SimpleClassCoder(); 13 | devJar = new DevJar(); 14 | safeSimpleCoder = new SafeClassCoder(); 15 | } 16 | 17 | private static Connector connector; 18 | 19 | private static SimpleClassCoder classCoder; 20 | 21 | private static SafeClassCoder safeSimpleCoder; 22 | 23 | private static DevJar devJar; 24 | 25 | public static Connector getConnector() { 26 | return connector; 27 | } 28 | 29 | @Deprecated 30 | public static ClassCoder getClassCoder() { 31 | return classCoder; 32 | } 33 | 34 | public static ClassCoder getDESClassCoder(){ 35 | return classCoder; 36 | } 37 | 38 | public static ClassCoder getAESClassCoder(){ 39 | return safeSimpleCoder; 40 | } 41 | 42 | public static DevTool getDevJar() { 43 | return devJar; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Cckey.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.GenerateKey; 4 | 5 | public class Cckey implements CommandBase{ 6 | 7 | @Override 8 | public Object execute(String[] args) { 9 | try{ 10 | GenerateKey.createKey(args); 11 | }catch (Exception e){ 12 | e.printStackTrace(); 13 | return "生成密钥文件过程中发生未知错误"; 14 | } 15 | 16 | return "生成秘钥文件"; 17 | } 18 | 19 | @Override 20 | public String[] usage() { 21 | return new String[]{"用于生成秘钥文件","key_File_Name key文件生成路径,相对于keyfile路径"}; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/CommandBase.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | public interface CommandBase { 4 | 5 | /** 6 | * 执行 7 | * @return 8 | */ 9 | 10 | Object execute(String[] args); 11 | 12 | /** 13 | * usage 14 | */ 15 | String[] usage(); 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Compare.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | import java.sql.SQLException; 6 | 7 | public class Compare extends ConnectorCommand{ 8 | 9 | public Compare(Connector connector) { 10 | super(connector); 11 | } 12 | 13 | @Override 14 | public Object execute(String[] args) { 15 | try{ 16 | return connector.compareKey(args[0],args[1],Integer.parseInt(args[2])); 17 | }catch (SQLException e){ 18 | e.printStackTrace(); 19 | return false; 20 | } 21 | } 22 | 23 | @Override 24 | public String[] usage() { 25 | return new String[]{"用于从远程数据库找到这个key是否存在","ip 绑定服务器ip地址 port 绑定服务器端口"}; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Connect.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | import net.noyark.www.utils.DBTypes; 5 | import org.apache.commons.io.FileUtils; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.util.List; 10 | 11 | /** 12 | * 该指令连接数据库 13 | */ 14 | 15 | public class Connect extends ConnectorCommand{ 16 | 17 | public Connect(Connector connector) { 18 | super(connector); 19 | } 20 | 21 | private static final int ARR_IP = 0; 22 | 23 | private static final int ARR_DB_NAME = 1; 24 | 25 | private static final int ARR_PORT = 2; 26 | 27 | private static final int ARR_START_TYPE = 3; 28 | 29 | @Override 30 | public Object execute(String[] args) { 31 | if (args.length == 2){ 32 | //just ip dbName 33 | if(args[0].equals("-f")){ 34 | try{ 35 | List infor = FileUtils.readLines(new File(args[1]),"UTF-8"); 36 | connector.setUserName(infor.get(Save.USERNAME)); 37 | connector.setPassword(infor.get(Save.PASSWORD)); 38 | //ip db start port 39 | connect(infor.get(Save.IP),infor.get(Save.DB_NAME),infor.get(Save.PORT),infor.get(Save.START_TYPE)); 40 | connector.setTable(infor.get(Save.TABLE)); 41 | }catch (IOException e){ 42 | e.printStackTrace(); 43 | } 44 | }else{ 45 | connector.connect(args[0],args[1]); 46 | } 47 | }else if(args.length == 3){ 48 | if(args[2].toUpperCase().equals("oracle")){ 49 | connector.connect(args[0],args[1], DBTypes.ORACLE); 50 | }else if(args[2].toUpperCase().equals("sqlserver")){ 51 | connector.connect(args[0],args[1],DBTypes.SQLSERVER); 52 | }else{ 53 | connector.connect(args[0],args[1],DBTypes.MYSQL); 54 | } 55 | }else{ 56 | connect(args); 57 | } 58 | return "connect successfully"; 59 | } 60 | 61 | 62 | @Override 63 | public String[] usage() { 64 | return new String[]{"用于连接远程授权端","-f filename 直接导入标准的配置文件","ip dbname 默认为mysql数据库","ip dbname type 选择数据库类型,有mysql oracle sqlserver"}; 65 | } 66 | 67 | private void connect(String... args){ 68 | if(args[ARR_START_TYPE].toUpperCase().equals("oracle")){ 69 | connector.connect(args[ARR_IP],args[ARR_DB_NAME],Integer.parseInt(args[ARR_PORT]),DBTypes.ORACLE); 70 | }else if(args[ARR_START_TYPE].toUpperCase().equals("sqlserver")){ 71 | connector.connect(args[ARR_IP],args[ARR_DB_NAME],Integer.parseInt(args[ARR_PORT]),DBTypes.SQLSERVER); 72 | }else{ 73 | connector.connect(args[ARR_IP],args[ARR_DB_NAME],Integer.parseInt(args[ARR_PORT]),DBTypes.MYSQL); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/ConnectorCommand.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | public abstract class ConnectorCommand implements CommandBase { 6 | 7 | protected Connector connector; 8 | 9 | public ConnectorCommand(Connector connector){ 10 | this.connector = connector; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Ctable.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | public class Ctable extends ConnectorCommand { 6 | 7 | public Ctable(Connector connector) { 8 | super(connector); 9 | } 10 | 11 | @Override 12 | public Object execute(String[] args) { 13 | connector.createKeyTable(args[0]); 14 | return "创建成功"; 15 | } 16 | 17 | @Override 18 | public String[] usage() { 19 | return new String[]{"用于创建标准的授权存储表","table_name table名称"}; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Dcclass.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | 4 | import net.noyark.www.utils.api.Connector; 5 | import net.noyark.www.utils.encode.DecryptStart; 6 | 7 | 8 | 9 | /** 10 | * 将代码解码并判断有没有远程端的key 11 | * dcclass ip port key classkeyfilename classname... 12 | * dcclass ip port -f filename keyname classkeyfilename classname... 13 | */ 14 | 15 | public class Dcclass extends ConnectorCommand { 16 | 17 | public static final int BEFORE_LENGTH_F = 5; 18 | 19 | public static final int BEFORE_LENGTH = 3; 20 | 21 | public static final int IP_INDEX_F = 0; 22 | 23 | public static final int PORT_INDEX_F = 1; 24 | 25 | public static final int KEY_FILE_INDEX_F = 3; 26 | 27 | public static final int KEY_NAME_INDEX_F = 4; 28 | 29 | public static final int KEY_INDEX = 2; 30 | 31 | public static final int IP_INDEX = 0; 32 | 33 | public static final int PORT_INDEX = 1; 34 | 35 | public Dcclass(Connector connector) { 36 | super(connector); 37 | } 38 | 39 | @Override 40 | public Object execute(String[] args) { 41 | try{ 42 | if("-f".equals(args[2])){ 43 | //keyname classname; 44 | String[] deargs = new String[args.length-BEFORE_LENGTH_F]; 45 | System.arraycopy(args,BEFORE_LENGTH_F,deargs,0,deargs.length); 46 | DecryptStart.decode(deargs,true); 47 | return connector.compareKey(args[KEY_FILE_INDEX_F],args[KEY_NAME_INDEX_F],args[IP_INDEX_F],Integer.parseInt(args[PORT_INDEX_F])); 48 | }else{ 49 | String[] deargs = new String[args.length-BEFORE_LENGTH]; 50 | System.arraycopy(args,BEFORE_LENGTH,deargs,0,deargs.length); 51 | DecryptStart.decode(deargs,true); 52 | return connector.compareKey(args[KEY_INDEX],args[IP_INDEX],Integer.parseInt(args[PORT_INDEX])); 53 | } 54 | }catch (Exception e){ 55 | e.printStackTrace(); 56 | return "error"; 57 | } 58 | } 59 | 60 | @Override 61 | public String[] usage() { 62 | return new String[]{"解码代码,并判断远程端是否符合,前提连接了远程端","ip 服务器地址 port 端口 key 远端秘钥 class_key_filename 秘钥文件 classname... class字节码文件的包名+类名","ip port -f 选项 filename uuid秘钥的yml文件 key_name yml文件中的键名 class_key_filename classname..."}; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Dclass.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.DecryptStart; 4 | 5 | public class Dclass implements CommandBase { 6 | 7 | @Override 8 | public Object execute(String[] args) { 9 | try{ 10 | DecryptStart.decode(args,true); 11 | }catch (Exception e){ 12 | e.printStackTrace(); 13 | return "解码失败,缺少主要参数"; 14 | } 15 | return "解码成功"; 16 | } 17 | 18 | @Override 19 | public String[] usage() { 20 | return new String[]{"解码单个class文件","class_key_file 解码key文件名 classname class包名+类名"}; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/DeclassAll.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Pool; 4 | import net.noyark.www.utils.encode.SimpleClassCoder; 5 | 6 | /** 7 | * 解密全部项目的class文件,目前只支持将class文件加密打包 8 | * 9 | * declassall mainClass KeyFile 10 | * classpath在配置文件 11 | * 12 | */ 13 | public class DeclassAll implements CommandBase { 14 | 15 | @Override 16 | public Object execute(String[] args) { 17 | 18 | Pool.getClassCoder().recursiveDecode(args[0],args[1]); 19 | return "success"; 20 | } 21 | 22 | @Override 23 | public String[] usage() { 24 | return new String[]{"解密指定application classpath下全部的class文件","main_class 运行主类 keyFile 加密key"}; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Dev.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.DevTool; 4 | import net.noyark.www.utils.api.Pool; 5 | import net.noyark.www.utils.encode.Util; 6 | 7 | /** 8 | * 打包jar包的指令,默认为application的指定类路径 9 | * dev target 10 | * dev classpath target 11 | * dev -m classpath target main 12 | * dev -m target main 13 | * 也可以指定mainClass 14 | */ 15 | public class Dev implements CommandBase { 16 | 17 | @Override 18 | public Object execute(String[] args) { 19 | try{ 20 | DevTool tool = Pool.getDevJar(); 21 | if(args[0].equals("-m")){ 22 | if(args.length==3){ 23 | //-m target main 24 | tool.devJar(Util.getClassPath(""),args[1],args[2]); 25 | }else if(args.length==4){ 26 | tool.devJar(args[1],args[2],args[3]); 27 | }else{ 28 | return "打包失败"; 29 | } 30 | }else{ 31 | if(args.length==1){ 32 | tool.devJar(Util.getClassPath(""),args[0],null); 33 | }else{ 34 | tool.devJar(args[0],args[1],null); 35 | } 36 | } 37 | }catch (Exception e){ 38 | e.printStackTrace(); 39 | return "打包失败"; 40 | } 41 | return "success"; 42 | } 43 | 44 | @Override 45 | public String[] usage() { 46 | return new String[]{"打包成jar","target 目标路径","classpath 指定打包根路径,target 目标路径","-m选项 在前面基础上可以添加主运行类"}; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Ead.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Pool; 4 | import net.noyark.www.utils.encode.Util; 5 | 6 | 7 | /** 8 | * 加密并打包 9 | * ead main_class key_file target 10 | */ 11 | 12 | public class Ead implements CommandBase{ 13 | 14 | @Override 15 | public Object execute(String[] args) { 16 | try{ 17 | Pool.getClassCoder().recursiveEncode(args[0],args[1]); 18 | Pool.getDevJar().devJar(Util.getClassPath(""),args[2],args[0]); 19 | }catch (Exception e){ 20 | e.printStackTrace(); 21 | return "error"; 22 | } 23 | 24 | return "success"; 25 | } 26 | 27 | @Override 28 | public String[] usage() { 29 | return new String[]{"可以对一堆未加密的class文件加密,并打包","main_class 主类,keyFile 加密文件路径(根为${keyfile}) target 目标文件"}; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Echo.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | 4 | public class Echo implements CommandBase { 5 | 6 | @Override 7 | public Object execute(String[] args) { 8 | StringBuilder builder = new StringBuilder(); 9 | for(String arg:args){ 10 | builder.append(arg); 11 | } 12 | return builder; 13 | } 14 | 15 | @Override 16 | public String[] usage() { 17 | return new String[]{"输出","${var} 变量名"}; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Eclass.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.EncryptClasses; 4 | 5 | public class Eclass implements CommandBase{ 6 | 7 | @Override 8 | public Object execute(String[] args) { 9 | try{ 10 | String[] classes = new String[args.length-1]; 11 | System.arraycopy(args,1,classes,0,classes.length); 12 | EncryptClasses.encode(args[0],classes); 13 | }catch (Exception e){ 14 | e.printStackTrace(); 15 | return "加密失败"; 16 | } 17 | return "加密成功"; 18 | } 19 | 20 | @Override 21 | public String[] usage() { 22 | return new String[]{"keyfile 加密key文件 classname... 包名+类名"}; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/EnclassAll.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | 4 | import net.noyark.www.utils.api.Pool; 5 | 6 | public class EnclassAll implements CommandBase { 7 | 8 | @Override 9 | public Object execute(String[] args) { 10 | Pool.getClassCoder().recursiveEncode(args[0],args[1]); 11 | return "success"; 12 | } 13 | 14 | @Override 15 | public String[] usage() { 16 | return new String[]{"加密classpath下全部文件(application指定)","comment 注释 keyfile 加密key文件"}; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Exit.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.ex.ShutDownException; 4 | 5 | 6 | /** 7 | * 退出程序 8 | */ 9 | 10 | public class Exit implements CommandBase { 11 | 12 | 13 | @Override 14 | public Object execute(String[] args) { 15 | throw new ShutDownException(); 16 | } 17 | 18 | @Override 19 | public String[] usage() { 20 | return new String[]{"关闭程序"}; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Gclass.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.Util; 4 | 5 | public class Gclass implements CommandBase{ 6 | 7 | @Override 8 | public Object execute(String[] args) { 9 | return Util.getClassFile(); 10 | } 11 | 12 | @Override 13 | public String[] usage() { 14 | 15 | return new String[]{"获取classes文件路径,即程序默认加密的地方"}; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Help.kt: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command 2 | 3 | import net.noyark.www.utils.Message 4 | import java.util.* 5 | 6 | 7 | class Help constructor(var map:Map): CommandBase{ 8 | 9 | /** 10 | * help command 11 | */ 12 | override fun execute(args: Array?):Any { 13 | if(args!!.isNotEmpty()){ 14 | for(arg in args) { 15 | Message.info(Arrays.toString(map.get(arg)!!.usage())) 16 | } 17 | }else{ 18 | for(entry in map){ 19 | Message.info("-----"); 20 | var cmd = entry.key 21 | val cmdobj = entry.value 22 | var usages = cmdobj.usage() 23 | Message.info(cmd) 24 | for(usage in usages){ 25 | Message.info(usage) 26 | } 27 | } 28 | } 29 | return "" 30 | } 31 | 32 | override fun usage(): Array { 33 | return arrayOf("用于帮助") 34 | } 35 | 36 | 37 | } -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Jar.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | 4 | import net.noyark.www.utils.jar.DecodeJar; 5 | 6 | /** 7 | * jar jarfile key_file 加密方式 arg1 arg2 arg3 8 | */ 9 | 10 | public class Jar implements CommandBase { 11 | 12 | @Override 13 | public Object execute(String[] args) { 14 | DecodeJar jar = new DecodeJar(args[0],args[1], args[2]); 15 | String[] realArgs = new String[args.length-3]; 16 | System.arraycopy( args, 3, realArgs, 0, args.length-3 ); 17 | try{ 18 | jar.runJar(realArgs); 19 | }catch (Exception e){ 20 | e.printStackTrace(); 21 | return "Process finished with exit code -1"; 22 | } 23 | return "Process finished with exit code 0"; 24 | } 25 | 26 | @Override 27 | public String[] usage() { 28 | return new String[]{"运行加密jar包","jar_file jar包路径 key_file 加密方式(AES/DES) 加密key args... 主main方法参数"}; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Jarin.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.Util; 4 | 5 | public class Jarin implements CommandBase{ 6 | 7 | @Override 8 | public Object execute(String[] args) { 9 | return Util.getJarInFIle(); 10 | } 11 | 12 | @Override 13 | public String[] usage() { 14 | return new String[]{"获取jar包所在路径","${jarin}可以嵌入指令"}; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/KeyFile.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.Util; 4 | 5 | public class KeyFile implements CommandBase { 6 | 7 | @Override 8 | public Object execute(String[] args) { 9 | return Util.getKeyFile(); 10 | } 11 | 12 | @Override 13 | public String[] usage() { 14 | return new String[]{"获取key解析的路径,可以用于decode相关"}; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Random.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | public class Random extends ConnectorCommand{ 6 | 7 | public Random(Connector connector) { 8 | super(connector); 9 | } 10 | 11 | @Override 12 | public Object execute(String[] args) { 13 | if(!args[0].equals("")){ 14 | connector.randomKeys(Integer.parseInt(args[0])); 15 | }else{ 16 | connector.randomKey(); 17 | } 18 | return "生成成功"; 19 | } 20 | 21 | @Override 22 | public String[] usage() { 23 | return new String[]{"可以生成多个授权码,并插入授权端","number 生成数量"}; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Reboot.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.ex.ShutDownException; 4 | 5 | import java.util.concurrent.CountDownLatch; 6 | 7 | @Deprecated 8 | public class Reboot implements CommandBase { 9 | 10 | private CountDownLatch latch; 11 | 12 | public Reboot(CountDownLatch latch) { 13 | this.latch = latch; 14 | } 15 | 16 | @Override 17 | public Object execute(String[] args) { 18 | latch.countDown(); 19 | throw new ShutDownException(); 20 | } 21 | 22 | @Override 23 | public String[] usage() { 24 | return new String[]{"淘汰"}; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/SPwd.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | /** 6 | * 设置或者修改密码 7 | */ 8 | 9 | public class SPwd extends ConnectorCommand{ 10 | 11 | public SPwd(Connector connector) { 12 | super(connector); 13 | } 14 | 15 | @Override 16 | public Object execute(String[] args) { 17 | connector.setPassword(args[0]); 18 | return "设置数据库连接密码成功"+args[0].replaceAll(".","\\*"); 19 | } 20 | 21 | @Override 22 | public String[] usage() { 23 | return new String[]{"设置密码","password 数据库密码"}; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/SUser.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | /** 6 | * 设置或者修改用户名 7 | */ 8 | 9 | public class SUser extends ConnectorCommand{ 10 | 11 | public SUser(Connector connector) { 12 | super(connector); 13 | } 14 | 15 | @Override 16 | public Object execute(String[] args) { 17 | connector.setUserName(args[0]); 18 | return "设置数据库连接用户名成功"+args[0]; 19 | } 20 | 21 | @Override 22 | public String[] usage() { 23 | return new String[]{"修改用户名","user_name 用户名"}; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Save.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | import net.noyark.www.utils.DB_CONNECT; 5 | import org.apache.commons.io.FileUtils; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * 将设置的数据库信息保存到持久层 14 | */ 15 | public class Save extends ConnectorCommand { 16 | 17 | public Save(Connector connector) { 18 | super(connector); 19 | } 20 | 21 | public static final int USERNAME = 0; 22 | 23 | public static final int PASSWORD = 1; 24 | 25 | public static final int DB_NAME = 2; 26 | 27 | public static final int IP = 3; 28 | 29 | public static final int PORT = 4; 30 | 31 | public static final int START_TYPE = 5; 32 | 33 | public static final int TABLE = 6; 34 | 35 | //save fileName 36 | @Override 37 | public Object execute(String[] args) { 38 | 39 | DB_CONNECT db_connect = (DB_CONNECT) connector; 40 | 41 | File file = new File(args[0]); 42 | 43 | List informationList = new ArrayList<>(); 44 | 45 | informationList.add(db_connect.getUserName());//0 46 | informationList.add(db_connect.getPassword());//1 47 | informationList.add(db_connect.getDbName());//2 48 | informationList.add(db_connect.getIp());//3 49 | informationList.add(db_connect.getPort()+"");//4 50 | informationList.add(db_connect.getType().getStart());//5 51 | informationList.add(db_connect.getTable());//6 52 | try{ 53 | FileUtils.writeLines(file,informationList); 54 | }catch (IOException e){ 55 | e.printStackTrace(); 56 | } 57 | return "save successfully"; 58 | } 59 | 60 | @Override 61 | public String[] usage() { 62 | return new String[]{"将目前数据库配置保存到硬盘"}; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Sppf.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.encode.Util; 4 | 5 | /** 6 | * 设置配置文件路径,如同application.properties 7 | * 8 | * sppf filename 9 | */ 10 | public class Sppf implements CommandBase{ 11 | 12 | @Override 13 | public Object execute(String[] args) { 14 | Util.setReadApplication(args[0]); 15 | return "set successfully"; 16 | } 17 | 18 | @Override 19 | public String[] usage() { 20 | return new String[]{"设置配置文件路径","file_name 路径+文件名"}; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/Stable.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command; 2 | 3 | import net.noyark.www.utils.api.Connector; 4 | 5 | public class Stable extends ConnectorCommand { 6 | 7 | public Stable(Connector connector) { 8 | super(connector); 9 | } 10 | 11 | @Override 12 | public Object execute(String[] args) { 13 | connector.setTable(args[0]); 14 | return "设置成功"+args[0]; 15 | } 16 | 17 | @Override 18 | public String[] usage() { 19 | return new String[]{"设置连接的表","table_name 表名"}; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/Ackey.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.safe.encode.AESGenerateKey; 4 | 5 | /** 6 | * 创建aes专用key 7 | * ackey keyfile 8 | */ 9 | 10 | public class Ackey implements SafeCommand{ 11 | 12 | @Override 13 | public Object execute(String[] args) { 14 | try{ 15 | AESGenerateKey.createKey(args[0]); 16 | }catch (Exception e){ 17 | e.printStackTrace(); 18 | return "生成失败"; 19 | } 20 | return "生成成功"; 21 | } 22 | 23 | @Override 24 | public String[] usage() { 25 | return new String[]{"ackey 用于生成aes秘钥","key_file 生成路径"}; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/Adc.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.api.Pool; 4 | 5 | /** 6 | * 解密单个类 7 | */ 8 | 9 | public class Adc implements SafeCommand { 10 | 11 | @Override 12 | public Object execute(String[] args) { 13 | 14 | 15 | Pool.getAESClassCoder().decode(args[0],args[1],true); 16 | 17 | return null; 18 | } 19 | 20 | @Override 21 | public String[] usage() { 22 | return new String[]{"adc 解密单个类","file_name key文件 classname 类名"}; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/Adeclassall.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.api.Pool; 4 | 5 | /** 6 | * 解密classpath下全部类 7 | */ 8 | 9 | public class Adeclassall implements SafeCommand{ 10 | 11 | @Override 12 | public Object execute(String[] args) { 13 | 14 | Pool.getAESClassCoder().recursiveDecode(args[0],args[1]); 15 | 16 | return "解密成功"; 17 | } 18 | 19 | @Override 20 | public String[] usage() { 21 | return new String[]{"adeclassall 用于解密classpath下的aes加密class文件","mainClass 运行入口 keyFile 解密文件"}; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/Aec.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.safe.encode.SafeEncryptClasses; 4 | 5 | /** 6 | * aec 以aes-base64标准加密一个类 7 | * aec key_file classname1 classname2 8 | */ 9 | 10 | public class Aec implements SafeCommand{ 11 | 12 | @Override 13 | public Object execute(String[] args) { 14 | String[] classes = new String[args.length-1]; 15 | System.arraycopy(args,1,classes,0,classes.length); 16 | SafeEncryptClasses safeEncryptClasses = new SafeEncryptClasses(); 17 | safeEncryptClasses.encode(args[0],classes); 18 | return "加密成功"; 19 | } 20 | 21 | @Override 22 | public String[] usage() { 23 | return new String[]{"aec 加密为aes类型","key_file 秘钥文件 classname1 classname2"}; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/Aenclassall.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.api.Pool; 4 | 5 | /** 6 | * 加密全部classpath下的类文件 7 | */ 8 | 9 | public class Aenclassall implements SafeCommand{ 10 | 11 | @Override 12 | public Object execute(String[] args) { 13 | Pool.getAESClassCoder().recursiveEncode(args[0],args[1]); 14 | return "加密"; 15 | } 16 | 17 | @Override 18 | public String[] usage() { 19 | return new String[]{"aenclassall 加密全部classpath的类文件,以aes形式加密","comment 注释,随便写,keyfile key文件路径"}; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/Akeyec.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.api.Pool; 4 | 5 | /** 6 | * 同时创建key 7 | */ 8 | public class Akeyec implements SafeCommand{ 9 | 10 | @Override 11 | public Object execute(String[] args) { 12 | String[] classes = new String[args.length-1]; 13 | System.arraycopy(args,1,classes,0,classes.length); 14 | Pool.getAESClassCoder().encode(args[0],classes); 15 | return "创建key并加密成功"; 16 | } 17 | 18 | @Override 19 | public String[] usage() { 20 | return new String[]{"akeyec 可以创建key文件并同时加密","keyfile key文件路径 classname... 类名"}; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/command/newcommand/SafeCommand.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.command.newcommand; 2 | 3 | import net.noyark.www.utils.command.CommandBase; 4 | 5 | /** 6 | * 这里标识的是最新的加密解决方案的指令,更加安全有效 7 | */ 8 | public interface SafeCommand extends CommandBase { 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/encode/DecryptStart.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.encode; 2 | 3 | import net.noyark.www.utils.Message; 4 | 5 | import java.io.*; 6 | import java.security.*; 7 | import java.lang.reflect.*; 8 | import java.util.Arrays; 9 | import java.util.Enumeration; 10 | import java.util.jar.JarEntry; 11 | import java.util.jar.JarFile; 12 | import javax.crypto.*; 13 | import javax.crypto.spec.*; 14 | 15 | /** 16 | * 加密的代码 17 | * 流程很简单,通过api生成key,通过key把class文件内容读出来编码再写进去 18 | * 之后一个编码的文件就诞生的 19 | * 怎么解码,也很简单,把key放进去,然后用定制的classloader把class对象取出,api根据key解密,就ok了 20 | *解密考虑问题:就是万一是系统的class,那么分三步走,总有一款适合你 21 | */ 22 | 23 | public class DecryptStart extends ClassLoader 24 | { 25 | private Cipher cipher; 26 | 27 | private InputStream in; 28 | 29 | private long len; 30 | 31 | public DecryptStart(SecretKey key) throws GeneralSecurityException { 32 | setKey(key); 33 | } 34 | public DecryptStart(SecretKey key,ClassLoader parent) throws GeneralSecurityException{ 35 | super(parent); 36 | setKey(key); 37 | } 38 | 39 | public void setKey(SecretKey key) throws GeneralSecurityException{ 40 | String algorithm = "DES"; 41 | SecureRandom sr = new SecureRandom(); 42 | if(Util.getDecodeMessageOut()){ 43 | Message.info("[DecryptStart: creating cipher]"); 44 | } 45 | cipher = Cipher.getInstance(algorithm); 46 | cipher.init(Cipher.DECRYPT_MODE, key, sr); 47 | } 48 | 49 | public DecryptStart(SecretKey key,InputStream in,long len) throws GeneralSecurityException{ 50 | setKey(key); 51 | setIn(in); 52 | this.len = len; 53 | } 54 | 55 | public DecryptStart(SecretKey key,InputStream in,long len,ClassLoader parent) throws GeneralSecurityException{ 56 | super(parent); 57 | setKey(key); 58 | setIn(in); 59 | this.len = len; 60 | } 61 | 62 | public void setIn(InputStream in){ 63 | this.in = in; 64 | } 65 | 66 | /** 67 | * 把之前密钥放入,接着解密,通过定制classloader加载类,就ok了 68 | * @param args 69 | * @param executeMain 70 | * @return 71 | * @throws Exception 72 | */ 73 | public static Class decode(String args[],boolean executeMain) throws Exception { 74 | String keyFilename = args[0]; 75 | String appName = args[1]; 76 | 77 | // 传递给应用本身的参数 78 | String realArgs[] = new String[args.length-2]; 79 | System.arraycopy( args, 2, realArgs, 0, args.length-2 ); 80 | 81 | // 创建解密的ClassLoader 82 | DecryptStart dr = new DecryptStart(Util.readKey(keyFilename)); 83 | 84 | // 创建应用主类的一个实例,通过ClassLoader装入它 85 | if(Util.getDecodeMessageOut()){ 86 | Message.info("[DecryptStart: loading "+appName+"]"); 87 | } 88 | 89 | Class clasz = dr.loadClass(appName.replaceAll("/|\\\\",".")); 90 | 91 | executeMain(clasz,realArgs,appName,executeMain); 92 | return clasz; 93 | } 94 | 95 | private static void executeMain(Class clasz,String[] realArgs,String appName,boolean executeMain){ 96 | if(executeMain){ 97 | try{ 98 | /*反射调用main方法 99 | * 100 | */ 101 | Class mainArgs[] = { (new String[1]).getClass() }; 102 | Method main = clasz.getMethod("main", mainArgs); 103 | 104 | // 创建一个包含main()方法参数的数组 105 | Object argsArray[] = { realArgs }; 106 | Message.info("[DecryptStart: running "+appName+".main()]"); 107 | 108 | // 调用main() 109 | main.invoke(null, argsArray); 110 | }catch (Exception e){ 111 | Message.error("没有找到main方法应用程序入口,无法执行"); 112 | } 113 | } 114 | } 115 | 116 | public Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 117 | try { 118 | // 要创建的Class对象 119 | Class clasz; 120 | 121 | // 必需的步骤1:如果类已经在系统缓冲之中,不必再次装入它 122 | clasz = findLoadedClass(name); 123 | 124 | if (clasz != null) 125 | return clasz; 126 | 127 | // 下面是定制部分 128 | try{ 129 | String filename = Util.getClassPath(name); 130 | //读取经过加密的类文件 131 | byte classData[]; 132 | if(in ==null){ 133 | classData = Util.readFile(filename+".class"); 134 | }else{ 135 | classData = Util.readFile(in,len); 136 | } 137 | if(classData != null){ 138 | byte decryptedClassData[] = cipher.doFinal(classData); //解密 139 | clasz = defineClass( name, decryptedClassData, 0, decryptedClassData.length); // 再把它转换成一个类 140 | if(Util.getDecodeMessageOut()) { 141 | Message.info("[DecryptStart: decrypting class " + name + "]"); 142 | } 143 | } 144 | }catch(FileNotFoundException e){ 145 | } 146 | 147 | // 必需的步骤2:如果上面没有成功 148 | // 尝试用默认的ClassLoader装入它 149 | if (clasz == null) 150 | clasz = findSystemClass(name); 151 | 152 | // 必需的步骤3:如有必要,则装入相关的类 153 | if (resolve && clasz != null) 154 | resolveClass(clasz); 155 | return clasz;//把类返回给调用者 156 | 157 | }catch (Exception e){ 158 | e.printStackTrace(); 159 | } 160 | return null; 161 | } 162 | 163 | } -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/encode/EncryptClasses.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.encode; 2 | 3 | import net.noyark.www.utils.Message; 4 | 5 | import java.security.*; 6 | import javax.crypto.*; 7 | import javax.crypto.spec.*; 8 | 9 | public class EncryptClasses 10 | { 11 | static public void encode(String keyFilename,String args[]) throws Exception { 12 | String algorithm = "DES"; 13 | 14 | // 生成密匙 15 | SecureRandom sr = new SecureRandom(); 16 | byte rawKey[] = Util.readFile(Util.getKeyFile()+"/"+keyFilename); 17 | DESKeySpec dks = new DESKeySpec(rawKey); 18 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( algorithm ); 19 | SecretKey key = keyFactory.generateSecret(dks); 20 | 21 | // 创建用于实际加密操作的Cipher对象 22 | Cipher ecipher = Cipher.getInstance(algorithm); 23 | ecipher.init(Cipher.ENCRYPT_MODE, key, sr); 24 | 25 | // 加密命令行中指定的每一个类 26 | for (int i=0; i decode(String keyFile,String classname,boolean executeMain){ 28 | try{ 29 | return DecryptStart.decode(new String[]{keyFile,classname},executeMain); 30 | }catch (Exception e){ 31 | e.printStackTrace(); 32 | throw new ParseException("the class have some problems..",e); 33 | } 34 | } 35 | //不执行main 36 | public Class decode(String keyFile,String classname){ 37 | return decode(keyFile,classname,false); 38 | } 39 | 40 | @Override 41 | public Class getClassInJar(String jarFile, String classname, String keyFile) { 42 | return Util.getClassInJar(jarFile,classname,keyFile,DecryptMethod.DES); 43 | } 44 | 45 | @Override 46 | public Class getClassInJar(String jarFile, String classname, String keyFile, ClassLoader loader) { 47 | return Util.getClassInJar(jarFile,classname,keyFile,loader,DecryptMethod.DES); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/encode/Util.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.encode; 2 | 3 | 4 | import net.noyark.www.utils.Message; 5 | import net.noyark.www.utils.Coder; 6 | import net.noyark.www.utils.jar.DecodeJar; 7 | 8 | import javax.crypto.SecretKey; 9 | import javax.crypto.SecretKeyFactory; 10 | import javax.crypto.spec.DESKeySpec; 11 | import java.io.*; 12 | import java.util.*; 13 | 14 | public class Util 15 | { 16 | private static InputStream readApplication; 17 | // 把文件读入byte数组 18 | static public byte[] readFile(String filename) throws IOException { 19 | File file = new File(filename); 20 | long len = file.length(); 21 | return readFile(new FileInputStream(file),len); 22 | } 23 | 24 | static public byte[] readFile(InputStream in,long len) throws IOException{ 25 | 26 | byte[] bytes = new byte[(int)len]; 27 | in.read(bytes); 28 | in.close(); 29 | return bytes; 30 | 31 | } 32 | 33 | // 把byte数组写出到文件 34 | static public void writeFile(String filename, byte data[]) throws IOException { 35 | FileOutputStream fout = new FileOutputStream(filename); 36 | fout.write(data); 37 | fout.close(); 38 | } 39 | 40 | static public void writeFile(String fileName,String data) throws IOException{ 41 | PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"UTF-8"))); 42 | writer.println(data); 43 | writer.close(); 44 | } 45 | 46 | static public String readKeyFile(String fileName) throws IOException{ 47 | BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); 48 | return reader.readLine(); 49 | } 50 | 51 | static public void writeClassData(String filename,byte[] encryptedClassData) throws IOException{ 52 | String out = Util.getClassOut(); 53 | if(out.startsWith("THIS")) { 54 | Util.writeFile(filename + ".class", encryptedClassData); // 保存加密后的内容 55 | }else{ 56 | Util.writeFile(out,encryptedClassData); 57 | } 58 | } 59 | 60 | /** 61 | * classpath:file 62 | * @param file 63 | */ 64 | public static void setReadApplication(String file){ 65 | try{ 66 | if(file.startsWith("classpath:")){ 67 | readApplication = Util.class.getResourceAsStream(file.replace("classpath:","")); 68 | }else{ 69 | readApplication = new FileInputStream(file); 70 | } 71 | }catch (IOException e){ 72 | e.printStackTrace(); 73 | } 74 | } 75 | 76 | public static File getClassFile(){ 77 | return new File(getJarInFIle()+"classes"); 78 | } 79 | 80 | public static File getKeyFile(){ 81 | return new File(getJarInFIle()+"/keyfile"); 82 | } 83 | 84 | public static File getJarInFIle(){ 85 | return new File(Util.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile(); 86 | } 87 | 88 | public static String getClassPath(String name){ 89 | String filename = null; 90 | try{ 91 | Properties properties = new Properties(); 92 | InputStream in; 93 | //自定义配置文件位置 94 | if(readApplication == null){ 95 | in = new FileInputStream(getJarInFIle()+"/application.properties"); 96 | }else{ 97 | in = readApplication; 98 | } 99 | if(in == null){ 100 | filename = getJarInFIle()+"/"+name; 101 | }else{ 102 | properties.load(in); 103 | String cp = properties.getProperty("classpath"); 104 | if(cp.startsWith("now:")){ 105 | filename = (cp.replace("now:",getJarInFIle().toString()+"/")+"/"+name).replace(".","/"); 106 | } 107 | 108 | } 109 | 110 | }catch (IOException e){ 111 | e.printStackTrace(); 112 | } 113 | return filename; 114 | } 115 | 116 | 117 | public static String getClassOut(){ 118 | try{ 119 | Properties properties = new Properties(); 120 | properties.load(getInputStreamOfApplication()); 121 | String to = properties.getProperty("encode.to"); 122 | if(to.equals("this")){ 123 | return "THIS:在当前的class文件所在地,会被覆盖"; 124 | }else{ 125 | //输出在out文件 126 | return getJarInFIle()+"/"+to; 127 | } 128 | }catch (IOException e){ 129 | e.printStackTrace(); 130 | } 131 | return null; 132 | } 133 | 134 | public static InputStream getInputStreamOfApplication() throws IOException{ 135 | InputStream in; 136 | if(readApplication == null){ 137 | in = new FileInputStream(getJarInFIle()+"/application.properties"); 138 | }else{ 139 | in = readApplication; 140 | } 141 | return in; 142 | } 143 | 144 | public static boolean getDecodeMessageOut(){ 145 | try{ 146 | InputStream in =getInputStreamOfApplication(); 147 | Properties properties = new Properties(); 148 | properties.load(in); 149 | return Boolean.parseBoolean(properties.getProperty("decode.message")); 150 | }catch (Exception e){ 151 | e.printStackTrace(); 152 | } 153 | return false; 154 | } 155 | 156 | public static SecretKey readKey(String keyFilename) throws Exception{ 157 | // 读取密匙 158 | if(Util.getDecodeMessageOut()) { 159 | Message.info("[DecryptStart: reading key]"); 160 | } 161 | byte rawKey[] = Util.readFile(keyFilename); 162 | DESKeySpec dks = new DESKeySpec(rawKey); 163 | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); 164 | return keyFactory.generateSecret(dks); 165 | } 166 | 167 | 168 | public static Class getClassInJar(String jarFile, String classname, String keyFile, Coder.DecryptMethod method) { 169 | return getClassInJar(jarFile,classname,keyFile,null,method); 170 | } 171 | 172 | 173 | public static Class getClassInJar(String jarFile, String classname, String keyFile, ClassLoader loader, Coder.DecryptMethod method) { 174 | 175 | try{ 176 | return new DecodeJar(jarFile,keyFile,method.getMethod()).getDecodeClass(classname,loader); 177 | }catch (Exception e){ 178 | return null; 179 | } 180 | } 181 | 182 | } 183 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/ex/DBConnectException.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.ex; 2 | 3 | public class DBConnectException extends RuntimeException { 4 | 5 | public DBConnectException(String message, Throwable cause) { 6 | super(message, cause); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/ex/ParseException.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.ex; 2 | 3 | public class ParseException extends RuntimeException { 4 | 5 | public ParseException(String message,Throwable cause){ 6 | super(message,cause); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/ex/ShutDownException.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.ex; 2 | 3 | public class ShutDownException extends RuntimeException { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/jar/DecodeJar.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.jar; 2 | 3 | import net.noyark.www.utils.Message; 4 | import net.noyark.www.utils.encode.DecryptStart; 5 | import net.noyark.www.utils.encode.Util; 6 | import net.noyark.www.utils.safe.encode.SafeClassLoader; 7 | 8 | import java.io.*; 9 | import java.lang.reflect.InvocationTargetException; 10 | import java.net.MalformedURLException; 11 | import java.net.URL; 12 | import java.net.URLClassLoader; 13 | import java.util.Iterator; 14 | import java.util.jar.Attributes; 15 | import java.util.jar.JarEntry; 16 | import java.util.jar.JarException; 17 | import java.util.jar.JarFile; 18 | /** 19 | * 将加密的jar包运行 20 | */ 21 | 22 | public class DecodeJar { 23 | 24 | private File jarFile; 25 | 26 | private String keyFile; 27 | 28 | private String decodeMethod;//AES/DES 29 | 30 | public DecodeJar(String jarFileName,String keyFile,String decodeMethod) { 31 | this.jarFile = new File(jarFileName); 32 | this.keyFile = keyFile; 33 | this.decodeMethod = decodeMethod; 34 | } 35 | 36 | public void runJar(String[] args) throws IllegalAccessException,NoSuchMethodException, InvocationTargetException { 37 | Class claz = getMainClass(); 38 | if(claz == null){ 39 | Message.error("没有主清单目录"); 40 | return; 41 | } 42 | claz.getMethod("main",String[].class).invoke(null,(Object) args); 43 | } 44 | 45 | public Class getMainClass(){ 46 | try{ 47 | JarFile jarFile = new JarFile(this.jarFile); 48 | Iterator entries = jarFile.stream().iterator(); 49 | InputStream MF_STREAM = null; 50 | while (entries.hasNext()){ 51 | JarEntry jar = entries.next(); 52 | if("META-INF/MANIFEST.MF".equals(jar.getName())){ 53 | MF_STREAM = jarFile.getInputStream(jar); 54 | break; 55 | } 56 | } 57 | if(MF_STREAM == null){ 58 | throw new JarException("这个jar文件无法被运行,由于它没有MANIFEST.MF"); 59 | }else{ 60 | String main_class = jarFile.getManifest().getMainAttributes().get(Attributes.Name.MAIN_CLASS).toString(); 61 | if(main_class!=null){ 62 | return getDecodeClass(main_class); 63 | }else{ 64 | return null; 65 | } 66 | } 67 | }catch (Exception e){ 68 | e.printStackTrace(); 69 | } 70 | return null; 71 | } 72 | 73 | public Class getDecodeClass(String main_class) throws Exception{ 74 | return getDecodeClass(main_class,null); 75 | } 76 | 77 | //DES或者AES 78 | public Class getDecodeClass(String main_class,ClassLoader parent) throws Exception{ 79 | JarFile jarFile = new JarFile(this.jarFile); 80 | Iterator mainGets = jarFile.stream().iterator(); 81 | while (mainGets.hasNext()){ 82 | JarEntry entry = mainGets.next(); 83 | if(entry.getName().replaceAll("/|\\\\",".").equals(main_class+".class")){ 84 | if(parent== null){ 85 | ClassLoader start = createDeClassLoader(decodeMethod,keyFile,getMainInputStream(main_class),entry.getSize()); 86 | return start.loadClass(main_class); 87 | }else{ 88 | ClassLoader start = createDeClassLoader(decodeMethod,keyFile,getMainInputStream(main_class),entry.getSize(),parent); 89 | return start.loadClass(main_class); 90 | } 91 | } 92 | } 93 | return null; 94 | } 95 | 96 | public InputStream getMainInputStream(String main_class) throws MalformedURLException { 97 | URLClassLoader loader = new URLClassLoader(new URL[]{this.jarFile.toURI().toURL()}); 98 | String classname = main_class.replace(".","/"); 99 | if(!classname.endsWith(".class")){ 100 | classname = classname+".class"; 101 | } 102 | return loader.getResourceAsStream(classname); 103 | } 104 | 105 | public ClassLoader createDeClassLoader(String es,String keyFile,InputStream main,long size) throws Exception{ 106 | if(es.equals("DES")){ 107 | return new DecryptStart(Util.readKey(keyFile),main,size); 108 | }else{ 109 | return new SafeClassLoader(keyFile,main,size); 110 | } 111 | } 112 | 113 | public ClassLoader createDeClassLoader(String es,String keyFile,InputStream main,long size,ClassLoader loader) throws Exception{ 114 | if(es.equals("DES")){ 115 | return new DecryptStart(Util.readKey(keyFile),main,size,loader); 116 | }else{ 117 | return new SafeClassLoader(keyFile,main,size,loader); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/safe/encode/AESGenerateKey.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.safe.encode; 2 | 3 | /** 4 | * 生成key文件 5 | */ 6 | public class AESGenerateKey { 7 | 8 | static public void createKey(String keyFile) throws Exception{ 9 | CodeUtils.randomKey(keyFile); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/safe/encode/CodeUtils.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.safe.encode; 2 | 3 | import net.noyark.www.utils.encode.Util; 4 | 5 | import javax.crypto.Cipher; 6 | import javax.crypto.KeyGenerator; 7 | import javax.crypto.spec.SecretKeySpec; 8 | import java.io.IOException; 9 | import java.util.Base64; 10 | 11 | /** 12 | * 对于aes加密的调优解决方案 13 | * 未来将采用最新的解决方案 14 | */ 15 | 16 | public class CodeUtils { 17 | 18 | /** 19 | * 密钥 20 | */ 21 | private static final String KEY = "1234567887654321";// AES加密要求key必须要128个比特位(这里需要长度为16,否则会报错) 22 | 23 | public static String randomKey(String fileName) throws IOException { 24 | String randomKey = (System.currentTimeMillis()+""+(((fileName.hashCode()&Integer.MAX_VALUE)%25)+100)+""); 25 | if(randomKey.length()>16){ 26 | randomKey = randomKey.substring(0,16); 27 | } 28 | Util.writeFile(fileName,randomKey); 29 | return randomKey; 30 | } 31 | 32 | public static String readKey(String fileName) throws IOException{ 33 | return Util.readKeyFile(fileName); 34 | } 35 | 36 | /** 37 | * 算法 38 | */ 39 | private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding"; 40 | 41 | 42 | /** 43 | * base 64 encode 44 | * @param bytes 待编码的byte[] 45 | * @return 编码后的base 64 code 46 | */ 47 | private static byte[] base64Encode(byte[] bytes){ 48 | return Base64.getEncoder().encode(bytes); 49 | } 50 | 51 | /** 52 | * base 64 decode 53 | * @param base64Code 待解码的base 64 code 54 | * @return 解码后的byte[] 55 | * @throws Exception 抛出异常 56 | */ 57 | private static byte[] base64Decode(byte[] base64Code) throws Exception{ 58 | return Base64.getDecoder().decode(base64Code); 59 | } 60 | 61 | 62 | /** 63 | * AES加密 64 | * @param content 待加密的内容 65 | * @param encryptKey 加密密钥 66 | * @return 加密后的byte[] 67 | */ 68 | private static byte[] aesEncryptToBytes(byte[] content, String encryptKey) throws Exception { 69 | KeyGenerator kgen = KeyGenerator.getInstance("AES"); 70 | kgen.init(128); 71 | Cipher cipher = Cipher.getInstance(ALGORITHMSTR); 72 | cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES")); 73 | 74 | return cipher.doFinal(content); 75 | } 76 | 77 | 78 | /** 79 | * AES加密为base 64 code 80 | * 81 | * @param content 待加密的内容 82 | * @param encryptKey 加密密钥 83 | * @return 加密后的base 64 code 84 | */ 85 | public static byte[] aesEncrypt(byte[] content, String encryptKey) throws Exception { 86 | return base64Encode(aesEncryptToBytes(content, encryptKey)); 87 | } 88 | 89 | /** 90 | * AES解密 91 | * 92 | * @param encryptBytes 待解密的byte[] 93 | * @param decryptKey 解密密钥 94 | * @return 解密后的String 95 | */ 96 | private static byte[] aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception { 97 | KeyGenerator kgen = KeyGenerator.getInstance("AES"); 98 | kgen.init(128); 99 | 100 | Cipher cipher = Cipher.getInstance(ALGORITHMSTR); 101 | cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES")); 102 | return cipher.doFinal(encryptBytes); 103 | } 104 | 105 | 106 | /** 107 | * 将base 64 code AES解密 108 | * 109 | * @param encryptStr 待解密的base 64 code 110 | * @param decryptKey 解密密钥 111 | * @return 解密后的string 112 | */ 113 | public static byte[] aesDecrypt(byte[] encryptStr, String decryptKey) throws Exception { 114 | return aesDecryptByBytes(base64Decode(encryptStr), decryptKey); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/safe/encode/SafeClassCoder.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.safe.encode; 2 | 3 | import net.noyark.www.utils.api.ClassCoder; 4 | import net.noyark.www.utils.encode.Util; 5 | 6 | 7 | /** 8 | * 最新的更安全的加密对象,基于aes和base64加密 9 | */ 10 | 11 | public class SafeClassCoder extends ClassCoder { 12 | 13 | 14 | @Override 15 | public void encode(String fileName, String... classname) { 16 | try{ 17 | AESGenerateKey.createKey(fileName); 18 | SafeEncryptClasses safeEncryptClasses = new SafeEncryptClasses(); 19 | safeEncryptClasses.encode(fileName,classname); 20 | }catch (Exception e){ 21 | e.printStackTrace(); 22 | } 23 | } 24 | 25 | @Override 26 | public Class decode(String keyFile, String classname, boolean executeMain) { 27 | try{ 28 | Class thisClass = new SafeClassLoader(keyFile).loadClass(classname); 29 | if(executeMain) { 30 | thisClass.getClass().getDeclaredMethod("main").invoke(null); 31 | } 32 | }catch (Exception e){ 33 | e.printStackTrace(); 34 | } 35 | return null; 36 | } 37 | 38 | @Override 39 | public Class decode(String keyFile, String classname) { 40 | return decode(keyFile,classname,false); 41 | } 42 | 43 | @Override 44 | public Class getClassInJar(String jarFile, String classname, String keyFile) { 45 | return Util.getClassInJar(jarFile,classname,keyFile,DecryptMethod.AES); 46 | } 47 | 48 | @Override 49 | public Class getClassInJar(String jarFile, String classname, String keyFile, ClassLoader loader) { 50 | return Util.getClassInJar(jarFile,classname,keyFile,loader,DecryptMethod.AES); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/safe/encode/SafeClassLoader.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.safe.encode; 2 | 3 | 4 | import net.noyark.www.utils.encode.Util; 5 | 6 | import java.io.FileNotFoundException; 7 | import java.io.InputStream; 8 | 9 | public class SafeClassLoader extends ClassLoader { 10 | 11 | private String keyFile; 12 | 13 | private InputStream in; 14 | 15 | private long len; 16 | 17 | public SafeClassLoader(String keyFile){ 18 | this.keyFile = keyFile; 19 | } 20 | 21 | public SafeClassLoader(String keyFile,ClassLoader parent){ 22 | super(parent); 23 | this.keyFile = keyFile; 24 | } 25 | 26 | public SafeClassLoader(String keyFile,InputStream in,long len,ClassLoader parent){ 27 | super(parent); 28 | this.keyFile = keyFile; 29 | this.len = len; 30 | setInputStream(in); 31 | } 32 | 33 | public SafeClassLoader(String keyFile,InputStream in,long len){ 34 | this(keyFile); 35 | this.len = len; 36 | setInputStream(in); 37 | } 38 | 39 | public void setInputStream(InputStream in){ 40 | this.in = in; 41 | } 42 | 43 | 44 | @Override 45 | protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 46 | 47 | Class clasz; 48 | 49 | 50 | clasz = findLoadedClass(name); 51 | 52 | if (clasz != null) 53 | return clasz; 54 | 55 | //加密部分 56 | 57 | // 下面是定制部分 58 | try{ 59 | String filename = Util.getClassPath(name); 60 | //读取经过加密的类文件 61 | byte[] classData; 62 | if(in ==null){ 63 | classData = Util.readFile(filename+".class"); 64 | }else{ 65 | classData = Util.readFile(in,len); 66 | } 67 | if(classData!=null){ 68 | //keyfile读取 69 | byte[] decryptedClassData = CodeUtils.aesDecrypt(classData,CodeUtils.readKey(keyFile)); //解密 70 | clasz = defineClass( name, decryptedClassData, 0, decryptedClassData.length); // 再把它转换成一个类 71 | } 72 | }catch(Exception e){ 73 | if(!(e instanceof FileNotFoundException)){ 74 | e.printStackTrace(); 75 | } 76 | } 77 | 78 | if (clasz == null) 79 | clasz = findSystemClass(name); 80 | 81 | 82 | if (resolve && clasz != null) 83 | resolveClass(clasz); 84 | return clasz; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/safe/encode/SafeEncryptClasses.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.safe.encode; 2 | 3 | import net.noyark.www.utils.Message; 4 | import net.noyark.www.utils.encode.Util; 5 | 6 | /** 7 | * 更加安全的加密方式 8 | */ 9 | 10 | public class SafeEncryptClasses { 11 | 12 | public void encode(String keyFile,String... classnames){ 13 | try{ 14 | String key = CodeUtils.readKey(keyFile); 15 | for(String classname:classnames){ 16 | String filename = Util.getClassPath(classname); 17 | byte[] classData = Util.readFile(filename+".class"); 18 | byte[] aesData = CodeUtils.aesDecrypt(classData,key); 19 | Util.writeClassData(filename,aesData); 20 | Message.info("Encrypted " + filename); 21 | } 22 | }catch (Exception e){ 23 | e.printStackTrace(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/stream/ClassStream.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.stream; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * class对象处理流 8 | */ 9 | 10 | public class ClassStream { 11 | 12 | private List> allClasses; 13 | 14 | public ClassStream(List> allClasses) { 15 | this.allClasses = allClasses; 16 | } 17 | 18 | public ClassStream filter(CompareTo compareTo){ 19 | List> getClasses = new ArrayList<>(); 20 | for(Class clz:allClasses) { 21 | boolean isIt = compareTo.compare(clz); 22 | if(isIt){ 23 | getClasses.add(clz); 24 | } 25 | } 26 | return new ClassStream(getClasses); 27 | } 28 | public ClassStream mustInstanceOf(Class compare){ 29 | List> getClasses = new ArrayList<>(); 30 | try{ 31 | for(Class clz:allClasses) { 32 | if(compare.isInstance(clz.newInstance())){ 33 | getClasses.add(clz); 34 | } 35 | } 36 | }catch (Exception e){ 37 | e.printStackTrace(); 38 | } 39 | return new ClassStream(getClasses); 40 | } 41 | 42 | public List> asList(){ 43 | return allClasses; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/net/noyark/www/utils/stream/CompareTo.java: -------------------------------------------------------------------------------- 1 | package net.noyark.www.utils.stream; 2 | 3 | @FunctionalInterface 4 | public interface CompareTo { 5 | 6 | boolean compare(Class clz); 7 | 8 | } 9 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: snakeyaml-1.24.jar mysql-connector-java-5.1.25.jar commons 3 | -pool2-2.6.1.jar commons-logging-1.2.jar commons-io-2.6.jar commons-d 4 | bcp2-2.6.0.jar 5 | Main-Class: net.noyark.www.utils.JarEncode 6 | 7 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | classpath=target/classes/ 2 | encode.to=this -------------------------------------------------------------------------------- /src/test/java/net/noyark/foundbug/test/TestEncode.java: -------------------------------------------------------------------------------- 1 | package net.noyark.foundbug.test; 2 | 3 | public class TestEncode { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(111); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/test/java/net/noyark/foundbug/test/TestYaml.java: -------------------------------------------------------------------------------- 1 | package net.noyark.foundbug.test; 2 | 3 | import net.noyark.www.utils.ex.ParseException; 4 | import org.junit.Test; 5 | import org.yaml.snakeyaml.Yaml; 6 | 7 | import java.io.FileInputStream; 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | public class TestYaml { 14 | 15 | @Test 16 | public void testYamlParse(){ 17 | try{ 18 | Yaml yaml = new Yaml(); 19 | InputStream in = new FileInputStream("key.yml"); 20 | Map key = yaml.load(in); 21 | String key1 = key.get("key").toString(); 22 | System.out.println(key1); 23 | }catch (IOException e){ 24 | throw new ParseException("the yaml config is wrong",e); 25 | } 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /test.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLu550/edclass4j/6fde648becda5b00f7e07aff3e92e93bfbd2780f/test.jar -------------------------------------------------------------------------------- /testKey.key: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MagicLu550/edclass4j/6fde648becda5b00f7e07aff3e92e93bfbd2780f/testKey.key --------------------------------------------------------------------------------