Commit 0868fc62 by zhangxingmin

push

parent 156bacf5
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectStorage">
<option name="path" value="项目群组-&gt;银盾-微服务" />
<option name="projectId" value="4cc05386e898000" />
</component>
</project>
\ No newline at end of file
package com.yd.notice.service.utils; //package com.yd.notice.service.utils;
//
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; //import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; //import org.springframework.stereotype.Component;
import javax.crypto.Cipher; //import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec; //import javax.crypto.spec.SecretKeySpec;
import java.util.Base64; //import java.util.Base64;
//
@Slf4j //@Slf4j
@Component //@Component
public class AesEncryptUtil { //public class AesEncryptUtil {
private static final String ALGORITHM = "AES"; // private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; // private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
//
@Value("${notice.aes.secret-key}") // @Value("${notice.aes.secret-key}")
private String secretKey; // 从配置文件注入,建议使用16/24/32字节的密钥 // private String secretKey; // 从配置文件注入,建议使用16/24/32字节的密钥
//
public String encrypt(String plainText) { // public String encrypt(String plainText) {
try { // try {
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM); // SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION); // Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, keySpec); // cipher.init(Cipher.ENCRYPT_MODE, keySpec);
byte[] encrypted = cipher.doFinal(plainText.getBytes()); // byte[] encrypted = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encrypted); // return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) { // } catch (Exception e) {
log.error("加密失败", e); // log.error("加密失败", e);
throw new RuntimeException("加密失败", e); // throw new RuntimeException("加密失败", e);
} // }
} // }
//
public String decrypt(String cipherText) { // public String decrypt(String cipherText) {
try { // try {
SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM); // SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION); // Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, keySpec); // cipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decoded = Base64.getDecoder().decode(cipherText); // byte[] decoded = Base64.getDecoder().decode(cipherText);
byte[] decrypted = cipher.doFinal(decoded); // byte[] decrypted = cipher.doFinal(decoded);
return new String(decrypted); // return new String(decrypted);
} catch (Exception e) { // } catch (Exception e) {
log.error("解密失败", e); // log.error("解密失败", e);
throw new RuntimeException("解密失败", e); // throw new RuntimeException("解密失败", e);
} // }
} // }
} //}
\ No newline at end of file \ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment