add interface

This commit is contained in:
Looly
2021-09-08 11:11:27 +08:00
parent 4c237ba95e
commit 8ddf18fb7a
18 changed files with 931 additions and 645 deletions

View File

@@ -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
*/

View File

@@ -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);
}
}