Search in sources :

Example 6 with AES

use of cn.hutool.crypto.symmetric.AES in project hutool by looly.

the class AESTest method encryptPKCS7Test.

@Test
public void encryptPKCS7Test() {
    // 构建
    AES aes = new AES(Mode.CBC.name(), "pkcs7padding", "1234567890123456".getBytes(), "1234567890123456".getBytes());
    String encryptHex = aes.encryptHex("123456");
    Assert.assertEquals("d637735ae9e21ba50cb686b74fab8d2c", encryptHex);
}
Also used : AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 7 with AES

use of cn.hutool.crypto.symmetric.AES in project hutool by looly.

the class AESTest method gcmTest.

/**
 * 见:https://blog.csdn.net/weixin_42468911/article/details/114358682
 */
@Test
public void gcmTest() {
    final SecretKey key = KeyUtil.generateKey("AES");
    byte[] iv = RandomUtil.randomBytes(12);
    AES aes = new AES("GCM", "NoPadding", key, new GCMParameterSpec(128, iv));
    // 原始数据
    String phone = "13534534567";
    // 加密
    byte[] encrypt = aes.encrypt(phone);
    final String decryptStr = aes.decryptStr(encrypt);
    Assert.assertEquals(phone, decryptStr);
}
Also used : SecretKey(javax.crypto.SecretKey) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 8 with AES

use of cn.hutool.crypto.symmetric.AES in project hutool by looly.

the class AESTest method aesWithSha1PrngTest.

@Test
public void aesWithSha1PrngTest() {
    final SecureRandom random = RandomUtil.getSecureRandom("123456".getBytes());
    final SecretKey secretKey = KeyUtil.generateKey("AES", 128, random);
    String content = "12sdfsdfs你好啊!";
    AES aes = new AES(secretKey);
    final String result1 = aes.encryptBase64(content);
    final String decryptStr = aes.decryptStr(result1);
    Assert.assertEquals(content, decryptStr);
}
Also used : SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 9 with AES

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);
}
Also used : AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 10 with AES

use of cn.hutool.crypto.symmetric.AES in project hutool by looly.

the class AESTest method encryptCTSTest.

@Test
public void encryptCTSTest() {
    String content = "test中文";
    AES aes = new AES(Mode.CTS, Padding.PKCS5Padding, "0CoJUm6Qyw8W8jue".getBytes(), "0102030405060708".getBytes());
    final String encryptHex = aes.encryptHex(content);
    Assert.assertEquals("8dc9de7f050e86ca2c8261dde56dfec9", encryptHex);
}
Also used : AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Aggregations

AES (cn.hutool.crypto.symmetric.AES)15 Test (org.junit.Test)15 SecretKey (javax.crypto.SecretKey)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SecureRandom (java.security.SecureRandom)1 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)1