diff --git a/src/main/java/com/lhstack/utils/Aes.java b/src/main/java/com/lhstack/utils/Aes.java index 931c35a5bfb92828153bf45b586ef41a7c1a9c21..b0668f8248c892ad0a7c84c1c0ac4c9da0730b21 100644 --- a/src/main/java/com/lhstack/utils/Aes.java +++ b/src/main/java/com/lhstack/utils/Aes.java @@ -37,4 +37,18 @@ public class Aes { throw new RuntimeException(e); } } + + public static byte[] encrypt(String key,String iv,byte[] bytes){ + try{ + Cipher cipher = StringUtils.hasText(iv) ? Cipher.getInstance(CBC) : Cipher.getInstance(ECB); + if(StringUtils.hasText(iv)){ + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"), new IvParameterSpec(iv.getBytes())); + }else { + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES")); + } + return cipher.doFinal(bytes); + }catch(Exception e){ + throw new RuntimeException(e); + } + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9e5e1a62f62fb01eefe4a435e632b74f3fbf352f..0b219863671f3f24c39480b8a3b7e7ea5ea7b0d0 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,2 +1,3 @@ aes: - key: axiwlazlf1456175 \ No newline at end of file + key: axiwlazlf1456175 + iv: 114aqws431@#!%_+ \ No newline at end of file diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index 3502d800b609b048420764c60e1fd008f95282b3..8740837ead9844fe47f7a13d37670def60eb2aa0 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -114,7 +114,7 @@ closed: false, aesKey: "", aesIv: "", - encryptText: "", + encryptText: "9GVtgT5fzydU1O595W+0fIPRew4IFoRUUijxalwx/pHe8ONGN2qvIIwah9bC0oj7QrcdrGfxO9klqwlc7ntUOantY5vb65DJDolUmX/RyU4A5T9DRD5I3tqn2pmaK3EjsSsba+FScfeLq5HTGM258OW2X0ryyPp7iw2s/anGlzw=", theme: "default", output: "", isJson: false, diff --git a/src/test/java/com/lhstack/TemplateApplicationTests.java b/src/test/java/com/lhstack/TemplateApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..afaf56e9d5a855ef4ead88e1cea614a613de114f --- /dev/null +++ b/src/test/java/com/lhstack/TemplateApplicationTests.java @@ -0,0 +1,46 @@ +package com.lhstack; + + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.bouncycastle.util.encoders.Base64; +import org.bouncycastle.util.encoders.Hex; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.lhstack.utils.Aes; + +@SpringBootTest +@RunWith(SpringRunner.class) +public class TemplateApplicationTests { + @Value("${aes.key:}") + private String defaultAesKey; + + @Value("${aes.iv:}") + private String defaultAesIv; + + @Test + public void test() throws Exception{ + Map data = new HashMap<>(); + data.put("date", new Date()); + data.put("time", System.currentTimeMillis()); + data.put("msg", "hello world"); + List list = Arrays.asList(1,2,3,4,5,6,7); + data.put("msg", list); + data.put("class", TemplateApplicationTests.class); + ObjectMapper objectMapper = new ObjectMapper(); + byte[] jsonBytes = objectMapper.writeValueAsBytes(data); + byte[] bytes = Aes.encrypt(defaultAesKey, defaultAesIv, jsonBytes); + System.out.println(new String(Base64.encode(bytes),StandardCharsets.UTF_8)); + System.out.println(new String(Hex.encode(bytes),StandardCharsets.UTF_8)); + } +} diff --git a/target/classes/application.yml b/target/classes/application.yml index 9e5e1a62f62fb01eefe4a435e632b74f3fbf352f..0b219863671f3f24c39480b8a3b7e7ea5ea7b0d0 100644 --- a/target/classes/application.yml +++ b/target/classes/application.yml @@ -1,2 +1,3 @@ aes: - key: axiwlazlf1456175 \ No newline at end of file + key: axiwlazlf1456175 + iv: 114aqws431@#!%_+ \ No newline at end of file diff --git a/target/classes/com/lhstack/utils/Aes.class b/target/classes/com/lhstack/utils/Aes.class index ba0dd662b1530603f7bf4f93d68e62bc9e9853f7..16efe5d05439e138430fcc16d5032963f9d6f4ea 100644 Binary files a/target/classes/com/lhstack/utils/Aes.class and b/target/classes/com/lhstack/utils/Aes.class differ diff --git a/target/classes/static/index.html b/target/classes/static/index.html index 3502d800b609b048420764c60e1fd008f95282b3..8740837ead9844fe47f7a13d37670def60eb2aa0 100644 --- a/target/classes/static/index.html +++ b/target/classes/static/index.html @@ -114,7 +114,7 @@ closed: false, aesKey: "", aesIv: "", - encryptText: "", + encryptText: "9GVtgT5fzydU1O595W+0fIPRew4IFoRUUijxalwx/pHe8ONGN2qvIIwah9bC0oj7QrcdrGfxO9klqwlc7ntUOantY5vb65DJDolUmX/RyU4A5T9DRD5I3tqn2pmaK3EjsSsba+FScfeLq5HTGM258OW2X0ryyPp7iw2s/anGlzw=", theme: "default", output: "", isJson: false, diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 0000000000000000000000000000000000000000..e8b6d1d41af3159017d7836cea422a64bb38cec7 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1 @@ +/root/Java/src/test/java/com/lhstack/TemplateApplicationTests.java diff --git a/target/surefire-reports/TEST-com.lhstack.TemplateApplicationTests.xml b/target/surefire-reports/TEST-com.lhstack.TemplateApplicationTests.xml new file mode 100644 index 0000000000000000000000000000000000000000..518a83776f3d7b1ca22403fb3935a462c754641c --- /dev/null +++ b/target/surefire-reports/TEST-com.lhstack.TemplateApplicationTests.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/target/surefire-reports/com.lhstack.TemplateApplicationTests.txt b/target/surefire-reports/com.lhstack.TemplateApplicationTests.txt new file mode 100644 index 0000000000000000000000000000000000000000..8e813c994486ae75c34fe42e5ad52e8190b713ce --- /dev/null +++ b/target/surefire-reports/com.lhstack.TemplateApplicationTests.txt @@ -0,0 +1,4 @@ +------------------------------------------------------------------------------- +Test set: com.lhstack.TemplateApplicationTests +------------------------------------------------------------------------------- +Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.48 s - in com.lhstack.TemplateApplicationTests diff --git a/target/test-classes/com/lhstack/TemplateApplicationTests.class b/target/test-classes/com/lhstack/TemplateApplicationTests.class new file mode 100644 index 0000000000000000000000000000000000000000..b0ce793c8e1dde983c594f8b7b8a60edbd7bff4d Binary files /dev/null and b/target/test-classes/com/lhstack/TemplateApplicationTests.class differ