org.kaizen4j.common.encrypt.DES Maven / Gradle / Ivy
package org.kaizen4j.common.encrypt;
import com.google.common.base.Throwables;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Objects;
/**
* DES 加密类
*
* @author John
*/
public final class DES {
private final static String ALGORITHM = "DES";
private DES() {}
/**
* 根据键值进行加密
*
* @param data 待加密数据字符串
* @param key 加密键字符串
* @return 加密后的字符串
*/
public static String encrypt(String data, String key) {
byte[] binaryData = encrypt(data.getBytes(), key.getBytes());
return Base64.encodeBase64String(binaryData);
}
/**
* 根据键值进行解密
*
* @param data 待解密数据字符串
* @param key 加密键字符串
* @return 解密后的字符串
*/
public static String decrypt(String data, String key) {
if (Objects.isNull(data)) {
return data;
}
byte[] binaryData = Base64.decodeBase64(data);
binaryData = decrypt(binaryData, key.getBytes());
return new String(binaryData);
}
/**
* 根据键值进行加密
*
* @param data 待加密数据字节数组
* @param key 加密键字节数组
* @return 加密后的字节数组
*
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private static byte[] encrypt(byte[] data, byte[] key) {
try {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
| IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException(e);
}
}
/**
* 根据键值进行解密
*
* @param data 待解密数据字节数组
* @param key 解密键字节数组
* @return 解密后的字节数组
*
* @throws InvalidKeyException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchPaddingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private static byte[] decrypt(byte[] data, byte[] key) {
try {
// 生成一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密钥数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
} catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException
| IllegalBlockSizeException | BadPaddingException e) {
throw new RuntimeException(e);
}
}
}