use of cn.hutool.crypto.symmetric.AES in project hutool by looly.
the class SymmetricTest method aesTest2.
@Test
public void aesTest2() {
String content = "test中文";
// 随机生成密钥
byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
// 构建
AES aes = SecureUtil.aes(key);
// 加密
byte[] encrypt = aes.encrypt(content);
// 解密
byte[] decrypt = aes.decrypt(encrypt);
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
// 加密为16进制表示
String encryptHex = aes.encryptHex(content);
// 解密为字符串
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(content, decryptStr);
}
use of cn.hutool.crypto.symmetric.AES in project hutool by looly.
the class SymmetricTest method aesTest3.
@Test
public void aesTest3() {
String content = "test中文aaaaaaaaaaaaaaaaaaaaa";
AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jud".getBytes(), "0102030405060708".getBytes());
// 加密
byte[] encrypt = aes.encrypt(content);
// 解密
byte[] decrypt = aes.decrypt(encrypt);
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
// 加密为16进制表示
String encryptHex = aes.encryptHex(content);
// 解密为字符串
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(content, decryptStr);
}
use of cn.hutool.crypto.symmetric.AES in project hutool by looly.
the class SymmetricTest method aesTest2.
@Test
public void aesTest2() {
String content = "test中文";
// 随机生成密钥
byte[] key = KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
// 构建
AES aes = SecureUtil.aes(key);
// 加密
byte[] encrypt = aes.encrypt(content);
// 解密
byte[] decrypt = aes.decrypt(encrypt);
Assert.assertEquals(content, StrUtil.utf8Str(decrypt));
// 加密为16进制表示
String encryptHex = aes.encryptHex(content);
// 解密为字符串
String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
Assert.assertEquals(content, decryptStr);
}
use of cn.hutool.crypto.symmetric.AES in project hutool by looly.
the class SymmetricTest method aesUpdateTest.
@Test
public void aesUpdateTest() {
String content = "4321c9a2db2e6b08987c3b903d8d11ff";
AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
// 加密为16进制表示
aes.setMode(CipherMode.encrypt);
String randomData = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
aes.setMode(CipherMode.encrypt);
String randomData2 = aes.updateHex(content.getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(randomData2, randomData);
Assert.assertEquals(randomData, "cd0e3a249eaf0ed80c330338508898c4");
}
use of cn.hutool.crypto.symmetric.AES in project hutool by looly.
the class SymmetricTest method aesZeroPaddingTest2.
@Test
public void aesZeroPaddingTest2() {
String content = "RandomUtil.randomString(RandomUtil.randomInt(2000))";
AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
final ByteArrayOutputStream encryptStream = new ByteArrayOutputStream();
aes.encrypt(IoUtil.toUtf8Stream(content), encryptStream, true);
final ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
aes.decrypt(IoUtil.toStream(encryptStream), contentStream, true);
Assert.assertEquals(content, StrUtil.utf8Str(contentStream.toByteArray()));
}
Aggregations