commit b57655bd0107fd0ae59a0ac44ecafaf6371f80b6 Author: ecpvint Date: Thu Aug 15 18:49:29 2024 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c684229 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/bin +/classes +/.idea +*.iml \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..f708af2 --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +## 影沐纪念项目 + +#### Java工具库 + +一些Java工具包括 `IO` , `Http` , `命令行` 等等 + +

Java项目

\ No newline at end of file diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..59499bc --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + diff --git a/src/net/droidtech/commands/OnCommandFinishedListener.java b/src/net/droidtech/commands/OnCommandFinishedListener.java new file mode 100644 index 0000000..371bb5d --- /dev/null +++ b/src/net/droidtech/commands/OnCommandFinishedListener.java @@ -0,0 +1,7 @@ +package net.droidtech.commands; + +public interface OnCommandFinishedListener { + + public void onFinished(String result); + +} diff --git a/src/net/droidtech/commands/Root.java b/src/net/droidtech/commands/Root.java new file mode 100644 index 0000000..e33177e --- /dev/null +++ b/src/net/droidtech/commands/Root.java @@ -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 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); + } +} diff --git a/src/net/droidtech/commands/Shell.java b/src/net/droidtech/commands/Shell.java new file mode 100644 index 0000000..8c7fb5b --- /dev/null +++ b/src/net/droidtech/commands/Shell.java @@ -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 envs=new HashMap(); + 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 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 keySet=(Set)envs.keySet(); + Iterator 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); + } + + } + } +} diff --git a/src/net/droidtech/httputils/DownloadListener.java b/src/net/droidtech/httputils/DownloadListener.java new file mode 100644 index 0000000..67e3b1a --- /dev/null +++ b/src/net/droidtech/httputils/DownloadListener.java @@ -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); + +} diff --git a/src/net/droidtech/httputils/HttpHeader.java b/src/net/droidtech/httputils/HttpHeader.java new file mode 100644 index 0000000..d439fdf --- /dev/null +++ b/src/net/droidtech/httputils/HttpHeader.java @@ -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; + } + +} diff --git a/src/net/droidtech/httputils/HttpUtils.java b/src/net/droidtech/httputils/HttpUtils.java new file mode 100644 index 0000000..f4dfcba --- /dev/null +++ b/src/net/droidtech/httputils/HttpUtils.java @@ -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=conn.getHeaderFields(); + Set headersSet=headers.keySet(); + ArrayList responseHeaders=new ArrayList(); + Iterator iterator=headersSet.iterator(); + + for(int i=0;i values=headers.get(currentItem); + + for(int i3=0;i3=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; + + } + +} diff --git a/src/net/droidtech/httputils/OnRequestFinishedListener.java b/src/net/droidtech/httputils/OnRequestFinishedListener.java new file mode 100644 index 0000000..f910ab1 --- /dev/null +++ b/src/net/droidtech/httputils/OnRequestFinishedListener.java @@ -0,0 +1,9 @@ +package net.droidtech.httputils; + +public interface OnRequestFinishedListener{ + + public void onFinished(Response result); + + public void onError(Exception e); + +} diff --git a/src/net/droidtech/httputils/PostParameter.java b/src/net/droidtech/httputils/PostParameter.java new file mode 100644 index 0000000..b0fdd0d --- /dev/null +++ b/src/net/droidtech/httputils/PostParameter.java @@ -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; + } +} diff --git a/src/net/droidtech/httputils/Response.java b/src/net/droidtech/httputils/Response.java new file mode 100644 index 0000000..3f0fc0b --- /dev/null +++ b/src/net/droidtech/httputils/Response.java @@ -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 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 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 getHeaders(){ + return headers; + } + + public String[] getHeaderValuesByKey(String key){ + ArrayList result=new ArrayList(); + for(int i=0;iheaders.size()||i<0){ + return null; + } + return headers.get(i).getValue(); + } + + public int getStatusCode(){ + return statusCode; + } + + public long getContentLength(){ + return contentLength; + } + +} diff --git a/src/net/droidtech/httputils/UA.java b/src/net/droidtech/httputils/UA.java new file mode 100644 index 0000000..77c5cbe --- /dev/null +++ b/src/net/droidtech/httputils/UA.java @@ -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"); + +} diff --git a/src/net/droidtech/io/DroidFile.java b/src/net/droidtech/io/DroidFile.java new file mode 100644 index 0000000..f220c67 --- /dev/null +++ b/src/net/droidtech/io/DroidFile.java @@ -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); + } +} diff --git a/src/net/droidtech/io/StreamReader.java b/src/net/droidtech/io/StreamReader.java new file mode 100644 index 0000000..9de3123 --- /dev/null +++ b/src/net/droidtech/io/StreamReader.java @@ -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; + } + } +} diff --git a/src/net/droidtech/io/StreamWriter.java b/src/net/droidtech/io/StreamWriter.java new file mode 100644 index 0000000..cc20853 --- /dev/null +++ b/src/net/droidtech/io/StreamWriter.java @@ -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()); + + } + } + +} diff --git a/src/net/droidtech/launcher/Main.java b/src/net/droidtech/launcher/Main.java new file mode 100644 index 0000000..4883ed5 --- /dev/null +++ b/src/net/droidtech/launcher/Main.java @@ -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())); + + } + +} diff --git a/src/net/droidtech/utils/AESUtils.java b/src/net/droidtech/utils/AESUtils.java new file mode 100644 index 0000000..848f133 --- /dev/null +++ b/src/net/droidtech/utils/AESUtils.java @@ -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; + } + + } + +} diff --git a/src/net/droidtech/utils/AGCT.java b/src/net/droidtech/utils/AGCT.java new file mode 100644 index 0000000..ab53f53 --- /dev/null +++ b/src/net/droidtech/utils/AGCT.java @@ -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>((((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= 0) { + out.write(buffer, 0, n); + } + } catch (IOException e) { + e.printStackTrace(); + return null; + } + return out.toByteArray(); + } + +} diff --git a/src/net/droidtech/utils/HexStringUtils.java b/src/net/droidtech/utils/HexStringUtils.java new file mode 100644 index 0000000..2875aa7 --- /dev/null +++ b/src/net/droidtech/utils/HexStringUtils.java @@ -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;i2||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