[test] AES单元测试

This commit is contained in:
its 2024-04-26 13:14:17 +08:00
parent cf8231017d
commit 0403c7c693

View File

@ -0,0 +1,55 @@
package com.yexuejc.base.encrypt;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class AESTest {
@Test
public void testEncrypt() throws Exception {
String data = "Hello World!";
AES aes = AES.builder()
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
.setKey("hj7x89H$yuBI0456")
.setIv("NIfb&95GUY86Gfgh")
.setCharset(StandardCharsets.UTF_8);
String encrypted = aes.encrypt(data);
assertNotNull(encrypted);
assertFalse(encrypted.isEmpty());
}
@Test
public void testDecrypt() throws Exception {
String data = "SGVsbG8gV29ybGQh";
AES aes = AES.builder()
.setAlgorithm(AES.ALGORITHM.AES_CBC_PKCS5Padding)
.setKey("hj7x89H$yuBI0456")
.setIv("NIfb&95GUY86Gfgh")
.setCharset(StandardCharsets.UTF_8);
String decrypted = aes.decrypt(data);
assertNotNull(decrypted);
assertFalse(decrypted.isEmpty());
assertEquals("Hello World!", decrypted);
}
@Test
public void testEncryptAndDecrypt() throws Exception {
String data = "张三";
AES aes = AES.builder()
.setAlgorithm(AES.ALGORITHM.AES_OFB_ISO10126Padding)
.setKey("hj7x89H$yuBI0456")
.setIv("NIfb&95GUY86Gfgh")
.setCharset(StandardCharsets.UTF_8);
String encrypt = aes.encrypt(data);
System.out.println("加密:" + encrypt);
String decrypt = aes.decrypt(encrypt);
System.out.println("解密:" + decrypt);
}
}