Skip to content

Commit

Permalink
fix unicode aes des utils , update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhao.zhang 张忱昊 committed Sep 19, 2020
1 parent 86d7b39 commit 6bb93f6
Show file tree
Hide file tree
Showing 18 changed files with 614 additions and 77 deletions.
193 changes: 192 additions & 1 deletion README.md
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)
2 changes: 1 addition & 1 deletion src/main/java/com/rekent/tools/utils/crypt/HexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static String toHexString(byte[] arg0, boolean toLowerCase) {
* @param arg0
* @return
*/
public static byte[] decode(String arg0) {
public static byte[] toPlainText(String arg0) {
int m = 0, n = 0;
int byteLen = arg0.length() / 2;
byte[] ret = new byte[byteLen];
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/rekent/tools/utils/crypt/UnicodeUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rekent.tools.utils.crypt;

import java.util.Arrays;

/**
* Utils to Unicode encode/decode
*
Expand All @@ -14,10 +16,14 @@ public class UnicodeUtils {
* @param arg0
* @return
*/
public static String encode(String arg0) {
public static String toUnicode(String arg0) {
StringBuilder unicode = new StringBuilder();
for (char element : arg0.toCharArray()) {
unicode.append("\\u").append(Integer.toHexString(element));
String hex = Integer.toHexString(element);
while (hex.length()<4) {
hex = "0" + hex;
}
unicode.append("\\u").append(hex);
}
return unicode.toString();
}
Expand All @@ -28,9 +34,10 @@ public static String encode(String arg0) {
* @param arg0
* @return
*/
public static String decode(String arg0) {
public static String toPlainText(String arg0) {
StringBuilder normalBuilder = new StringBuilder();
String[] pureHexs = arg0.split("\\\\u");
pureHexs = Arrays.copyOfRange(pureHexs, 1, pureHexs.length);
for (String element : pureHexs) {
int charInteger = Integer.parseUnsignedInt(element, 16);
normalBuilder.append((char) charInteger);
Expand Down
97 changes: 79 additions & 18 deletions src/main/java/com/rekent/tools/utils/crypto/AESUtils.java
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;
}
}
Loading

0 comments on commit 6bb93f6

Please sign in to comment.