init
This commit is contained in:
commit
b57655bd01
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/bin
|
||||
/classes
|
||||
/.idea
|
||||
*.iml
|
7
readme.md
Normal file
7
readme.md
Normal file
@ -0,0 +1,7 @@
|
||||
## 影沐纪念项目
|
||||
|
||||
#### Java工具库
|
||||
|
||||
一些Java工具包括 `IO` , `Http` , `命令行` 等等
|
||||
|
||||
<p>Java项目</p>
|
2
src/META-INF/MANIFEST.MF
Normal file
2
src/META-INF/MANIFEST.MF
Normal file
@ -0,0 +1,2 @@
|
||||
Manifest-Version: 1.0
|
||||
|
@ -0,0 +1,7 @@
|
||||
package net.droidtech.commands;
|
||||
|
||||
public interface OnCommandFinishedListener {
|
||||
|
||||
public void onFinished(String result);
|
||||
|
||||
}
|
41
src/net/droidtech/commands/Root.java
Normal file
41
src/net/droidtech/commands/Root.java
Normal file
@ -0,0 +1,41 @@
|
||||
package net.droidtech.commands;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Root {
|
||||
|
||||
private Shell shell=new Shell();
|
||||
|
||||
public void setenv(String key,String value){
|
||||
shell.setenv(key, value);
|
||||
}
|
||||
|
||||
public void setenvs(HashMap<String,String> envs){
|
||||
shell.setenvs(envs);
|
||||
}
|
||||
|
||||
public void setTimeOut(long timeOut){
|
||||
shell.setTimeOut(timeOut);
|
||||
}
|
||||
|
||||
public String exec(String command){
|
||||
return shell.exec("su -c \""+command+"\"");
|
||||
}
|
||||
|
||||
public boolean isRooted(){
|
||||
String result=exec("echo 888");
|
||||
if(result.indexOf("888")!=-1){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void execInThread(String command){
|
||||
shell.execInThread("su -c "+command);
|
||||
}
|
||||
|
||||
public void execInThread(String command,OnCommandFinishedListener listener){
|
||||
shell.execInThread("su -c "+command,listener);
|
||||
}
|
||||
}
|
146
src/net/droidtech/commands/Shell.java
Normal file
146
src/net/droidtech/commands/Shell.java
Normal file
@ -0,0 +1,146 @@
|
||||
package net.droidtech.commands;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import net.droidtech.io.StreamReader;
|
||||
|
||||
public class Shell {
|
||||
|
||||
private HashMap<String,String> envs=new HashMap<String,String>();
|
||||
private long timeOut=-1;
|
||||
|
||||
public void setTimeOut(long timeOut){
|
||||
this.timeOut=timeOut;
|
||||
}
|
||||
|
||||
public void setenv(String key,String value){
|
||||
envs.put(key,value);
|
||||
}
|
||||
|
||||
public void setenvs(HashMap<String,String> envs){
|
||||
this.envs=envs;
|
||||
}
|
||||
|
||||
|
||||
public String exec(String command){
|
||||
|
||||
ObserveThread obThread=null;
|
||||
|
||||
try{
|
||||
Process process = Runtime.getRuntime().exec("sh");
|
||||
DataOutputStream out=new DataOutputStream(process.getOutputStream());
|
||||
Set<String> keySet=(Set<String>)envs.keySet();
|
||||
Iterator<String> iterator=keySet.iterator();
|
||||
|
||||
for(;iterator.hasNext();){
|
||||
String key=iterator.next();
|
||||
out.write(("export "+key+"="+envs.get(key)+"\n").getBytes("UTF-8"));
|
||||
}
|
||||
|
||||
out.write((command+"\n").getBytes("UTF-8"));
|
||||
|
||||
if(timeOut>0){
|
||||
obThread=new ObserveThread(process,timeOut);
|
||||
obThread.start();
|
||||
}
|
||||
|
||||
out.writeBytes("exit\n");
|
||||
out.close();
|
||||
|
||||
byte[] input=null;
|
||||
DataInputStream in=new DataInputStream(process.getInputStream());
|
||||
|
||||
input=new StreamReader().readStream(in);
|
||||
|
||||
byte[] error=null;
|
||||
DataInputStream in1=new DataInputStream(process.getErrorStream());
|
||||
|
||||
error=new StreamReader().readStream(in1);
|
||||
|
||||
process.waitFor();
|
||||
|
||||
byte[] result=input;
|
||||
|
||||
if(input==null||input.length==0){
|
||||
result=error;
|
||||
}
|
||||
|
||||
if(obThread!=null){
|
||||
obThread.interrupt();
|
||||
}
|
||||
return new String(result,"UTF-8");
|
||||
|
||||
}catch (Exception e) {
|
||||
throw new IllegalStateException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void execInThread(String command){
|
||||
|
||||
execInThread(command,null);
|
||||
|
||||
}
|
||||
|
||||
public void execInThread(String command,OnCommandFinishedListener listener){
|
||||
|
||||
ExecThread thread=new ExecThread();
|
||||
|
||||
thread.prepare(command, listener);
|
||||
thread.start();
|
||||
|
||||
}
|
||||
|
||||
private class ObserveThread extends Thread{
|
||||
|
||||
private Process process=null;
|
||||
private long timeOut=0;
|
||||
|
||||
public ObserveThread(Process process,long timeOut){
|
||||
this.process=process;
|
||||
this.timeOut=timeOut;
|
||||
}
|
||||
|
||||
public void run(){
|
||||
|
||||
try {
|
||||
Thread.sleep(timeOut);
|
||||
process.destroy();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class ExecThread extends Thread{
|
||||
|
||||
private String command=null;
|
||||
private OnCommandFinishedListener listener=null;
|
||||
|
||||
public void prepare(String command,OnCommandFinishedListener listener){
|
||||
this.command=command;
|
||||
this.listener=listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(){
|
||||
|
||||
Shell shell=new Shell();
|
||||
|
||||
shell.setenvs(envs);
|
||||
shell.setTimeOut(timeOut);
|
||||
|
||||
if(listener!=null){
|
||||
listener.onFinished(shell.exec(command));
|
||||
}else{
|
||||
shell.exec(command);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
18
src/net/droidtech/httputils/DownloadListener.java
Normal file
18
src/net/droidtech/httputils/DownloadListener.java
Normal file
@ -0,0 +1,18 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
import net.droidtech.io.DroidFile;
|
||||
|
||||
public interface DownloadListener{
|
||||
|
||||
public static final int REASON_USER_CALLED=0x7F;
|
||||
public static final int REASON_EOF=0xFF;
|
||||
|
||||
public void onStart(DroidFile file,long contentLength);
|
||||
|
||||
public void onDownloadUpdate(int dataLen,byte[] data);
|
||||
|
||||
public void onDownloadAborted(int reason);
|
||||
|
||||
public void onDownloadError(RuntimeException error);
|
||||
|
||||
}
|
25
src/net/droidtech/httputils/HttpHeader.java
Normal file
25
src/net/droidtech/httputils/HttpHeader.java
Normal file
@ -0,0 +1,25 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
public class HttpHeader implements java.io.Serializable{
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String header=null;
|
||||
private String value=null;
|
||||
|
||||
public HttpHeader(String header,String value){
|
||||
this.header=header;
|
||||
this.value=value;
|
||||
}
|
||||
|
||||
public String getHeader(){
|
||||
return header;
|
||||
}
|
||||
|
||||
public String getValue(){
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
340
src/net/droidtech/httputils/HttpUtils.java
Normal file
340
src/net/droidtech/httputils/HttpUtils.java
Normal file
@ -0,0 +1,340 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import net.droidtech.io.StreamReader;
|
||||
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
private int timeout=0;
|
||||
private int readTimeout=0;
|
||||
private boolean isRedirect=true;
|
||||
private boolean checkSSL=false;
|
||||
|
||||
|
||||
public void setFollowRedirect(boolean status){
|
||||
this.isRedirect=status;
|
||||
}
|
||||
|
||||
public void setConnectTimeout(int timeout){
|
||||
this.timeout=timeout;
|
||||
}
|
||||
|
||||
public void setReadTimeout(int timeout){
|
||||
this.readTimeout=timeout;
|
||||
}
|
||||
|
||||
public void setSSLCheckerEnabled(boolean sslCheckStatus){
|
||||
this.checkSSL=sslCheckStatus;
|
||||
}
|
||||
|
||||
public static void passCheck(HttpsURLConnection connection){
|
||||
|
||||
TrustManager[] trustAllCerts = new TrustManager[]{
|
||||
// NopChecker
|
||||
new X509TrustManager() {
|
||||
public java.security.cert.X509Certificate[] getAcceptedIssuers(){
|
||||
return new java.security.cert.X509Certificate[]{};
|
||||
}
|
||||
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException{
|
||||
|
||||
}
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
try{
|
||||
|
||||
SSLContext sc = SSLContext.getInstance("TLS");
|
||||
sc.init(null, trustAllCerts, new java.security.SecureRandom());
|
||||
connection.setSSLSocketFactory(sc.getSocketFactory());
|
||||
|
||||
}catch(Exception e){
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static boolean isHttp(String URL){
|
||||
return URL.substring(0,URL.indexOf(":")).equals("http");
|
||||
}
|
||||
|
||||
public static boolean isHttps(String URL){
|
||||
return URL.substring(0,URL.indexOf(":")).equals("https");
|
||||
}
|
||||
|
||||
public static HttpURLConnection makeHttpConnection(String URL,String method){
|
||||
|
||||
try {
|
||||
|
||||
URL url=new URL(URL);
|
||||
|
||||
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
|
||||
connection.setRequestMethod(method);
|
||||
connection.setDoInput(true);
|
||||
connection.setDoOutput(true);
|
||||
|
||||
return connection;
|
||||
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void configureHttpConnection(HttpURLConnection conn){
|
||||
|
||||
conn.setInstanceFollowRedirects(isRedirect);
|
||||
conn.setConnectTimeout(timeout);
|
||||
conn.setReadTimeout(readTimeout);
|
||||
|
||||
if(isHttps(conn.getURL().toString())&&!this.checkSSL){
|
||||
passCheck((HttpsURLConnection)conn);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void putHeaders(HttpURLConnection conn, HttpHeader[] headers){
|
||||
|
||||
if(headers!=null&&headers.length!=0){
|
||||
|
||||
for(int i=0;i<headers.length;i++){
|
||||
if(headers[i]!=null){
|
||||
conn.setRequestProperty(headers[i].getHeader(),headers[i].getValue());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void addPostParameters(HttpURLConnection conn, PostParameter[] parameters){
|
||||
|
||||
if(parameters==null||parameters.length==0){
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
|
||||
DataOutputStream out=new DataOutputStream(conn.getOutputStream());
|
||||
out.writeBytes(parameters[0].getKey()+"="+parameters[0].getValue());
|
||||
for(int i=1;i<parameters.length;i++){
|
||||
out.writeBytes("&"+parameters[i].getKey()+"="+parameters[i].getValue());
|
||||
}
|
||||
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
throw new IllegalStateException("Cannot put post parameter.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void addPostParamaterBinary(HttpURLConnection conn, byte[] data){
|
||||
|
||||
try{
|
||||
|
||||
DataOutputStream out=new DataOutputStream(conn.getOutputStream());
|
||||
out.write(data);
|
||||
|
||||
}catch(Exception e){
|
||||
throw new IllegalStateException("Cannot put binary post parameter.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Response readResponse(HttpURLConnection conn){
|
||||
|
||||
try{
|
||||
|
||||
Response response=new Response();
|
||||
|
||||
Map<String,List<String>> headers=conn.getHeaderFields();
|
||||
Set<String> headersSet=headers.keySet();
|
||||
ArrayList<HttpHeader> responseHeaders=new ArrayList<HttpHeader>();
|
||||
Iterator<String> iterator=headersSet.iterator();
|
||||
|
||||
for(int i=0;i<headersSet.size();i++){
|
||||
|
||||
String currentItem=iterator.next();
|
||||
|
||||
List<String> values=headers.get(currentItem);
|
||||
|
||||
for(int i3=0;i3<values.size();i3++){
|
||||
responseHeaders.add(new HttpHeader(currentItem,values.get(i3)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
byte[] data=null;
|
||||
if(conn.getResponseCode()<400){
|
||||
data=new StreamReader().readStream(conn.getInputStream());
|
||||
}else if(conn.getResponseCode()>=400){
|
||||
data=new StreamReader().readStream(conn.getErrorStream());
|
||||
}
|
||||
|
||||
response.putContent(data);
|
||||
response.setStatusCode(conn.getResponseCode());
|
||||
response.addHeaders(responseHeaders);
|
||||
response.setContentLength(conn.getContentLengthLong());
|
||||
|
||||
return response;
|
||||
|
||||
}catch(Exception e){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Response get(String URL){
|
||||
return get(URL,null);
|
||||
}
|
||||
|
||||
public Response get(String URL,HttpHeader[] headers){
|
||||
|
||||
HttpURLConnection conn=HttpUtils.makeHttpConnection(URL, "GET");
|
||||
if(conn==null){
|
||||
throw new IllegalStateException("Cannot create http connection.");
|
||||
}
|
||||
|
||||
configureHttpConnection(conn);
|
||||
HttpUtils.putHeaders(conn, headers);
|
||||
|
||||
try{
|
||||
conn.connect();
|
||||
}catch(IOException e){
|
||||
throw new IllegalStateException("Cannot connect to server.");
|
||||
}
|
||||
|
||||
Response response=HttpUtils.readResponse(conn);
|
||||
if(response==null){
|
||||
conn.disconnect();
|
||||
throw new IllegalStateException("Error while reading response.");
|
||||
}
|
||||
conn.disconnect();
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
public Response getURLInfo(String URL){
|
||||
return getURLInfo(URL,null);
|
||||
}
|
||||
|
||||
public Response getURLInfo(String URL,HttpHeader[] headers){
|
||||
|
||||
HttpURLConnection conn=HttpUtils.makeHttpConnection(URL, "HEAD");
|
||||
if(conn==null){
|
||||
throw new IllegalStateException("Cannot create http connection.");
|
||||
}
|
||||
|
||||
configureHttpConnection(conn);
|
||||
HttpUtils.putHeaders(conn, headers);
|
||||
|
||||
try{
|
||||
conn.connect();
|
||||
}catch(IOException e){
|
||||
throw new IllegalStateException("Cannot connect to server.");
|
||||
}
|
||||
|
||||
Response response=HttpUtils.readResponse(conn);
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
if(response==null){
|
||||
throw new IllegalStateException("Error while reading response.");
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
public Response post(String URL,PostParameter[] parameters){
|
||||
return post(URL,parameters,null);
|
||||
}
|
||||
|
||||
public Response post(String URL,PostParameter[] parameters,HttpHeader[] headers){
|
||||
|
||||
HttpURLConnection conn=HttpUtils.makeHttpConnection(URL, "POST");
|
||||
if(conn==null){
|
||||
throw new IllegalStateException("Cannot create http connection.");
|
||||
}
|
||||
|
||||
configureHttpConnection(conn);
|
||||
HttpUtils.putHeaders(conn, headers);
|
||||
|
||||
try{
|
||||
conn.connect();
|
||||
}catch(IOException e){
|
||||
throw new IllegalStateException("Cannot connect to server.");
|
||||
}
|
||||
|
||||
HttpUtils.addPostParameters(conn, parameters);
|
||||
|
||||
Response response=HttpUtils.readResponse(conn);
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
if(response==null){
|
||||
throw new IllegalStateException("Error while reading response.");
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public Response postBinaryData(String URL,byte[] postData){
|
||||
return postBinaryData(URL,null,postData);
|
||||
}
|
||||
|
||||
|
||||
public Response postBinaryData(String URL,HttpHeader[] headers,byte[] data){
|
||||
|
||||
HttpURLConnection conn=HttpUtils.makeHttpConnection(URL, "POST");
|
||||
if(conn==null){
|
||||
throw new IllegalStateException("Cannot create http connection.");
|
||||
}
|
||||
|
||||
configureHttpConnection(conn);
|
||||
HttpUtils.putHeaders(conn, headers);
|
||||
|
||||
try{
|
||||
conn.connect();
|
||||
}catch(IOException e){
|
||||
throw new IllegalStateException("Cannot connect to server.");
|
||||
}
|
||||
|
||||
HttpUtils.addPostParamaterBinary(conn,data);
|
||||
|
||||
Response response=HttpUtils.readResponse(conn);
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
if(response==null){
|
||||
throw new IllegalStateException("Error while reading response.");
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
public interface OnRequestFinishedListener{
|
||||
|
||||
public void onFinished(Response result);
|
||||
|
||||
public void onError(Exception e);
|
||||
|
||||
}
|
64
src/net/droidtech/httputils/PostParameter.java
Normal file
64
src/net/droidtech/httputils/PostParameter.java
Normal file
@ -0,0 +1,64 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
|
||||
public class PostParameter {
|
||||
private String key=null;
|
||||
|
||||
private String value=null;
|
||||
|
||||
private String encoding="UTF-8";
|
||||
|
||||
private boolean encodingStatus=true;
|
||||
|
||||
|
||||
|
||||
public PostParameter(String key,String value){
|
||||
|
||||
if(key==null||value==null){
|
||||
return;
|
||||
}
|
||||
|
||||
this.key=key;
|
||||
this.value=value;
|
||||
|
||||
}
|
||||
|
||||
public void setEncoding(String encoding){
|
||||
|
||||
if(encoding==null){
|
||||
return;
|
||||
}
|
||||
|
||||
this.encoding=encoding;
|
||||
|
||||
}
|
||||
|
||||
public void setEncodingEnabled(boolean status){
|
||||
this.encodingStatus=status;
|
||||
}
|
||||
|
||||
public String getKey(){
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getValue(){
|
||||
try{
|
||||
return this.encodingStatus?URLEncoder.encode(this.encoding,this.value):value;
|
||||
}catch(Exception e){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getValueRaw(){
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public String getEncoding(){
|
||||
return encoding;
|
||||
}
|
||||
|
||||
public boolean getEncodingStatus(){
|
||||
return encodingStatus;
|
||||
}
|
||||
}
|
82
src/net/droidtech/httputils/Response.java
Normal file
82
src/net/droidtech/httputils/Response.java
Normal file
@ -0,0 +1,82 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Response{
|
||||
|
||||
private byte[] content=null;
|
||||
private ArrayList<HttpHeader> headers=null;
|
||||
private int statusCode=0;
|
||||
private long contentLength=0;
|
||||
|
||||
protected Response(){
|
||||
|
||||
}
|
||||
|
||||
protected void putContent(byte[] content){
|
||||
this.content=content;
|
||||
}
|
||||
|
||||
protected void setStatusCode(int statusCode){
|
||||
this.statusCode=statusCode;
|
||||
}
|
||||
|
||||
protected void setContentLength(long contentLength){
|
||||
this.contentLength=contentLength;
|
||||
}
|
||||
|
||||
protected void addHeaders(ArrayList<HttpHeader> headers){
|
||||
this.headers=headers;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return toString("UTF-8");
|
||||
}
|
||||
|
||||
public String toString(String encoding){
|
||||
try {
|
||||
return new String(content,encoding);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
throw new IllegalArgumentException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getBytes(){
|
||||
return content;
|
||||
}
|
||||
|
||||
public ArrayList<HttpHeader> getHeaders(){
|
||||
return headers;
|
||||
}
|
||||
|
||||
public String[] getHeaderValuesByKey(String key){
|
||||
ArrayList<String> result=new ArrayList<String>();
|
||||
for(int i=0;i<headers.size();i++){
|
||||
if(headers.get(i).getHeader()!=null){
|
||||
if(headers.get(i).getHeader().equals(key)){
|
||||
result.add(headers.get(i).getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
return result.toArray(new String[0]);
|
||||
}
|
||||
|
||||
public String getHeaderValueByIndex(int i){
|
||||
if(i>headers.size()||i<0){
|
||||
return null;
|
||||
}
|
||||
return headers.get(i).getValue();
|
||||
}
|
||||
|
||||
public int getStatusCode(){
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
public long getContentLength(){
|
||||
return contentLength;
|
||||
}
|
||||
|
||||
}
|
11
src/net/droidtech/httputils/UA.java
Normal file
11
src/net/droidtech/httputils/UA.java
Normal file
@ -0,0 +1,11 @@
|
||||
package net.droidtech.httputils;
|
||||
|
||||
public class UA {
|
||||
|
||||
public static final HttpHeader USER_AGENT_THE_WORLD=new HttpHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7");
|
||||
public static final HttpHeader USER_AGENT_CHROME=new HttpHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3596.0 Safari/537.36");
|
||||
public static final HttpHeader USER_AGENT_360SE=new HttpHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 QIHU 360SE");
|
||||
public static final HttpHeader USER_AGENT_FIREFOX=new HttpHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0");
|
||||
public static final HttpHeader USER_AGENT_IE=new HttpHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
|
||||
|
||||
}
|
119
src/net/droidtech/io/DroidFile.java
Normal file
119
src/net/droidtech/io/DroidFile.java
Normal file
@ -0,0 +1,119 @@
|
||||
package net.droidtech.io;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class DroidFile extends File{
|
||||
|
||||
private static final long serialVersionUID = 7311643678032690177L;
|
||||
private File file=null;
|
||||
|
||||
public DroidFile(String path){
|
||||
super(path);
|
||||
file=new File(path);
|
||||
}
|
||||
public DataInputStream getInputStream(){
|
||||
try {
|
||||
if(file.exists()&&!file.isDirectory()){
|
||||
DataInputStream in=new DataInputStream(new FileInputStream(file));
|
||||
return in;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public DataOutputStream getOutputStream(){
|
||||
try {
|
||||
if(file.exists()&&!file.isDirectory()){
|
||||
DataOutputStream out=new DataOutputStream(new FileOutputStream(file));
|
||||
return out;
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void copyTo(File target){
|
||||
DroidFile file=new DroidFile(target.getAbsolutePath());
|
||||
if(file.createNewFile()){
|
||||
StreamWriter writer=new StreamWriter();
|
||||
writer.writeStream(this.getInputStream(),file);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean createNewFile(){
|
||||
try {
|
||||
File parentDir=new File(file.getParent());
|
||||
if(!parentDir.exists()){
|
||||
parentDir.mkdirs();
|
||||
}
|
||||
file.createNewFile();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
return false;
|
||||
}
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean mkdir(){
|
||||
file.mkdirs();
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
public String getMD5String(){
|
||||
byte[] md5data=getMD5Bytes();
|
||||
if(md5data!=null){
|
||||
return new BigInteger(1,md5data).toString(16);
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getMD5Bytes(){
|
||||
DataInputStream in=this.getInputStream();
|
||||
if(in==null){
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
byte[] buffer=new byte[4096];
|
||||
MessageDigest md5=MessageDigest.getInstance("MD5");
|
||||
while(true){
|
||||
int count=in.read(buffer);
|
||||
if(count!=-1){
|
||||
md5.update(buffer,0,count);
|
||||
}else{
|
||||
in.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return md5.digest();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public DroidFile getChildFile(String name){
|
||||
if(!this.isDirectory()){
|
||||
return null;
|
||||
}
|
||||
return new DroidFile(this.getAbsolutePath()+File.separator+name);
|
||||
}
|
||||
}
|
44
src/net/droidtech/io/StreamReader.java
Normal file
44
src/net/droidtech/io/StreamReader.java
Normal file
@ -0,0 +1,44 @@
|
||||
package net.droidtech.io;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class StreamReader {
|
||||
|
||||
public String readStream(InputStream in,String encoding){
|
||||
try {
|
||||
return new String(readStream(in),encoding);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new IllegalArgumentException("Undefined encoding:"+encoding);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] readStream(InputStream in){
|
||||
|
||||
if(in==null){
|
||||
return null;
|
||||
}
|
||||
|
||||
ByteArrayOutputStream data=new ByteArrayOutputStream();
|
||||
byte[] buffer=new byte[4096];
|
||||
|
||||
try{
|
||||
while(true){
|
||||
int count=in.read(buffer);
|
||||
if(count!=-1){
|
||||
data.write(buffer,0,count);
|
||||
}else{
|
||||
in.close();
|
||||
data.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return data.toByteArray();
|
||||
}catch(IOException e){
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
48
src/net/droidtech/io/StreamWriter.java
Normal file
48
src/net/droidtech/io/StreamWriter.java
Normal file
@ -0,0 +1,48 @@
|
||||
package net.droidtech.io;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class StreamWriter {
|
||||
|
||||
public void writeStream(InputStream in,File outFile){
|
||||
writeStream(in,outFile,false);
|
||||
}
|
||||
|
||||
public void writeStream(InputStream in,File outFile,boolean append){
|
||||
|
||||
byte[] buffer=new byte[4096];
|
||||
|
||||
if(in==null||outFile==null){
|
||||
return;
|
||||
}
|
||||
|
||||
try{
|
||||
|
||||
DataOutputStream out=new DataOutputStream(new FileOutputStream(outFile,append));
|
||||
DataInputStream dataInput=new DataInputStream(in);
|
||||
|
||||
while(true){
|
||||
|
||||
int count=dataInput.read(buffer);
|
||||
if(count!=-1){
|
||||
out.write(buffer,0,count);
|
||||
}else{
|
||||
dataInput.close();
|
||||
out.close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}catch(IOException e){
|
||||
|
||||
throw new IllegalStateException(e.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
33
src/net/droidtech/launcher/Main.java
Normal file
33
src/net/droidtech/launcher/Main.java
Normal file
@ -0,0 +1,33 @@
|
||||
package net.droidtech.launcher;
|
||||
|
||||
import net.droidtech.httputils.HttpHeader;
|
||||
import net.droidtech.httputils.HttpUtils;
|
||||
import net.droidtech.httputils.PostParameter;
|
||||
import net.droidtech.io.DroidFile;
|
||||
import net.droidtech.io.StreamReader;
|
||||
import net.droidtech.utils.GZIP;
|
||||
import net.droidtech.utils.HexStringUtils;
|
||||
|
||||
import net.droidtech.httputils.Response;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Inet4Address;
|
||||
import java.nio.Buffer;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Base64;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Main{
|
||||
|
||||
|
||||
public static void main(String[] args){
|
||||
|
||||
HttpUtils utils=new HttpUtils();
|
||||
System.out.println(new String(utils.get("https://wtfismyip.com/text").getBytes()));
|
||||
|
||||
}
|
||||
|
||||
}
|
115
src/net/droidtech/utils/AESUtils.java
Normal file
115
src/net/droidtech/utils/AESUtils.java
Normal file
@ -0,0 +1,115 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
public class AESUtils {
|
||||
|
||||
public static final int MODE_ENCRYPT=Cipher.ENCRYPT_MODE;
|
||||
public static final int MODE_DECRYPT=Cipher.DECRYPT_MODE;
|
||||
|
||||
public static final int DEFAULT_KEY_LENGTH=256;
|
||||
|
||||
private SecretKeySpec keySpec;
|
||||
private IvParameterSpec IV;
|
||||
private Cipher cipher;
|
||||
|
||||
public AESUtils(){
|
||||
|
||||
this.IV=new IvParameterSpec(new byte[16]);
|
||||
|
||||
}
|
||||
|
||||
public void setIV(byte[] IV){
|
||||
|
||||
if(IV==null){
|
||||
return;
|
||||
}
|
||||
|
||||
if(IV.length<16){
|
||||
return;
|
||||
}
|
||||
|
||||
this.IV=new IvParameterSpec(IV);
|
||||
|
||||
}
|
||||
|
||||
public static byte[] generateRandomIV(){
|
||||
|
||||
byte[] random=new byte[16];
|
||||
new Random().nextBytes(random);
|
||||
return random;
|
||||
|
||||
}
|
||||
|
||||
public void setKey(byte[] key){
|
||||
|
||||
if(key==null){
|
||||
return;
|
||||
}
|
||||
|
||||
if(key.length<16){
|
||||
return;
|
||||
}
|
||||
|
||||
this.keySpec = new SecretKeySpec(key, "AES");
|
||||
|
||||
}
|
||||
|
||||
public static byte[] generateRandomKey(){
|
||||
|
||||
return AESUtils.generateRandomKey(256);
|
||||
|
||||
}
|
||||
|
||||
public static byte[] generateRandomKey(int length){
|
||||
|
||||
try {
|
||||
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
|
||||
keyGenerator.init(length);
|
||||
return keyGenerator.generateKey().getEncoded();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setMode(int mode){
|
||||
|
||||
try {
|
||||
this.cipher = Cipher.getInstance("AES/CBC/NoPadding");
|
||||
cipher.init(mode, this.keySpec, this.IV);
|
||||
} catch (Exception e) {
|
||||
throw new java.lang.RuntimeException(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] encrypt(byte[] data){
|
||||
|
||||
try {
|
||||
byte[] result=this.cipher.doFinal(data);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] decrypt(byte[] data){
|
||||
|
||||
try {
|
||||
byte[] result=this.cipher.doFinal(data);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
94
src/net/droidtech/utils/AGCT.java
Normal file
94
src/net/droidtech/utils/AGCT.java
Normal file
@ -0,0 +1,94 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
public class AGCT {
|
||||
|
||||
private static final String[] AGCT_MAPPING={"AG","GA","CT","TC","AC","CA","GT","TG","AT","TA","CG","GC","AA","CC","GG","TT"};
|
||||
|
||||
private static final int VARIABLE_SIZE=8;
|
||||
private static final int UNIT_SIZE=4;
|
||||
|
||||
private AGCT(){
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static String encode(byte[] data){
|
||||
|
||||
StringBuilder result=new StringBuilder();
|
||||
|
||||
if(data==null || data.length==0){
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
for(int i=0;i<data.length;i++){
|
||||
|
||||
for(int i3=0;i3<(AGCT.VARIABLE_SIZE/AGCT.UNIT_SIZE);i3++){
|
||||
|
||||
//TODO: please change this magic shit into a EASY TO READ EXPRESSION.
|
||||
//JUST NEED TO RIGHT-SHIFT 4 bits or do var & 0X0F
|
||||
byte tmp=(byte)(data[i]>>((((AGCT.VARIABLE_SIZE/AGCT.UNIT_SIZE)-1)-i3)*AGCT.UNIT_SIZE)&0x0F);
|
||||
|
||||
result.append(AGCT.AGCT_MAPPING[tmp]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
|
||||
}
|
||||
|
||||
public static byte[] decode(String code){
|
||||
|
||||
if(code==null || code.length()==0 || code.length()%2!=0){
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
int length=code.length()/2;
|
||||
|
||||
boolean isRounded=code.length()%4==0;
|
||||
|
||||
int result_cursor=0;
|
||||
|
||||
byte[] result=new byte[code.length()/4+(isRounded?0:1)];
|
||||
|
||||
for(int i=0;i<length;){
|
||||
|
||||
for(int i3=0;i3<((i<(length-1))||((i==length-1)&&isRounded)?(AGCT.VARIABLE_SIZE/AGCT.UNIT_SIZE):(AGCT.VARIABLE_SIZE/AGCT.UNIT_SIZE)-1);i3++){
|
||||
|
||||
byte tmp=0;
|
||||
|
||||
for(int i5=0;i5<AGCT_MAPPING.length;i5++){
|
||||
|
||||
String cp=code.substring((i*2),(i*2)+2);
|
||||
|
||||
if(cp.equals(AGCT.AGCT_MAPPING[i5])){
|
||||
|
||||
tmp=(byte)i5;
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
result[result_cursor]=(byte)(result[result_cursor]+((tmp<<(((AGCT.VARIABLE_SIZE/AGCT.UNIT_SIZE)-1)-i3)*AGCT.UNIT_SIZE)));
|
||||
|
||||
i++;
|
||||
|
||||
}
|
||||
|
||||
result_cursor++;
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
}
|
12
src/net/droidtech/utils/DRFStreamer.java
Normal file
12
src/net/droidtech/utils/DRFStreamer.java
Normal file
@ -0,0 +1,12 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
public class DRFStreamer {
|
||||
|
||||
static{
|
||||
System.loadLibrary("drfstreamer");
|
||||
}
|
||||
|
||||
public static native byte[] encode(byte[] data);
|
||||
public static native byte[] decode(byte[] data);
|
||||
|
||||
}
|
48
src/net/droidtech/utils/GZIP.java
Normal file
48
src/net/droidtech/utils/GZIP.java
Normal file
@ -0,0 +1,48 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
public class GZIP {
|
||||
|
||||
private GZIP(){
|
||||
|
||||
}
|
||||
public static byte[] compress(byte[] data) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
GZIPOutputStream gzip;
|
||||
try {
|
||||
gzip = new GZIPOutputStream(out);
|
||||
gzip.write(data);
|
||||
gzip.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] decompress(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
|
||||
try {
|
||||
GZIPInputStream ungzip = new GZIPInputStream(in);
|
||||
byte[] buffer = new byte[256];
|
||||
int n;
|
||||
while ((n = ungzip.read(buffer)) >= 0) {
|
||||
out.write(buffer, 0, n);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
}
|
87
src/net/droidtech/utils/HexStringUtils.java
Normal file
87
src/net/droidtech/utils/HexStringUtils.java
Normal file
@ -0,0 +1,87 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class HexStringUtils {
|
||||
|
||||
private HexStringUtils(){
|
||||
|
||||
}
|
||||
|
||||
public static byte[] parseHexString(String arg){
|
||||
|
||||
return parseHexString(arg,null);
|
||||
|
||||
}
|
||||
|
||||
public static byte[] parseHexString(String arg, String separator){
|
||||
|
||||
ByteArrayOutputStream result=new ByteArrayOutputStream();
|
||||
|
||||
if(separator==null){
|
||||
|
||||
if(arg.length()%2!=0){
|
||||
throw new IllegalArgumentException("Unsupported string format or incompletely");
|
||||
}
|
||||
|
||||
char[] tempStack=new char[2];
|
||||
int stackSize=0;
|
||||
|
||||
for(int i=0;i<arg.length();i++){
|
||||
|
||||
if(stackSize<2){
|
||||
tempStack[stackSize]=arg.charAt(i);
|
||||
stackSize++;
|
||||
}
|
||||
if(stackSize==2){
|
||||
result.write(Integer.parseInt(new String(tempStack), 16));
|
||||
stackSize=0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(separator!=null){
|
||||
|
||||
String[] items=arg.split(separator);
|
||||
for(int i=0;i<items.length;i++){
|
||||
if(items[i].length()>2||items[i].length()<=0){
|
||||
throw new IllegalArgumentException("Unsupported string format");
|
||||
}
|
||||
result.write(Integer.parseInt(items[i],16));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result.toByteArray();
|
||||
|
||||
}
|
||||
|
||||
public static String byteToHexString(byte[] value){
|
||||
|
||||
return byteToHexString(value,null);
|
||||
|
||||
}
|
||||
|
||||
public static String byteToHexString(byte[] value, String separator){
|
||||
|
||||
StringBuffer result=new StringBuffer();
|
||||
|
||||
for(int i=0;i<value.length;i++){
|
||||
|
||||
if((value[i]&0xFF)<=0xF){
|
||||
result.append("0");
|
||||
}
|
||||
|
||||
result.append(Integer.toHexString((value[i])&0xFF));
|
||||
|
||||
if(separator!=null&&i<(value.length-1)){
|
||||
result.append(separator);
|
||||
}
|
||||
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
}
|
32
src/net/droidtech/utils/MD5Util.java
Normal file
32
src/net/droidtech/utils/MD5Util.java
Normal file
@ -0,0 +1,32 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
public class MD5Util {
|
||||
|
||||
private MD5Util(){
|
||||
|
||||
}
|
||||
|
||||
public static String getMD5String(byte[] data){
|
||||
|
||||
return HexStringUtils.byteToHexString(MD5Util.getMD5(data));
|
||||
|
||||
}
|
||||
|
||||
public static byte[] getMD5(byte[] data){
|
||||
|
||||
try{
|
||||
|
||||
MessageDigest m = MessageDigest.getInstance("MD5");
|
||||
return m.digest(data);
|
||||
|
||||
}catch(Exception e){
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
83
src/net/droidtech/utils/RSAUtils.java
Normal file
83
src/net/droidtech/utils/RSAUtils.java
Normal file
@ -0,0 +1,83 @@
|
||||
package net.droidtech.utils;
|
||||
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
|
||||
public class RSAUtils {
|
||||
|
||||
private Cipher encryptCipher;
|
||||
private Cipher decryptCipher;
|
||||
|
||||
public RSAUtils(){
|
||||
|
||||
}
|
||||
|
||||
public static KeyPair genKeyPair(){
|
||||
return genKeyPair(1024);
|
||||
}
|
||||
|
||||
public static KeyPair genKeyPair(int keyLength){
|
||||
try {
|
||||
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
|
||||
keyPairGenerator.initialize(keyLength);
|
||||
return keyPairGenerator.generateKeyPair();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
// TODO Auto-generated catch block
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void setPublicKey(byte[] key){
|
||||
try {
|
||||
this.encryptCipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||
X509EncodedKeySpec keySpec=new X509EncodedKeySpec(key);
|
||||
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
|
||||
PublicKey keyInstance=keyFactory.generatePublic(keySpec);
|
||||
this.encryptCipher.init(Cipher.ENCRYPT_MODE,keyInstance);
|
||||
} catch (Exception e) {
|
||||
throw new java.lang.IllegalArgumentException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void setPrivateKey(byte[] key){
|
||||
try {
|
||||
this.decryptCipher = Cipher.getInstance("RSA/ECB/NoPadding");
|
||||
PKCS8EncodedKeySpec keySpec=new PKCS8EncodedKeySpec(key);
|
||||
KeyFactory keyFactory=KeyFactory.getInstance("RSA");
|
||||
PrivateKey keyInstance=keyFactory.generatePrivate(keySpec);
|
||||
this.decryptCipher.init(Cipher.DECRYPT_MODE,keyInstance);
|
||||
} catch (Exception e) {
|
||||
throw new java.lang.IllegalArgumentException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
//公钥加密
|
||||
public byte[] encrypt(byte[] content){
|
||||
try {
|
||||
return this.encryptCipher.doFinal(content);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//私钥解密
|
||||
public byte[] decrypt(byte[] content){
|
||||
try {
|
||||
return this.decryptCipher.doFinal(content);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user