-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix unicode aes des utils , update readme
- Loading branch information
chenhao.zhang 张忱昊
committed
Sep 19, 2020
1 parent
86d7b39
commit 6bb93f6
Showing
18 changed files
with
614 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,193 @@ | ||
# CommonUtils | ||
Collection of Java Utils | ||
- Base on `jdk 1.8` | ||
|
||
# Introduction | ||
|
||
## 常用工具类 | ||
|
||
### StringUtils | ||
|
||
``` | ||
字符串相关工具类:判空,判等 | ||
``` | ||
|
||
- boolean isBlank(CharSequence charSequence) | ||
- boolean isNotBlank(CharSequence charSequence) | ||
- boolean isAnyBank(CharSequence... charSequences) | ||
- boolean isAllBlank(CharSequence... charSequences) | ||
- boolean equals(CharSequence arg0, CharSequence arg1) | ||
|
||
### CollectionUtils | ||
|
||
``` | ||
集合相关工具类:判空 | ||
``` | ||
|
||
- boolean isEmpty(Collection<?> collection) | ||
- boolean isNotEmpty(Collection<?> collection) | ||
|
||
### DateUtils | ||
|
||
``` | ||
日期相关工具类:格式转换(字符串<->日期),特定日期获取(当前周开始结束日、当前月开始结束日) | ||
``` | ||
|
||
- String toStringByFormat(Date date, String format) | ||
- Date toDateByFormat(String date, String format) | ||
- Date changeDate(Date date, int field, int value) | ||
- Date startofthisWeek(Date date, int firstDayofWeek) | ||
- Date endofthisWeek(Date date, int firstDayofWeek) | ||
- Date startofthisMonth(Date date) | ||
- Date endOfMonth(Date date) | ||
|
||
### FileTypeUtils | ||
|
||
``` | ||
文件类型工具类:根据文件名称判断,根据文件字节流魔数判断,针对独立的Office:Docx/Xlxs判断(依赖 org.jdom) | ||
``` | ||
|
||
- String determineByName(String name) | ||
- String determineByMagicNum(byte[] fileBytes) | ||
- boolean isDocx(InputStream input) | ||
- boolean isXlsx(InputStream input) | ||
|
||
### FileUtils | ||
|
||
``` | ||
文件读写工具类:针对本地文件读写,获取文件名 | ||
``` | ||
|
||
- byte[] readLocalFiles(String path) | ||
- writeLocalFiles(byte[] fileBytes, String path, OpenOption option) | ||
- String getFileName(String fileName) | ||
|
||
### IOUtils | ||
|
||
``` | ||
字节流以及字节操作工具类:字节流 <-> 字节操作 | ||
``` | ||
|
||
- void safetyClose(Closeable closeable) | ||
- byte[] readStreamAsBytes(InputStream stream) | ||
- String readStreamAsString(InputStream stream, String charset) | ||
- String readByteArrayAsHex(byte[] byteArray, int limit) | ||
|
||
### RandomUtils | ||
|
||
``` | ||
随机生成工具类:生成随机字符串、生成随机数字 | ||
``` | ||
|
||
- String randomString(int length) | ||
- int randomNum(int min, int max) | ||
|
||
### JsonUtils | ||
|
||
``` | ||
Josn转换工具类:Json字符串 <-> Object,基于jackson-databind | ||
``` | ||
|
||
- String serializeObject(Object object) | ||
- T deserializeObject(String text, Class<? extends T> clazz) | ||
- T deserializeArray(String text, TypeReference<T> typeReference) | ||
|
||
## 编码工具类 | ||
|
||
### Base64Utils | ||
|
||
``` | ||
Base64工具类,支持普通编码以及URL编码,基于java.util.Base64 | ||
``` | ||
|
||
- String encode(String arg0) | ||
- String decode(String arg0) | ||
- String urlEncode(String arg0) | ||
- String urlDecode(String arg0) | ||
|
||
### HexUtils | ||
|
||
``` | ||
16进制转换工具类 | ||
``` | ||
|
||
- String toHexString(byte[] arg0) | ||
- String toHexString(byte[] arg0, boolean toLowerCase) | ||
- byte[] toPlainText(String arg0) | ||
|
||
### UnicodeUtils | ||
|
||
``` | ||
Unicode转换工具类 | ||
``` | ||
|
||
- String toUnicode(String arg0) | ||
- String toPlainText(String arg0) | ||
|
||
## 哈希工具类 | ||
|
||
### MD5Utils | ||
|
||
``` | ||
MD5 摘要计算工具类 | ||
``` | ||
|
||
- String toMD5(String arg0) | ||
- String toMD5(byte[] arg0) | ||
|
||
### SHA256Utils | ||
|
||
``` | ||
SHA256 摘要计算工具类 | ||
``` | ||
|
||
- String toSHA256(String arg0) | ||
- String toSHA256(byte[] arg0) | ||
|
||
### HmacMD5Utils | ||
|
||
``` | ||
HmacMD5 摘要计算工具类 | ||
``` | ||
|
||
- String toHmcMD5(String arg0, byte[] key) | ||
- String toHmacMD5(byte[] arg0, byte[] key) | ||
|
||
### HmacSHA256Utils | ||
|
||
``` | ||
HmacSHA256 摘要计算工具类 | ||
``` | ||
|
||
- String toHmacSHA256(String arg0, byte[] key) | ||
- String toHmacSHA256(byte[] arg0, byte[] key) | ||
|
||
## 加密工具类 | ||
|
||
### AESUtils | ||
|
||
``` | ||
AES 加密工具类支持设置工作模式以及填充模式 | ||
``` | ||
|
||
- String encrypt(String arg0, String mode, String padding, String key) | ||
- String decrypt(String arg0, String mode, String padding, String key) | ||
|
||
### DESUtils | ||
|
||
``` | ||
DES 加密工具类支持设置工作模式以及填充模式 | ||
``` | ||
|
||
- String encrypt(String arg0, String mode, String padding, String key) | ||
- String decrypt(String arg0, String mode, String padding, String key) | ||
|
||
### RSAUtils | ||
|
||
``` | ||
RSA 对称密钥加密工具类,支持公钥加解密以及私钥加解密 | ||
``` | ||
|
||
- byte[] decryptByPrivateKey(byte[] arg0, byte[] privateKey) | ||
- byte[] decryptByPublicKey(byte[] arg0, byte[] publicKey) | ||
- byte[] encryptByPrivateKey(byte[] arg0, byte[] privateKey) | ||
- byte[] encryptByPublicKey(byte[] arg0, byte[] publicKey) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 79 additions & 18 deletions
97
src/main/java/com/rekent/tools/utils/crypto/AESUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,83 @@ | ||
package com.rekent.tools.utils.crypto; | ||
|
||
import java.security.GeneralSecurityException; | ||
import java.security.InvalidKeyException; | ||
|
||
import javax.crypto.BadPaddingException; | ||
import javax.crypto.Cipher; | ||
import javax.crypto.IllegalBlockSizeException; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import com.rekent.tools.utils.crypt.HexUtils; | ||
|
||
/** | ||
* | ||
* Utils to AES Encrypt/Decrypt | ||
* | ||
* @author richard.zhang | ||
* | ||
*/ | ||
public class AESUtils { | ||
// private static Cipher cipher; | ||
// | ||
// static { | ||
// try { | ||
// cipher = Cipher.getInstance("AES"); | ||
// } catch (Exception e) { | ||
// throw new RuntimeException(e); | ||
// } | ||
// } | ||
// | ||
// public static String encode(String arg0, int opmode, String key) { | ||
// cipher.init(Cipher.DECRYPT_MODE, getSecretKey()); | ||
// | ||
// } | ||
// | ||
// public static String decode(String arg0, int opmode, String key) { | ||
// | ||
// } | ||
private static final String ALGORITHM = "AES"; | ||
private static Cipher cipher; | ||
|
||
/** | ||
* aes encrypt | ||
* return hex | ||
* | ||
* @param arg0 | ||
* @param mode | ||
* @param padding | ||
* @param key | ||
* @return | ||
* @throws GeneralSecurityException | ||
*/ | ||
public static String encrypt(String arg0, String mode, String padding, String key) throws GeneralSecurityException { | ||
byte[] encodeBytes = null; | ||
try { | ||
String algorithm = ALGORITHM.concat("/").concat(mode).concat("/").concat(padding); | ||
cipher = Cipher.getInstance(algorithm); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
try { | ||
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key)); | ||
encodeBytes = cipher.doFinal(arg0.getBytes()); | ||
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { | ||
throw e; | ||
} | ||
return HexUtils.toHexString(encodeBytes); | ||
} | ||
|
||
/** | ||
* aes decrypt auto hex decode arg0 | ||
* | ||
* @param arg0 | ||
* @param mode | ||
* @param padding | ||
* @param key | ||
* @return | ||
* @throws GeneralSecurityException | ||
*/ | ||
public static String decrypt(String arg0, String mode, String padding, String key) throws GeneralSecurityException { | ||
byte[] encodeBytes = null; | ||
try { | ||
String algorithm = ALGORITHM.concat("/").concat(mode).concat("/").concat(padding); | ||
cipher = Cipher.getInstance(algorithm); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
try { | ||
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key)); | ||
encodeBytes = cipher.doFinal(HexUtils.toPlainText(arg0)); | ||
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { | ||
throw e; | ||
} | ||
return new String(encodeBytes); | ||
} | ||
|
||
protected static SecretKeySpec getSecretKey(String key) { | ||
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); | ||
return secretKeySpec; | ||
} | ||
} |
Oops, something went wrong.