|
@@ -0,0 +1,68 @@
|
|
|
|
+package com.fdkankan.ucenter.util;
|
|
|
|
+
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import javax.crypto.Cipher;
|
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
+import java.util.Base64;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 基于AES的加解密util
|
|
|
|
+ *
|
|
|
|
+ * @author wangzhonghai@autohome.com.cn
|
|
|
|
+ * @since 2023-02-09
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class QczjAesUtil {
|
|
|
|
+
|
|
|
|
+ @Value("${encryrt.key}")
|
|
|
|
+ private String ENCRYPT_KEY;
|
|
|
|
+
|
|
|
|
+ private Base64.Decoder DECODER = Base64.getDecoder();
|
|
|
|
+ private Base64.Encoder ENCODER = Base64.getEncoder();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String encrypt(String content) {
|
|
|
|
+ try {
|
|
|
|
+ byte[] raw = ENCRYPT_KEY.getBytes(StandardCharsets.UTF_8);
|
|
|
|
+ SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
|
|
|
|
+ byte[] byteEncode = content.getBytes(StandardCharsets.UTF_8);
|
|
|
|
+ byte[] byteAes = cipher.doFinal(byteEncode, 0, byteEncode.length);
|
|
|
|
+
|
|
|
|
+ return ENCODER.encodeToString(byteAes);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("[encrypt].err", e);
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public String decrypt(String content) {
|
|
|
|
+ try {
|
|
|
|
+ byte[] raw = ENCRYPT_KEY.getBytes(StandardCharsets.UTF_8);
|
|
|
|
+ SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
|
|
|
|
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
|
|
|
+ cipher.init(Cipher.DECRYPT_MODE, skeySpec);
|
|
|
|
+ byte[] encrypted1 = DECODER.decode(content);
|
|
|
|
+ byte[] original = cipher.doFinal(encrypted1);
|
|
|
|
+
|
|
|
|
+ return new String(original, StandardCharsets.UTF_8);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("[decrypt].err", e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ QczjAesUtil qczjAesUtil = new QczjAesUtil();
|
|
|
|
+ String decrypt = qczjAesUtil.encrypt("15819461282");
|
|
|
|
+ System.out.println(decrypt);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|