mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add interface
This commit is contained in:
@@ -7,14 +7,11 @@ import cn.hutool.crypto.KeyUtil;
|
||||
import cn.hutool.crypto.Mode;
|
||||
import cn.hutool.crypto.Padding;
|
||||
import cn.hutool.crypto.symmetric.AES;
|
||||
import org.bouncycastle.crypto.util.BasicAlphabetMapper;
|
||||
import org.bouncycastle.jcajce.spec.FPEParameterSpec;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
public class AESTest {
|
||||
@@ -117,33 +114,6 @@ public class AESTest {
|
||||
Assert.assertEquals(content, decryptStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* 见:https://github.com/dromara/hutool/issues/1814
|
||||
*/
|
||||
@Test
|
||||
public void fpeTest() {
|
||||
// 映射字符表,规定了明文和密文的字符范围
|
||||
BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("0123456789");
|
||||
|
||||
// 初始化 aes 密钥
|
||||
byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
|
||||
AES aes = new AES("FF1", "NoPadding",
|
||||
new SecretKeySpec(keyBytes, "FF1"),
|
||||
new FPEParameterSpec(numberMapper.getRadix(), new byte[]{}));
|
||||
|
||||
// 原始数据
|
||||
String phone = "13534534567";
|
||||
// 加密
|
||||
byte[] inputDataByte = numberMapper.convertToIndexes(phone.toCharArray());
|
||||
byte[] encrypt = aes.encrypt(inputDataByte);
|
||||
|
||||
// 通过 mapper 将密文输出处理为原始格式
|
||||
char[] encryptChars = numberMapper.convertToChars(encrypt);
|
||||
// 手机号码加密: 13534534567 -> 49725950626
|
||||
Assert.assertEquals(phone.length(), encryptChars.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 见:https://blog.csdn.net/weixin_42468911/article/details/114358682
|
||||
*/
|
||||
|
@@ -0,0 +1,48 @@
|
||||
package cn.hutool.crypto.test.symmetric.fpe;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.crypto.symmetric.fpe.FPE;
|
||||
import org.bouncycastle.crypto.util.BasicAlphabetMapper;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class FPETest {
|
||||
|
||||
@Test
|
||||
public void ff1Test(){
|
||||
// 映射字符表,规定了明文和密文的字符范围
|
||||
BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("A0123456789");
|
||||
// 初始化 aes 密钥
|
||||
byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
|
||||
final FPE fpe = new FPE(FPE.FPEMode.FF1, keyBytes, numberMapper, null);
|
||||
|
||||
// 原始数据
|
||||
String phone = RandomUtil.randomString("A0123456789", 13);
|
||||
final String encrypt = fpe.encrypt(phone);
|
||||
// 加密后与原密文长度一致
|
||||
Assert.assertEquals(phone.length(), encrypt.length());
|
||||
|
||||
final String decrypt = fpe.decrypt(encrypt);
|
||||
Assert.assertEquals(phone, decrypt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ff3Test(){
|
||||
// 映射字符表,规定了明文和密文的字符范围
|
||||
BasicAlphabetMapper numberMapper = new BasicAlphabetMapper("A0123456789");
|
||||
// 初始化 aes 密钥
|
||||
byte[] keyBytes = RandomUtil.randomBytes(16);
|
||||
|
||||
final FPE fpe = new FPE(FPE.FPEMode.FF3_1, keyBytes, numberMapper, null);
|
||||
|
||||
// 原始数据
|
||||
String phone = RandomUtil.randomString("A0123456789", 13);
|
||||
final String encrypt = fpe.encrypt(phone);
|
||||
// 加密后与原密文长度一致
|
||||
Assert.assertEquals(phone.length(), encrypt.length());
|
||||
|
||||
final String decrypt = fpe.decrypt(encrypt);
|
||||
Assert.assertEquals(phone, decrypt);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user