REM:
1.增加AES256加密的单元测试
This commit is contained in:
chenlinghy 2020-09-18 16:42:34 +08:00
parent 22ec62482c
commit b522f17b18
1 changed files with 48 additions and 1 deletions

View File

@ -1,12 +1,17 @@
package com.dispose.test.dev.function; package com.dispose.test.dev.function;
import com.security.arithmetic.CryptoHelper; import com.security.arithmetic.CryptoHelper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import javax.crypto.BadPaddingException; import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException; import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException; 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.math.BigInteger;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@ -17,6 +22,7 @@ import java.security.NoSuchAlgorithmException;
* *
* @author <huangxin@cmhi.chinamoblie.com> * @author <huangxin@cmhi.chinamoblie.com>
*/ */
@Slf4j
public class CryptoHelperTest { public class CryptoHelperTest {
/** /**
* T 1 sha 256 test. * T 1 sha 256 test.
@ -86,10 +92,51 @@ public class CryptoHelperTest {
byte[] aesCode = CryptoHelper.aes256Encryption(srcTest.getBytes(StandardCharsets.UTF_8), key); byte[] aesCode = CryptoHelper.aes256Encryption(srcTest.getBytes(StandardCharsets.UTF_8), key);
String showText = CryptoHelper.base64Encryption(aesCode); String showText = CryptoHelper.base64Encryption(aesCode);
Assert.assertEquals(showText, "3sTXo4P2/pGQEfL9UJ/wRQ=="); Assert.assertEquals(showText, "3sTXo4P2/pGQEfL9UJ/wRQ==");
byte[] aesDecode = CryptoHelper.aes256Decryption(aesCode, key); byte[] aesDecode = CryptoHelper.aes256Decryption(aesCode, key);
Assert.assertEquals(new String(aesDecode), srcTest); 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));
}
} }