From b522f17b18bf287672c822c372095faed53391e3 Mon Sep 17 00:00:00 2001 From: chenlinghy Date: Fri, 18 Sep 2020 16:42:34 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=E5=A2=9E=E5=8A=A0AES256?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/dev/function/CryptoHelperTest.java | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java b/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java index fa8db32b..4528a775 100644 --- a/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java +++ b/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java @@ -1,12 +1,17 @@ package com.dispose.test.dev.function; import com.security.arithmetic.CryptoHelper; +import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.Test; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.math.BigInteger; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; @@ -17,6 +22,7 @@ import java.security.NoSuchAlgorithmException; * * @author */ +@Slf4j public class CryptoHelperTest { /** * T 1 sha 256 test. @@ -86,10 +92,51 @@ public class CryptoHelperTest { byte[] aesCode = CryptoHelper.aes256Encryption(srcTest.getBytes(StandardCharsets.UTF_8), key); String showText = CryptoHelper.base64Encryption(aesCode); - Assert.assertEquals(showText, "3sTXo4P2/pGQEfL9UJ/wRQ=="); byte[] aesDecode = CryptoHelper.aes256Decryption(aesCode, key); Assert.assertEquals(new String(aesDecode), srcTest); } + + @Test + public void t5_aes256EncryptionTest() throws IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { + InputStream is = ClassLoader.getSystemResourceAsStream("git.properties"); + assert is != null; + String password = ""; + String testEncValue = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40"; + String testEncValue1 = "P3mq9iSIvQcvfyfdWR8sAnfAadO"; + String testEncValue2 = "h0K0_8u"; + + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + log.info("Version Information:"); + while (true) { + String val = reader.readLine(); + log.info("{}", val); + + if (val == null) { + break; + } + + if (val.startsWith("git.commit.id=")) { + password = val.substring("git.commit.id=".length()); + } + } + + password = password + "cmcc@10086!"; + + byte[] encode = CryptoHelper.aes256Encryption(testEncValue.getBytes(StandardCharsets.UTF_8), password); + Assert.assertEquals(CryptoHelper.base64Encryption(encode), "Trf2LEETes3oKnY1CF7LINcm2KlJbJxHIyvERz2174CTzQEhJtuo+PnO+fR3eDf+"); + log.info("Encrypt with key {}: {} --> {}", password, testEncValue, + CryptoHelper.base64Encryption(encode)); + + encode = CryptoHelper.aes256Encryption(testEncValue1.getBytes(StandardCharsets.UTF_8), password); + Assert.assertEquals(CryptoHelper.base64Encryption(encode), "JPYbpchhllvf6M+uolBFYOgM2fSyqGChRcnzoOCt6WM="); + log.info("Encrypt with key {}: {} --> {}", password, testEncValue1, + CryptoHelper.base64Encryption(encode)); + + encode = CryptoHelper.aes256Encryption(testEncValue2.getBytes(StandardCharsets.UTF_8), password); + Assert.assertEquals(CryptoHelper.base64Encryption(encode), "eBlCdflAlcnta81xW9f86A=="); + log.info("Encrypt with key {}: {} --> {}", password, testEncValue2, + CryptoHelper.base64Encryption(encode)); + } }