From a2a247bbff8271e5d2bd6b423faec5cdbd77c463 Mon Sep 17 00:00:00 2001
From: yexuejc <940526mf>
Date: Mon, 3 Sep 2018 19:27:23 +0800
Subject: [PATCH] 1.1.7
---
UPDATE.md | 9 +
pom.xml | 13 +-
.../java/com/yexuejc/base/encrypt/RSA.java | 1 -
.../com/yexuejc/base/encrypt/RSACoder.java | 200 +++++++++
.../com/yexuejc/base/util/AlgorithmUtils.java | 45 ++
.../java/com/yexuejc/base/util/DateUtil.java | 10 +-
.../java/com/yexuejc/base/util/ImgUtil.java | 419 ++++++++++++++++++
.../java/com/yexuejc/base/util/JsonUtil.java | 8 +
.../java/com/yexuejc/base/util/JwtUtil.java | 66 ++-
.../yexuejc/base/util/MapRemoveNullUtil.java | 8 +
.../com/yexuejc/base/util/MoneyUtils.java | 2 +-
.../java/com/yexuejc/base/util/ThreeDES.java | 101 +++++
12 files changed, 869 insertions(+), 13 deletions(-)
create mode 100644 src/main/java/com/yexuejc/base/encrypt/RSACoder.java
create mode 100644 src/main/java/com/yexuejc/base/util/ImgUtil.java
create mode 100644 src/main/java/com/yexuejc/base/util/ThreeDES.java
diff --git a/UPDATE.md b/UPDATE.md
index 86a6207..5489860 100644
--- a/UPDATE.md
+++ b/UPDATE.md
@@ -1,6 +1,15 @@
yexuejc-base 更新记录
------------------
+#### version :1.1.8
+**time:2018-8-17 11:22:50**
+**branch:** master
+**update:**
+>1. 增肌图片处理工具类
+>2. 增肌3des工具类
+>3. 增肌RSA工具类
+>4. 优化其他工具类
+#
#### version :1.1.7
**time:2018-8-17 11:22:50**
**branch:** master
diff --git a/pom.xml b/pom.xml
index 1dbd45f..30f0df3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
+ * 用公钥解密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] decryptByPublic(String data, String key)
+ throws Exception {
+ return getDataByPublicKey(data, key, Cipher.DECRYPT_MODE);
+ }
+
+ /**
+ * 解密
+ * 用私钥解密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] decryptByPrivateKey(String data, String key)
+ throws Exception {
+ return getDataByPrivateKey(data, key, Cipher.DECRYPT_MODE);
+ }
+
+ /**
+ * 加密
+ * 用公钥加密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] encryptByPublicKey(String data, String key)
+ throws Exception {
+ return getDataByPublicKey(data, key, Cipher.ENCRYPT_MODE);
+ }
+
+ /**
+ * 加密
+ * 用私钥加密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] encryptByPrivateKey(String data, String key)
+ throws Exception {
+ return getDataByPrivateKey(data, key, Cipher.ENCRYPT_MODE);
+ }
+
+ /**
+ * 解密
+ * 用公钥解密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] decryptByPublic(byte[] data, String key)
+ throws Exception {
+ return getDataByPublicKey(data, key, Cipher.DECRYPT_MODE);
+ }
+
+ /**
+ * 解密
+ * 用私钥解密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] decryptByPrivateKey(byte[] data, String key)
+ throws Exception {
+ return getDataByPrivateKey(data, key, Cipher.DECRYPT_MODE);
+ }
+
+ /**
+ * 加密
+ * 用公钥加密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] encryptByPublicKey(byte[] data, String key)
+ throws Exception {
+ return getDataByPublicKey(data, key, Cipher.ENCRYPT_MODE);
+ }
+
+ /**
+ * 加密
+ * 用私钥加密
+ *
+ * @param data
+ * @param key
+ * @return
+ * @throws Exception
+ */
+ public static byte[] encryptByPrivateKey(byte[] data, String key)
+ throws Exception {
+ return getDataByPrivateKey(data, key, Cipher.ENCRYPT_MODE);
+ }
+
+ /**
+ * 通过公钥获得加解密数据
+ *
+ * @param data String
+ * @param key String
+ * @param mode int
+ * @return
+ */
+ public static byte[] getDataByPublicKey(String data, String key, int mode)
+ throws Exception {
+ return getDataByPublicKey(data.getBytes(), key, mode);
+ }
+
+ /**
+ * 通过私钥获得加解密数据
+ *
+ * @param data String
+ * @param key String
+ * @param mode 加密或解密
+ * @return
+ */
+ public static byte[] getDataByPrivateKey(String data, String key, int mode)
+ throws Exception {
+ return getDataByPrivateKey(data.getBytes(), key, mode);
+ }
+
+ /**
+ * 通过公钥获得加解密数据
+ *
+ * @param data String
+ * @param key String
+ * @param mode int
+ * @return
+ */
+ public static byte[] getDataByPublicKey(byte[] data, String key, int mode)
+ throws Exception {
+ // 取得私钥
+ X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key.getBytes("UTF-8")));
+ KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
+ Key privateKey = keyFactory.generatePublic(x509KeySpec);
+ // 对数据进行加密或解密
+ Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
+ cipher.init(mode, privateKey);
+ return cipher.doFinal(data);
+ }
+
+ /**
+ * 通过私钥获得加解密数据
+ *
+ * @param data String
+ * @param key String
+ * @param mode 加密或解密
+ * @return
+ */
+ public static byte[] getDataByPrivateKey(byte[] data, String key, int mode)
+ throws Exception {
+ // 取得私钥
+ PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key.getBytes("UTF-8")));
+ KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
+ Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
+ // 对数据解密
+ Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
+ cipher.init(mode, privateKey);
+ return cipher.doFinal(data);
+ }
+}
diff --git a/src/main/java/com/yexuejc/base/util/AlgorithmUtils.java b/src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
index 9daf60a..8917516 100644
--- a/src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
+++ b/src/main/java/com/yexuejc/base/util/AlgorithmUtils.java
@@ -1,6 +1,8 @@
package com.yexuejc.base.util;
+import sun.misc.BASE64Decoder;
+
/**
* 算法工具类
*
@@ -107,4 +109,47 @@ public class AlgorithmUtils {
static class NextCodeException extends Exception {
private static final long serialVersionUID = 8956943499144648985L;
}
+
+ /**
+ * 16进制转为2进制
+ *
+ * @param hexString 16进制字符串
+ */
+ public static String x16ConvertTo2(String hexString) {
+ if (hexString == null || hexString.length() % 2 != 0) {
+ return null;
+ }
+ String bString = "", tmp;
+ for (int i = 0; i < hexString.length(); i++) {
+ tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
+ bString += tmp.substring(tmp.length() - 4);
+ }
+ return bString;
+ }
+
+ /**
+ * 16进制转为2进制byte[]
+ *
+ * @param hexString 16进制字符串
+ * @return
+ */
+ public static byte[] hexStringToBytes(String hexString) {
+ if (hexString == null || hexString.equals("")) {
+ return null;
+ }
+ hexString = hexString.toUpperCase();
+ int length = hexString.length() / 2;
+ char[] hexChars = hexString.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ return d;
+ }
+
+ private static byte charToByte(char c) {
+ return (byte) "0123456789ABCDEF".indexOf(c);
+ }
+
}
diff --git a/src/main/java/com/yexuejc/base/util/DateUtil.java b/src/main/java/com/yexuejc/base/util/DateUtil.java
index b037fcf..e2016d2 100644
--- a/src/main/java/com/yexuejc/base/util/DateUtil.java
+++ b/src/main/java/com/yexuejc/base/util/DateUtil.java
@@ -6,7 +6,13 @@ import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
-
+/**
+ * java.util.Date 时间工具类
+ * @ClassName DateUtil
+ * @Description
+ * @author maxf
+ * @date 2018/9/3 15:27
+ */
public class DateUtil {
private DateUtil() {
}
@@ -71,7 +77,7 @@ public class DateUtil {
/**
* 日期字符串转date
*
- * @param date
+ * @param dateStr
* @return Date
* @throws ParseException
*/
diff --git a/src/main/java/com/yexuejc/base/util/ImgUtil.java b/src/main/java/com/yexuejc/base/util/ImgUtil.java
new file mode 100644
index 0000000..55d5448
--- /dev/null
+++ b/src/main/java/com/yexuejc/base/util/ImgUtil.java
@@ -0,0 +1,419 @@
+package com.yexuejc.base.util;
+
+import sun.misc.BASE64Decoder;
+import sun.misc.BASE64Encoder;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageReader;
+import javax.imageio.stream.ImageInputStream;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.Proxy;
+import java.net.URL;
+import java.util.Iterator;
+
+/**
+ * 图片处理工具类
+ *
+ * @author maxf
+ * @ClassName ImageUtil
+ * @Description
+ * @date 2018/9/3 15:25
+ */
+public class ImgUtil {
+
+ /**
+ * 将一张网络图片转化成Base64字符串
+ *
+ * @param imgURL 网络图片url
+ * @return String Base64字符串
+ */
+ public static String getImageStrFromUrl(String imgURL) {
+ ByteArrayOutputStream data = new ByteArrayOutputStream();
+ try {
+ byte[] by = new byte[1024];
+ InputStream is = getImageInputStreamFromUrl(null, imgURL);
+ // 将内容读取内存中
+ int len = -1;
+ while ((len = is.read(by)) != -1) {
+ data.write(by, 0, len);
+ }
+ // 关闭流
+ is.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ // 对字节数组Base64编码
+ BASE64Encoder encoder = new BASE64Encoder();
+ return encoder.encode(data.toByteArray());
+ }
+
+ /**
+ * 读取网络图片url为输入流
+ *
本方法未关闭io流,请自行关闭io流
+ *is.close();
+ * + * @param imgURL 网络图片url + * @return InputStream 输入流 + * @throws IOException + */ + public static InputStream getImageInputStreamFromUrl(Proxy proxy, String imgURL) throws IOException { + // 创建URL + URL url = new URL(imgURL); + // 创建链接 + HttpURLConnection conn; + if (null == proxy) { + conn = (HttpURLConnection) url.openConnection(); + } else { + // 如果有代理则通过代理下载 + conn = (HttpURLConnection) url.openConnection(proxy); + } + conn.setRequestMethod("GET"); + conn.setConnectTimeout(5000); + InputStream is = conn.getInputStream(); + return is; + } + + /** + * 读取网络图片url为输入流 + *本方法未关闭io流,请自行关闭io流
+ *is.close();
+ * + * @param imgURL + * @return + * @throws IOException + */ + public static InputStream getImageInputStreamFromUrl(String imgURL) throws IOException { + return getImageInputStreamFromUrl(null, imgURL); + } + + + /** + * 将图片文件流转换成字节数组 + *+ * 好处就是字节数组可以多次利用,而流一旦读取过一次之后就不能再使用了 + * + * @param proxy + * @param targetUrl + * @return + * @throws MalformedURLException + * @throws IOException + */ + public static byte[] getByteArrayFromInputStream(Proxy proxy, String targetUrl) + throws MalformedURLException, IOException { + InputStream is = getImageInputStreamFromUrl(proxy, targetUrl); + return getByteArray(is); + } + + /** + * 将图片文件流转换成字节数组 + *
+ * 好处就是字节数组可以多次利用,而流一旦读取过一次之后就不能再使用了 + * + * @param targetUrl + * @return + * @throws MalformedURLException + * @throws IOException + */ + public static byte[] getByteArrayFromInputStream(String targetUrl) + throws MalformedURLException, IOException { + return getByteArrayFromInputStream(null, targetUrl); + } + + /** + * 将图片文件流转换成字节数组 + * + * @return + * @throws MalformedURLException + * @throws IOException + */ + public static byte[] getByteArray(InputStream is) + throws MalformedURLException, IOException { + // 把文件写到字节数组保存起来 + ByteArrayOutputStream fos = getByteArrayFromInputStream(is); + byte[] bytes = fos.toByteArray(); + fos.close(); + return bytes; + } + + /** + * ByteArrayOutputStream转换成字节数组 + * + * @param fos + * @return + * @throws MalformedURLException + * @throws IOException + */ + public static byte[] getByteArray(ByteArrayOutputStream fos) + throws MalformedURLException, IOException { + // 把文件写到字节数组保存起来 + byte[] bytes = fos.toByteArray(); + fos.close(); + return bytes; + } + + /** + * 将图片文件流转换成ByteArrayOutputStream + *
本方法未关闭io流,请自行关闭io流
+ *is.close();
+ * + * @param is + * @return + * @throws IOException + */ + public static ByteArrayOutputStream getByteArrayFromInputStream(InputStream is) throws IOException { + // 把文件写到字节数组保存起来 + BufferedInputStream bis = new BufferedInputStream(is); + ByteArrayOutputStream fos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + while ((len = bis.read(buffer)) != -1) { + fos.write(buffer, 0, len); + } + bis.close(); + return fos; + } + + /** + * 如果你已经将图片文件流InputStream读取出来放到一个字节数组 + *
+ * 那么根据这个字节数组也是可以转换成对应的图片流,并再次获取图片基本信息
+ *
+ * @param imgBytes
+ * @return
+ */
+ public static ImageInfo getImageInfoFromInputStream(byte[] imgBytes) {
+ ByteArrayInputStream in = new ByteArrayInputStream(imgBytes);
+ ImageInfo image = getImageInfoFromInputStream(in);
+ return image;
+ }
+
+ /**
+ * 从图片文件流读取图片文件的基本信息
+ *
+ * @param inputStream
+ * @return
+ */
+ public static ImageInfo getImageInfoFromInputStream(InputStream inputStream) {
+ ImageInputStream imgStream = null;
+ try {
+ // 创建Image流
+ imgStream = ImageIO.createImageInputStream(inputStream);
+ Iterator