新增信息加密工具类

This commit is contained in:
椰子 2023-01-03 10:24:32 +08:00
parent 0b8bc52f99
commit 3aa978ae71
2 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,115 @@
package com.simaek.util;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.util.Base64;
/**
* 信息加密工具类使用PBEWithMD5AndDES加密算法可对有一定安全性要求的信息进行加密
* 安全性要求较高的信息请勿使用此工具进行加密
*
* @author Yaser Hsueh
*/
public final class EncryptUtil {
private EncryptUtil() {
throw new AssertionError("No com.cicdi.utils.EncryptUtil instances for you!");
}
/**
* 随机字符生成
*/
private static final SecureRandom RANDOM = new SecureRandom();
/**
* 使用的加密算法
*/
private static final String ALGORITHM = "PBEWithMD5AndDES";
/**
* 迭代次数
*/
private static final int ITERATIONS = 1000;
/**
* 盐值长度
*/
private static final int SALT_LENGTH = 8;
/**
* 使用PBEWithMD5AndDES算法对信息进行加密
*
* @param message 需要加密的信息
* @return 加密后的信息
*/
public static String encrypt(String message, String password) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, BadPaddingException, InvalidKeyException {
return Base64.getEncoder().encodeToString(encrypt(message.getBytes(StandardCharsets.UTF_8), password));
}
/**
* 对使用PBEWithMD5AndDES算法加密后的信息进行解密
*
* @param encryptedMessage 经过PBEWithMD5AndDES算法加密后的信息
* @return 解密后的信息
*/
public static String decrypt(String encryptedMessage, String password) throws InvalidAlgorithmParameterException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, IOException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException {
return new String(decrypt(Base64.getDecoder().decode(encryptedMessage), password), StandardCharsets.UTF_8);
}
private static byte[] encrypt(byte[] message, String password) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException {
// 创建Key
final SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
byte[] salt = generateSalt(SALT_LENGTH);
final PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, ITERATIONS);
SecretKey key = factory.generateSecret(keySpec);
// 构建Cipher.
final Cipher cipherEncrypt = Cipher.getInstance(ALGORITHM);
cipherEncrypt.init(Cipher.ENCRYPT_MODE, key);
// 保存参数
byte[] params = cipherEncrypt.getParameters().getEncoded();
// 加密信息
byte[] encryptedMessage = cipherEncrypt.doFinal(message);
return ByteBuffer
.allocate(1 + params.length + encryptedMessage.length)
.put((byte) params.length)
.put(params)
.put(encryptedMessage)
.array();
}
private static byte[] decrypt(byte[] encryptedMessage, String password) throws BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException, IOException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException {
int paramsLength = Byte.toUnsignedInt(encryptedMessage[0]);
int messageLength = encryptedMessage.length - paramsLength - 1;
byte[] params = new byte[paramsLength];
byte[] message = new byte[messageLength];
System.arraycopy(encryptedMessage, 1, params, 0, paramsLength);
System.arraycopy(encryptedMessage, paramsLength + 1, message, 0, messageLength);
// 创建Key
final SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
final PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKey key = factory.generateSecret(keySpec);
// 构建参数
AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(ALGORITHM);
algorithmParameters.init(params);
// 构建Cipher
final Cipher cipherDecrypt = Cipher.getInstance(ALGORITHM);
cipherDecrypt.init(Cipher.DECRYPT_MODE, key, algorithmParameters);
return cipherDecrypt.doFinal(message);
}
/**
* 生成指定长度的随机字节数组
*
* @param length 字节数组长度
* @return 字节数组
*/
private static byte[] generateSalt(int length) {
byte[] salt = new byte[length];
synchronized (RANDOM) {
RANDOM.nextBytes(salt);
return salt;
}
}
}

View File

@ -0,0 +1,33 @@
package com.simaek.util;
import org.junit.jupiter.api.Test;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
/**
* @author Yaser Hsueh
*/
class EncryptUtilTest {
@Test
void encrypt() throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, InvalidKeySpecException, IOException, BadPaddingException, InvalidKeyException {
String password = "jlhs@2020";
String message = "password";
System.out.println(EncryptUtil.encrypt(message, password));;
}
@Test
void decrypt() throws InvalidAlgorithmParameterException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, IOException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException {
String password = "jlhs@2020";
String encrypted = "DzANBAicaKn2c3D5oAIBCsKjPp8wXX1Ux4U/k0V0g657ShUc9L3uNA==";
// String encrypted = "DzANBAjG4uTtrfjHwgIBClPDEU6MjlN+953giRz6WbM=";
System.out.println(EncryptUtil.decrypt(encrypted, password));
}
}