From cc88a8992288af244c723b57d29330806660a930 Mon Sep 17 00:00:00 2001 From: lu Date: Sun, 27 Mar 2016 17:15:56 +0800 Subject: [PATCH] first first --- build.gradle | 5 + freejava.iml | 31 +++++ src/main/java/com/miludeer/Milu.java | 7 + .../java/com/miludeer/basic/MEncrypt.java | 126 ++++++++++++++++++ src/main/java/com/miludeer/basic/MString.java | 40 ++++++ src/main/java/com/miludeer/basic/MTime.java | 66 +++++++++ 6 files changed, 275 insertions(+) create mode 100644 build.gradle create mode 100644 freejava.iml create mode 100644 src/main/java/com/miludeer/Milu.java create mode 100644 src/main/java/com/miludeer/basic/MEncrypt.java create mode 100644 src/main/java/com/miludeer/basic/MString.java create mode 100644 src/main/java/com/miludeer/basic/MTime.java diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..c152b19 --- /dev/null +++ b/build.gradle @@ -0,0 +1,5 @@ +apply plugin: 'java' + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) +} \ No newline at end of file diff --git a/freejava.iml b/freejava.iml new file mode 100644 index 0000000..fa202d0 --- /dev/null +++ b/freejava.iml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/miludeer/Milu.java b/src/main/java/com/miludeer/Milu.java new file mode 100644 index 0000000..615842f --- /dev/null +++ b/src/main/java/com/miludeer/Milu.java @@ -0,0 +1,7 @@ +package com.miludeer; + +public class Milu { + public static String Msg() { + return "Welcome to use Miludeer lib!"; + } +} diff --git a/src/main/java/com/miludeer/basic/MEncrypt.java b/src/main/java/com/miludeer/basic/MEncrypt.java new file mode 100644 index 0000000..96c8d87 --- /dev/null +++ b/src/main/java/com/miludeer/basic/MEncrypt.java @@ -0,0 +1,126 @@ +package com.miludeer.basic; + +import java.io.UnsupportedEncodingException; +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.KeyGenerator; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +/** + * Created by lu on 16/3/27. + */ +public class MEncrypt { + + /** + * 获得字符串的md5字符串,大写 + * @param s + * @return + */ + public final static String MD5(String s) { + char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; + try { + byte[] btInput = s.getBytes(); + // 获得MD5摘要算法的 MessageDigest 对象 + MessageDigest mdInst = MessageDigest.getInstance("MD5"); + // 使用指定的字节更新摘要 + mdInst.update(btInput); + // 获得密文 + byte[] md = mdInst.digest(); + // 把密文转换成十六进制的字符串形式 + int j = md.length; + char str[] = new char[j * 2]; + int k = 0; + for (int i = 0; i < j; i++) { + byte byte0 = md[i]; + str[k++] = hexDigits[byte0 >>> 4 & 0xf]; + str[k++] = hexDigits[byte0 & 0xf]; + } + return new String(str); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + /** + * 获得字符串的md5字符串,小写 + * @param s + * @return + */ + public final static String md5(String s) { + return MD5(s).toLowerCase(); + } + + /** + * aes加密 + * + * @param content 需要加密的内容 + * @param password 加密密码 + * @return + */ + public static byte[] AESencrypt(String content, String password) { + try { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + kgen.init(128, new SecureRandom(password.getBytes())); + SecretKey secretKey = kgen.generateKey(); + byte[] enCodeFormat = secretKey.getEncoded(); + SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 + byte[] byteContent = content.getBytes("utf-8"); + cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 + byte[] result = cipher.doFinal(byteContent); + return result; // 加密 + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } + return null; + } + + /** aes解密 + * @param content 待解密内容 + * @param password 解密密钥 + * @return + */ + public static byte[] AESdecrypt(byte[] content, String password) { + try { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + kgen.init(128, new SecureRandom(password.getBytes())); + SecretKey secretKey = kgen.generateKey(); + byte[] enCodeFormat = secretKey.getEncoded(); + SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 + cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 + byte[] result = cipher.doFinal(content); + return result; // 加密 + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (NoSuchPaddingException e) { + e.printStackTrace(); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (IllegalBlockSizeException e) { + e.printStackTrace(); + } catch (BadPaddingException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/src/main/java/com/miludeer/basic/MString.java b/src/main/java/com/miludeer/basic/MString.java new file mode 100644 index 0000000..9f6892d --- /dev/null +++ b/src/main/java/com/miludeer/basic/MString.java @@ -0,0 +1,40 @@ +package com.miludeer.basic; + +/** + * 字符串处理 + * Created by lu on 16/3/27. + */ +public class MString { + + /**将二进制转换成16进制样式字符串 + * @param buf + * @return + */ + public static String parseByte2HexStr(byte buf[]) { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < buf.length; i++) { + String hex = Integer.toHexString(buf[i] & 0xFF); + if (hex.length() == 1) { + hex = '0' + hex; + } + sb.append(hex.toUpperCase()); + } + return sb.toString(); + } + + /**将16进制转换为二进制byte[] 数组 + * @param hexStr + * @return + */ + public static byte[] parseHexStr2Byte(String hexStr) { + if (hexStr.length() < 1) + return null; + byte[] result = new byte[hexStr.length()/2]; + for (int i = 0;i< hexStr.length()/2; i++) { + int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16); + int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16); + result[i] = (byte) (high * 16 + low); + } + return result; + } +} diff --git a/src/main/java/com/miludeer/basic/MTime.java b/src/main/java/com/miludeer/basic/MTime.java new file mode 100644 index 0000000..c01a16a --- /dev/null +++ b/src/main/java/com/miludeer/basic/MTime.java @@ -0,0 +1,66 @@ +package com.miludeer.basic; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * 日期时间处理 + * Created by lu on 16/3/27. + */ +public class MTime { + private String commonFormat = "yyyy-MM-dd HH:mm:ss"; + private Date time; + + /** + * + * @param itime 时间的long值 + */ + public MTime(long itime) { + this.time = new Date(itime); + } + + /** + * + * @param date 时间的Date值 + */ + public MTime(Date date) { + this.time = date; + } + + /** + * + * @param strTime 时间字符串 + * @param format 时间格式 + */ + public MTime(String strTime, String format) { + SimpleDateFormat sdf = new SimpleDateFormat(format); + Date date = null; + try { + date = sdf.parse(strTime); + } catch (ParseException e) { + date = new Date(0); + } + this.time = date; + } + + + + public long getTime() { + return this.time.getTime(); + } + + public String getCommonTime() { + SimpleDateFormat sdf = new SimpleDateFormat(commonFormat); + return sdf.format(this.time); + } + + public String getSpecialTime(String format) { + SimpleDateFormat sdf = new SimpleDateFormat(format); + return sdf.format(this.time); + } + + public long timeDiffNow() { + return this.time.getTime() - new Date().getTime(); + } +}