diff --git a/CHANGELOG.md b/CHANGELOG.md index 58091117d..045e8f304 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ * 【poi 】 添加系列方法writeCol,以支持按列输出(pr#1003@Gitee) * 【core 】 CollUtil新增anyMatch和allMatch方法(pr#1008@Gitee) * 【core 】 CsvWriter如果开启了append=true,默认自动开启endingLineBreak=true(pr#1010@Gitee) -* 【crypto】 CsvWriter如果开启了append=true,默认自动开启endingLineBreak=true(pr#1010@Gitee) +* 【crypto】 添加同态加密算法Paillier(pr#1010@Gitee) ### 🐞Bug修复 * 【core 】 修复TreeUtil.getParentsName()获取到的路径集合中存在值为null的路径名称问题(issue#I795IN@Gitee) diff --git a/hutool-crypto/src/test/java/cn/hutool/crypto/asymmetric/PaillierTest.java b/hutool-crypto/src/test/java/cn/hutool/crypto/asymmetric/PaillierTest.java index 5aa3780b1..d110c7632 100644 --- a/hutool-crypto/src/test/java/cn/hutool/crypto/asymmetric/PaillierTest.java +++ b/hutool-crypto/src/test/java/cn/hutool/crypto/asymmetric/PaillierTest.java @@ -1,9 +1,11 @@ package cn.hutool.crypto.asymmetric; +import cn.hutool.core.lang.Console; import cn.hutool.crypto.asymmetric.paillier.Paillier; import cn.hutool.crypto.asymmetric.paillier.PaillierKeyPair; import cn.hutool.crypto.asymmetric.paillier.PaillierPrivateKey; import cn.hutool.crypto.asymmetric.paillier.PaillierpublicKey; +import org.junit.Assert; import org.junit.Test; import java.math.BigInteger; @@ -16,38 +18,44 @@ public class PaillierTest { @Test public void test() throws NoSuchAlgorithmException { //生成 公钥和私钥 - PaillierKeyPair paillierKeyPair = Paillier.generateKey(); - PaillierpublicKey publicKey = paillierKeyPair.getPublicKey(); - PaillierPrivateKey privateKey = paillierKeyPair.getPrivateKey(); + final PaillierKeyPair paillierKeyPair = Paillier.generateKey(); + final PaillierpublicKey publicKey = paillierKeyPair.getPublicKey(); + final PaillierPrivateKey privateKey = paillierKeyPair.getPrivateKey(); //创建两个大整数m1,m2: - BigInteger m1 = new BigInteger("10"); - BigInteger m2 = new BigInteger("40"); + final BigInteger m1 = new BigInteger("10"); + final BigInteger m2 = new BigInteger("40"); //公钥加密私钥解 - byte[] em1 = Paillier.encrypt(m1, publicKey); - System.out.println("m1解密结果" + Paillier.decrypt(em1, privateKey)); + final byte[] em1 = Paillier.encrypt(m1, publicKey); + Assert.assertEquals("10", Paillier.decrypt(em1, privateKey)); //同态特性 - byte[] em2 = Paillier.encrypt(m2, publicKey); - System.out.println("m1加密em1 :"+ new BigInteger(em1).toString(16)); - System.out.println("m2加密em2 :"+ new BigInteger(em2).toString(16)); - byte[] add = Paillier.add(em1, em2, publicKey); - System.out.println("m1+m2 密文的和:" + new BigInteger(add).toString(16)); - System.out.println("m1+m2 密文的和的解:" + Paillier.decrypt(add,privateKey)); + final byte[] em2 = Paillier.encrypt(m2, publicKey); + Assert.assertNotNull(new BigInteger(em1).toString(16)); + Assert.assertNotNull(new BigInteger(em2).toString(16)); + //Console.log("m1加密em1 :"+ new BigInteger(em1).toString(16)); + //Console.log("m2加密em2 :"+ new BigInteger(em2).toString(16)); + final byte[] add = Paillier.add(em1, em2, publicKey); + Assert.assertNotNull(new BigInteger(add).toString(16)); + Assert.assertNotNull(Paillier.decrypt(add,privateKey)); + //Console.log("m1+m2 密文的和:" + new BigInteger(add).toString(16)); + //Console.log("m1+m2 密文的和的解:" + Paillier.decrypt(add,privateKey)); - byte[] mul = Paillier.multiply(em1, m2, publicKey); - System.out.println("m1*m2 密文的积:" + new BigInteger(mul).toString(16)); - System.out.println("m1*m2 密文的积的解:" + Paillier.decrypt(mul,privateKey)); + final byte[] mul = Paillier.multiply(em1, m2, publicKey); + Assert.assertNotNull(new BigInteger(mul).toString(16)); + Assert.assertNotNull(Paillier.decrypt(mul,privateKey)); + //Console.log("m1*m2 密文的积:" + new BigInteger(mul).toString(16)); + //Console.log("m1*m2 密文的积的解:" + Paillier.decrypt(mul,privateKey)); //加解密字符串 - String s = "123456dfsgsdg!@#%!@@#$!#%是豆腐干山豆根v啊微软"; - System.out.println("字符串明文: "+s); + final String s = "123456dfsgsdg!@#%!@@#$!#%是豆腐干山豆根v啊微软"; - byte[] encrypt = Paillier.encryptString(s, publicKey); - System.out.println("字符串密文: "+ new BigInteger(encrypt).toString(16) ); + final byte[] encrypt = Paillier.encryptString(s, publicKey); + Assert.assertNotNull(new BigInteger(encrypt).toString(16)); + //Console.log("字符串密文: "+ new BigInteger(encrypt).toString(16) ); - String decrypt = Paillier.decryptString(encrypt, privateKey); - System.out.println("字符串密文解密: "+ decrypt); + final String decrypt = Paillier.decryptString(encrypt, privateKey); + Assert.assertEquals(s, decrypt); } }