Search in sources :

Example 11 with AES

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

the class AESTest method encryptPKCS7Test2.

/**
 * AES加密/解密
 * 加解密模式:ECB模式 数据填充模式:PKCS7
 * <p>
 * 数据:16c5
 * 密钥: 0102030405060708090a0b0c0d0e0f10
 * 数据格式:hex格式 加解密模式:ECB模式 数据填充模式:PKCS7
 * 结果: 25869eb3ff227d9e34b3512d3c3c92ed 【加密后的Hex】
 * 结果: JYaes/8ifZ40s1EtPDyS7Q== 【加密后的Base64】
 * <p>
 * 数据:16c5
 * 密钥: 0102030405060708090a0b0c0d0e0f10
 * 数据格式:UTF-8格式 加解密模式:ECB模式 数据填充模式:PKCS7
 * 结果: 79c210d3e304932cf9ea6a9c887c6d7c 【加密后的Hex】
 * 结果: ecIQ0+MEkyz56mqciHxtfA== 【加密后的Base64】
 * <p>
 * AES在线解密 AES在线加密 Aes online hex 十六进制密钥 - The X 在线工具
 * https://the-x.cn/cryptography/Aes.aspx
 */
@Test
public void encryptPKCS7Test2() {
    // 构建
    AES aes = new AES(Mode.ECB.name(), "pkcs7padding", HexUtil.decodeHex("0102030405060708090a0b0c0d0e0f10"));
    // ------------------------------------------------------------------------
    // 加密数据为16进制字符串
    String encryptHex = aes.encryptHex(HexUtil.decodeHex("16c5"));
    // 加密后的Hex
    Assert.assertEquals("25869eb3ff227d9e34b3512d3c3c92ed", encryptHex);
    // 加密数据为16进制字符串
    String encryptHex2 = aes.encryptBase64(HexUtil.decodeHex("16c5"));
    // 加密后的Base64
    Assert.assertEquals("JYaes/8ifZ40s1EtPDyS7Q==", encryptHex2);
    // 解密
    Assert.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt("25869eb3ff227d9e34b3512d3c3c92ed")));
    Assert.assertEquals("16c5", HexUtil.encodeHexStr(aes.decrypt(HexUtil.encodeHexStr(Base64.decode("JYaes/8ifZ40s1EtPDyS7Q==")))));
    // ------------------------------------------------------------------------
    // ------------------------------------------------------------------------
    // 加密数据为字符串(UTF-8)
    String encryptStr = aes.encryptHex("16c5");
    // 加密后的Hex
    Assert.assertEquals("79c210d3e304932cf9ea6a9c887c6d7c", encryptStr);
    // 加密数据为字符串(UTF-8)
    String encryptStr2 = aes.encryptBase64("16c5");
    // 加密后的Base64
    Assert.assertEquals("ecIQ0+MEkyz56mqciHxtfA==", encryptStr2);
    // 解密
    Assert.assertEquals("16c5", aes.decryptStr("79c210d3e304932cf9ea6a9c887c6d7c"));
    Assert.assertEquals("16c5", aes.decryptStr(Base64.decode("ecIQ0+MEkyz56mqciHxtfA==")));
// ------------------------------------------------------------------------
}
Also used : AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 12 with AES

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

the class AESTest method encryptCBCTest.

@Test
public void encryptCBCTest() {
    // 构建
    AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "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 13 with AES

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

the class SymmetricTest method aesPkcs7PaddingTest.

@Test
public void aesPkcs7PaddingTest() {
    String content = RandomUtil.randomString(RandomUtil.randomInt(200));
    AES aes = new AES("CBC", "PKCS7Padding", RandomUtil.randomBytes(32), "DYgjCEIMVrj2W9xN".getBytes());
    // 加密为16进制表示
    String encryptHex = aes.encryptHex(content);
    // 解密
    String decryptStr = aes.decryptStr(encryptHex);
    Assert.assertEquals(content, decryptStr);
}
Also used : AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 14 with AES

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

the class SymmetricTest method aesTest4.

@Test
public void aesTest4() {
    String content = "4321c9a2db2e6b08987c3b903d8d11ff";
    AES aes = new AES(Mode.CBC, Padding.PKCS5Padding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
    // 加密为16进制表示
    String encryptHex = aes.encryptHex(content);
    Assert.assertEquals("cd0e3a249eaf0ed80c330338508898c4bddcfd665a1b414622164a273ca5daf7b4ebd2c00aaa66b84dd0a237708dac8e", encryptHex);
}
Also used : AES(cn.hutool.crypto.symmetric.AES) Test(org.junit.Test)

Example 15 with AES

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

the class SymmetricTest method aesZeroPaddingTest.

@Test
public void aesZeroPaddingTest() {
    String content = RandomUtil.randomString(RandomUtil.randomInt(200));
    AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
    // 加密为16进制表示
    String encryptHex = aes.encryptHex(content);
    // 解密
    String decryptStr = aes.decryptStr(encryptHex);
    Assert.assertEquals(content, decryptStr);
}
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