49 lines
1.2 KiB
Java
49 lines
1.2 KiB
Java
|
|
package com.ff.base.utils;
|
||
|
|
|
||
|
|
import com.ff.base.exception.base.BaseException;
|
||
|
|
import lombok.Data;
|
||
|
|
|
||
|
|
import javax.crypto.Cipher;
|
||
|
|
import javax.crypto.spec.SecretKeySpec;
|
||
|
|
import java.nio.charset.StandardCharsets;
|
||
|
|
import java.util.Base64;
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 解密有用
|
||
|
|
*
|
||
|
|
* @author shi
|
||
|
|
* @date 2025/03/24
|
||
|
|
*/
|
||
|
|
public class DESEncryptUtils {
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 给…解密
|
||
|
|
*
|
||
|
|
* @param inString 字符串中
|
||
|
|
* @param secretKey 密钥
|
||
|
|
* @return {@link String }
|
||
|
|
* @throws Exception 例外
|
||
|
|
*/
|
||
|
|
public static String DESEncrypt(String inString, String secretKey) {
|
||
|
|
try {
|
||
|
|
// 使用相同的密钥进行加密
|
||
|
|
SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.US_ASCII), "DES");
|
||
|
|
|
||
|
|
// 使用DES加密算法
|
||
|
|
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
|
||
|
|
cipher.init(Cipher.ENCRYPT_MODE, key);
|
||
|
|
|
||
|
|
// 加密输入字符串
|
||
|
|
byte[] encrypted = cipher.doFinal(inString.getBytes(StandardCharsets.UTF_8));
|
||
|
|
|
||
|
|
// 返回Base64编码的密文
|
||
|
|
return Base64.getEncoder().encodeToString(encrypted);
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new BaseException("加密失败");
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|