Skip to content

Commit 815803e

Browse files
committed
Add Crypto Toolkit And add corresponding unit tests; Add log4j configuration file for testing;
1 parent 2468f61 commit 815803e

File tree

13 files changed

+1005
-0
lines changed

13 files changed

+1005
-0
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package net.lamgc.utils.encrypt;
2+
3+
import javax.crypto.*;
4+
import javax.crypto.spec.SecretKeySpec;
5+
import java.security.InvalidKeyException;
6+
import java.security.NoSuchAlgorithmException;
7+
import java.security.SecureRandom;
8+
9+
/**
10+
* AES对称加密工具类
11+
* @author LamGC
12+
*/
13+
public class AESEncrypt {
14+
15+
private static final String Algorithm = "AES";
16+
17+
/**
18+
* 根据密钥规则(可以是密钥)生成一个AES密钥对象
19+
* @param encodeRules 密钥规则,可以填入原始密码
20+
* @param keySize 随机源大小,默认128
21+
* @return AES密钥
22+
*/
23+
public static SecretKey getSecretKey(String encodeRules, int keySize){
24+
if(keySize <= 0){
25+
//设置一个初始值
26+
keySize = 128;
27+
}
28+
KeyGenerator keygen = null;
29+
try{
30+
keygen = KeyGenerator.getInstance(Algorithm);
31+
} catch (NoSuchAlgorithmException ignored) {
32+
//算法没问题,不会出现
33+
}
34+
//生成原始对称密钥
35+
assert keygen != null; //用于去除Null警告
36+
keygen.init(keySize, new SecureRandom(encodeRules.getBytes()));
37+
//将原始对称密钥转生成为AES密钥
38+
return BytesToSecretKey(keygen.generateKey().getEncoded());
39+
}
40+
41+
/**
42+
* 根据密钥规则(可以是密钥)生成一个AES密钥对象
43+
* @param encodeRules 密钥规则, 可以使用随机数据
44+
* @param keySize 随机源大小,默认128
45+
* @return AES密钥
46+
*/
47+
public static SecretKey getSecretKey(byte[] encodeRules, int keySize){
48+
if(keySize <= 0){
49+
//设置一个初始值
50+
keySize = 128;
51+
}
52+
KeyGenerator keygen = null;
53+
try{
54+
keygen = KeyGenerator.getInstance(Algorithm);
55+
} catch (NoSuchAlgorithmException ignored) {
56+
//算法没问题,不会出现
57+
}
58+
//生成原始对称密钥
59+
assert keygen != null; //用于去除Null警告
60+
keygen.init(keySize, new SecureRandom(encodeRules));
61+
//将原始对称密钥转生成为AES密钥
62+
return BytesToSecretKey(keygen.generateKey().getEncoded());
63+
}
64+
65+
static SecretKey BytesToSecretKey(byte[] keyEncode){
66+
return new SecretKeySpec(keyEncode, Algorithm);
67+
}
68+
69+
/**
70+
* 加密数据.
71+
* @param data 待加密的数据
72+
* @param key AES密钥对象
73+
* @return 加密后的数据
74+
* @throws NoSuchPaddingException 不支持的填充方式
75+
* @throws InvalidKeyException 无效密钥异常
76+
* @throws BadPaddingException 错误填充异常
77+
* @throws IllegalBlockSizeException 数据块错误(可能是数据不完整导致的)
78+
*/
79+
public static byte[] encrypt(byte[] data, SecretKey key) throws NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
80+
Cipher cipher = null;
81+
try {
82+
cipher = Cipher.getInstance(Algorithm);
83+
} catch (NoSuchAlgorithmException ignored) {
84+
//算法没问题,不会出现
85+
}
86+
//初始化为加密模式,导入密钥
87+
assert cipher != null; //用于去除Null警告
88+
//初始化密码器
89+
cipher.init(Cipher.ENCRYPT_MODE, key);
90+
//进行加密并返回数据
91+
return cipher.doFinal(data);
92+
}
93+
94+
/**
95+
* 解密数据.
96+
* @param data 待解密的数据
97+
* @param key AES密钥对象
98+
* @return 解密后的数据
99+
* @throws NoSuchPaddingException 不支持的填充方式
100+
* @throws InvalidKeyException 无效密钥异常
101+
* @throws BadPaddingException 错误填充异常
102+
* @throws IllegalBlockSizeException 数据块错误(可能是数据不完整导致的)
103+
*/
104+
public static byte[] decrypt(byte[] data, SecretKey key) throws NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
105+
Cipher cipher = null;
106+
try {
107+
cipher = Cipher.getInstance(Algorithm);
108+
} catch (NoSuchAlgorithmException ignored) {
109+
//算法没问题,不会出现
110+
}
111+
//初始化为加密模式,导入密钥
112+
assert cipher != null; //用于去除Null警告
113+
//初始化密码器
114+
cipher.init(Cipher.DECRYPT_MODE, key);
115+
//进行加密并返回数据
116+
return cipher.doFinal(data);
117+
}
118+
119+
}
120+
121+
122+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package net.lamgc.utils.encrypt;
2+
3+
import javax.crypto.KeyAgreement;
4+
import javax.crypto.SecretKey;
5+
import java.security.*;
6+
import java.security.spec.InvalidKeySpecException;
7+
import java.security.spec.X509EncodedKeySpec;
8+
9+
public class DiffieHellmanEncrypt {
10+
11+
static {
12+
System.setProperty("jdk.crypto.KeyAgreement.legacyKDF", "true");
13+
}
14+
15+
/**
16+
* 非对称加密密钥算法
17+
*/
18+
private static final String KEY_ALGORITHM = "DH";
19+
20+
public static final int DEFAULT_KEY_SIZE = 512;
21+
22+
private KeyPair keyPair;
23+
24+
25+
public DiffieHellmanEncrypt(){
26+
initKey(DEFAULT_KEY_SIZE);
27+
}
28+
29+
public DiffieHellmanEncrypt(int keySize){
30+
initKey(keySize);
31+
}
32+
33+
/**
34+
* 初始化己方密钥
35+
* @param keySize 密钥长度
36+
*/
37+
public void initKey(int keySize){
38+
//实例化密钥对生成器
39+
KeyPairGenerator keyPairGenerator;
40+
try {
41+
keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
42+
} catch (NoSuchAlgorithmException e) {
43+
throw new RuntimeException(e);
44+
}
45+
//初始化密钥对生成器
46+
keyPairGenerator.initialize(keySize);
47+
//生成密钥对
48+
this.keyPair = keyPairGenerator.generateKeyPair();
49+
}
50+
51+
/**
52+
* 获取己方公钥
53+
* @return 己方公钥对象
54+
*/
55+
public PublicKey getPublicKey(){
56+
return this.keyPair.getPublic();
57+
}
58+
59+
/**
60+
* 获得DH公共密钥
61+
* @param publicKey 对方公钥
62+
* @param algorithm 公共密钥所对应的加密算法
63+
* @return DH公共密钥
64+
* @throws InvalidKeyException 当对方公钥数据无效时抛出
65+
*/
66+
public SecretKey getSecretKey(byte[] publicKey, Algorithm algorithm) throws InvalidKeyException {
67+
try{
68+
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
69+
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
70+
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
71+
return getSecretKey(pubKey, algorithm);
72+
}catch(NoSuchAlgorithmException | InvalidKeySpecException e){
73+
throw new RuntimeException(e);
74+
}
75+
}
76+
77+
public SecretKey getSecretKey(PublicKey publicKey, Algorithm algorithm) throws InvalidKeyException {
78+
try{
79+
KeyAgreement keyAgree = KeyAgreement.getInstance(KEY_ALGORITHM);
80+
keyAgree.init(this.keyPair.getPrivate());
81+
keyAgree.doPhase(publicKey, true);
82+
return keyAgree.generateSecret(algorithm.algorithmName);
83+
}catch(NoSuchAlgorithmException e){
84+
throw new RuntimeException(e);
85+
}
86+
}
87+
88+
89+
public enum Algorithm{
90+
AES("AES"),
91+
RC2("RC2"),
92+
RC4("ARCFOUR"),
93+
Blowfish("Blowfish"),
94+
DES("DES"),
95+
DES_ede("DESede"),
96+
HmacMD5("HmacMD5"),
97+
HmacSHA1("HmacSHA1"),
98+
HmacSHA256("HmacSHA256"),
99+
HmacSHA384("HmacSHA384"),
100+
HmacSHA512("HmacSHA512"),
101+
;
102+
103+
public String algorithmName;
104+
105+
Algorithm(String algorithmName) {
106+
this.algorithmName = algorithmName;
107+
}
108+
}
109+
110+
111+
112+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.lamgc.utils.encrypt;
2+
3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
5+
6+
public class MessageDigestUtils {
7+
8+
public static byte[] encrypt(byte[] data, Algorithm algorithm){
9+
MessageDigest digest;
10+
try {
11+
digest = MessageDigest.getInstance(algorithm.algorithmName);
12+
digest.update(data);
13+
} catch (NoSuchAlgorithmException e) {
14+
throw new RuntimeException(e);
15+
}
16+
return digest.digest();
17+
}
18+
19+
public enum Algorithm{
20+
MD2("MD2"),
21+
MD5("MD5"),
22+
SHA1("SHA-1"),
23+
SHA256("SHA-256"),
24+
SHA384("SHA-384"),
25+
SHA512("SHA-512")
26+
;
27+
28+
Algorithm(String algorithmName){
29+
this.algorithmName = algorithmName;
30+
}
31+
32+
public String algorithmName;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)