game-api/ff-base/src/main/java/com/ff/base/utils/DESEncryptUtils.java

49 lines
1.2 KiB
Java
Raw Normal View History

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("加密失败");
}
}
}