-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
727 additions
and
4 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
components/fluent-builtin/src/main/java/io/fluent/builtin/crypto/AesUtil.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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package io.fluent.builtin.crypto; | ||
|
||
|
||
import io.fluent.builtin.exception.EncryptException; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.IvParameterSpec; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* AES-128-CBC 算法 | ||
* iv 默认取key | ||
*/ | ||
public class AesUtil { | ||
|
||
/** | ||
* AES | ||
*/ | ||
static String ALGORITHM = "AES"; | ||
/** | ||
* AES 算法 | ||
*/ | ||
static String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding"; | ||
/** | ||
* 偏移量 | ||
*/ | ||
private static final int OFFSET = 16; | ||
|
||
|
||
/** | ||
* 加密 | ||
* | ||
* @param data 需要加密的内容 | ||
* @param key 加密密码 | ||
* @param iv 加密密码 | ||
* @return | ||
*/ | ||
public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) { | ||
try { | ||
if (key.length < OFFSET) { | ||
byte[] tmp = new byte[OFFSET]; | ||
System.arraycopy(key, 0, tmp, 0, key.length); | ||
key = tmp; | ||
} | ||
if (iv.length < 16) { | ||
byte[] tmp = new byte[16]; | ||
System.arraycopy(iv, 0, tmp, 0, iv.length); | ||
iv = tmp; | ||
} | ||
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM); | ||
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv, 0, OFFSET); | ||
Cipher cipher = Cipher.getInstance(AES_CBC_CIPHER); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec); | ||
return cipher.doFinal(data); | ||
} catch (Exception e) { | ||
throw new EncryptException("aes encrypt error:" + e.getMessage(), e); | ||
} | ||
} | ||
|
||
/** | ||
* 解密 | ||
* | ||
* @param data 待解密内容 | ||
* @param key 解密密钥 | ||
* @return | ||
*/ | ||
public static byte[] decrypt(byte[] data, byte[] key, byte[] iv) { | ||
try { | ||
if (key.length < 16) { | ||
byte[] tmp = new byte[16]; | ||
System.arraycopy(key, 0, tmp, 0, key.length); | ||
key = tmp; | ||
} | ||
if (iv.length < 16) { | ||
byte[] tmp = new byte[16]; | ||
System.arraycopy(iv, 0, tmp, 0, iv.length); | ||
iv = tmp; | ||
} | ||
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM); | ||
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv, 0, OFFSET); | ||
Cipher cipher = Cipher.getInstance(AES_CBC_CIPHER); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec); | ||
return cipher.doFinal(data); | ||
} catch (Exception e) { | ||
throw new EncryptException("ase decrypt error", e); | ||
} | ||
} | ||
|
||
/** | ||
* 加密 | ||
* <p> | ||
* * 定义 aes 加密的key | ||
* * 密钥 必须是16位, 自定义, | ||
* * 如果不是16位, 则会出现InvalidKeyException: Illegal key size | ||
* * 解决方案有两种: | ||
* * 需要安装Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files(可以在Oracle下载). | ||
* * .设置设置key的长度为16个字母和数字的字符窜(128 Bit/8=16字符)就不报错了。 | ||
* | ||
* @param data 需要加密的内容 | ||
* @param key 加密密码 | ||
* @return | ||
*/ | ||
public static String encrypt(String data, String key) { | ||
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); | ||
byte[] valueByte = encrypt(data.getBytes(StandardCharsets.UTF_8), keyBytes, keyBytes); | ||
return Base64.getEncoder().encodeToString(valueByte); | ||
} | ||
|
||
/** | ||
* 解密 | ||
* | ||
* @param data 待解密内容 base64 字符串 | ||
* @param key 解密密钥 | ||
* @return | ||
*/ | ||
public static String decrypt(String data, String key) { | ||
byte[] originalData = Base64.getDecoder().decode(data.getBytes()); | ||
byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); | ||
byte[] valueByte = decrypt(originalData, keyBytes, keyBytes); | ||
return new String(valueByte); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
components/fluent-builtin/src/main/java/io/fluent/builtin/crypto/DesUtil.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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.fluent.builtin.crypto; | ||
|
||
|
||
import io.fluent.builtin.exception.EncryptException; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.SecretKeySpec; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* des算法加解密 | ||
* DES/ECB/PKCS5Padding | ||
* | ||
*/ | ||
public class DesUtil { | ||
|
||
/** | ||
* AES | ||
*/ | ||
static final String ALGORITHM = "DES"; | ||
/** | ||
* AES 算法 | ||
*/ | ||
static final String DES_ECB_CIPHER = "DES/ECB/PKCS5Padding"; | ||
|
||
/** | ||
* 加密 | ||
* | ||
* @param data 需要加密的内容 | ||
* @param key 加密密码 | ||
* @return | ||
*/ | ||
public static byte[] encrypt(byte[] data, byte[] key) { | ||
try { | ||
//生成KEY | ||
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM); | ||
Cipher cipher = Cipher.getInstance(DES_ECB_CIPHER); | ||
cipher.init(Cipher.ENCRYPT_MODE, secretKey); | ||
return cipher.doFinal(data); | ||
} catch (Exception e) { | ||
throw new EncryptException("encrypt error", e); | ||
} | ||
} | ||
|
||
/** | ||
* 解密 | ||
* | ||
* @param data 待解密内容 | ||
* @param key 解密密钥 | ||
* @return | ||
*/ | ||
public static byte[] decrypt(byte[] data, byte[] key) { | ||
try { | ||
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM); | ||
Cipher cipher = Cipher.getInstance(DES_ECB_CIPHER); | ||
cipher.init(Cipher.DECRYPT_MODE, secretKey); | ||
return cipher.doFinal(data); | ||
} catch (Exception e) { | ||
throw new EncryptException("decrypt error", e); | ||
} | ||
} | ||
|
||
/** | ||
* 加密 | ||
* | ||
* @param data 需要加密的内容 | ||
* @param key 加密密码 | ||
* @return | ||
*/ | ||
public static String encrypt(String data, String key) { | ||
byte[] valueByte = encrypt(data.getBytes(StandardCharsets.UTF_8), key.getBytes(StandardCharsets.UTF_8)); | ||
return Base64.getEncoder().encodeToString(valueByte); | ||
} | ||
|
||
/** | ||
* 解密 | ||
* | ||
* @param data 待解密内容 base64 字符串 | ||
* @param key 解密密钥 | ||
* @return | ||
*/ | ||
public static String decrypt(String data, String key) { | ||
byte[] originalData = Base64.getDecoder().decode(data.getBytes()); | ||
byte[] valueByte = decrypt(originalData, key.getBytes(StandardCharsets.UTF_8)); | ||
return new String(valueByte); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
components/fluent-builtin/src/main/java/io/fluent/builtin/exception/EncryptException.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.fluent.builtin.exception; | ||
|
||
public class EncryptException extends SystemException { | ||
|
||
public EncryptException(String message, Throwable throwable) { | ||
super(message, throwable); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
components/fluent-builtin/src/main/java/io/fluent/builtin/exception/SystemException.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.fluent.builtin.exception; | ||
|
||
public class SystemException extends WrapMessageException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public SystemException(String message) { | ||
super(message); | ||
} | ||
|
||
public SystemException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...onents/fluent-builtin/src/main/java/io/fluent/builtin/exception/WrapMessageException.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.fluent.builtin.exception; | ||
|
||
/** | ||
* 只包装了 错误信息 的 {@link RuntimeException}. | ||
* 用于 {@link com.sprainkle.spring.cloud.advance.common.core.exception.assertion.Assert} 中用于包装自定义异常信息 | ||
* | ||
*/ | ||
public class WrapMessageException extends RuntimeException { | ||
|
||
public WrapMessageException(String message) { | ||
super(message); | ||
} | ||
|
||
public WrapMessageException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
components/fluent-builtin/src/main/java/io/fluent/builtin/meta/TargetType.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.fluent.builtin.meta; | ||
|
||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
public class TargetType<T> { | ||
|
||
public Type getType() { | ||
|
||
Type t = getClass().getGenericSuperclass(); | ||
if (!(t instanceof ParameterizedType)) { | ||
throw new IllegalStateException("Incorrect use of TypeToken: " + t); | ||
} | ||
ParameterizedType pt = (ParameterizedType) t; | ||
Type[] typeArgs = pt.getActualTypeArguments(); | ||
if (typeArgs.length > 0) { | ||
return typeArgs[0]; | ||
} | ||
throw new IllegalStateException("Incorrect use of TypeToken: " + t); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public Class<T> getClassType() { | ||
|
||
Type type = getType(); | ||
if (type instanceof ParameterizedType) { | ||
return (Class<T>) ((ParameterizedType) type).getRawType(); | ||
} else { | ||
return (Class<T>) type; | ||
} | ||
} | ||
} |
Oops, something went wrong.