mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
添加同态加密算法Paillier
This commit is contained in:
parent
c3a05d5159
commit
c84f674c1e
@ -9,7 +9,7 @@
|
|||||||
* 【poi 】 添加系列方法writeCol,以支持按列输出(pr#1003@Gitee)
|
* 【poi 】 添加系列方法writeCol,以支持按列输出(pr#1003@Gitee)
|
||||||
* 【core 】 CollUtil新增anyMatch和allMatch方法(pr#1008@Gitee)
|
* 【core 】 CollUtil新增anyMatch和allMatch方法(pr#1008@Gitee)
|
||||||
* 【core 】 CsvWriter如果开启了append=true,默认自动开启endingLineBreak=true(pr#1010@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修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复TreeUtil.getParentsName()获取到的路径集合中存在值为null的路径名称问题(issue#I795IN@Gitee)
|
* 【core 】 修复TreeUtil.getParentsName()获取到的路径集合中存在值为null的路径名称问题(issue#I795IN@Gitee)
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
package cn.hutool.crypto.asymmetric;
|
package cn.hutool.crypto.asymmetric;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.crypto.asymmetric.paillier.Paillier;
|
import cn.hutool.crypto.asymmetric.paillier.Paillier;
|
||||||
import cn.hutool.crypto.asymmetric.paillier.PaillierKeyPair;
|
import cn.hutool.crypto.asymmetric.paillier.PaillierKeyPair;
|
||||||
import cn.hutool.crypto.asymmetric.paillier.PaillierPrivateKey;
|
import cn.hutool.crypto.asymmetric.paillier.PaillierPrivateKey;
|
||||||
import cn.hutool.crypto.asymmetric.paillier.PaillierpublicKey;
|
import cn.hutool.crypto.asymmetric.paillier.PaillierpublicKey;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
@ -16,38 +18,44 @@ public class PaillierTest {
|
|||||||
@Test
|
@Test
|
||||||
public void test() throws NoSuchAlgorithmException {
|
public void test() throws NoSuchAlgorithmException {
|
||||||
//生成 公钥和私钥
|
//生成 公钥和私钥
|
||||||
PaillierKeyPair paillierKeyPair = Paillier.generateKey();
|
final PaillierKeyPair paillierKeyPair = Paillier.generateKey();
|
||||||
PaillierpublicKey publicKey = paillierKeyPair.getPublicKey();
|
final PaillierpublicKey publicKey = paillierKeyPair.getPublicKey();
|
||||||
PaillierPrivateKey privateKey = paillierKeyPair.getPrivateKey();
|
final PaillierPrivateKey privateKey = paillierKeyPair.getPrivateKey();
|
||||||
|
|
||||||
//创建两个大整数m1,m2:
|
//创建两个大整数m1,m2:
|
||||||
BigInteger m1 = new BigInteger("10");
|
final BigInteger m1 = new BigInteger("10");
|
||||||
BigInteger m2 = new BigInteger("40");
|
final BigInteger m2 = new BigInteger("40");
|
||||||
|
|
||||||
//公钥加密私钥解
|
//公钥加密私钥解
|
||||||
byte[] em1 = Paillier.encrypt(m1, publicKey);
|
final byte[] em1 = Paillier.encrypt(m1, publicKey);
|
||||||
System.out.println("m1解密结果" + Paillier.decrypt(em1, privateKey));
|
Assert.assertEquals("10", Paillier.decrypt(em1, privateKey));
|
||||||
|
|
||||||
//同态特性
|
//同态特性
|
||||||
byte[] em2 = Paillier.encrypt(m2, publicKey);
|
final byte[] em2 = Paillier.encrypt(m2, publicKey);
|
||||||
System.out.println("m1加密em1 :"+ new BigInteger(em1).toString(16));
|
Assert.assertNotNull(new BigInteger(em1).toString(16));
|
||||||
System.out.println("m2加密em2 :"+ new BigInteger(em2).toString(16));
|
Assert.assertNotNull(new BigInteger(em2).toString(16));
|
||||||
byte[] add = Paillier.add(em1, em2, publicKey);
|
//Console.log("m1加密em1 :"+ new BigInteger(em1).toString(16));
|
||||||
System.out.println("m1+m2 密文的和:" + new BigInteger(add).toString(16));
|
//Console.log("m2加密em2 :"+ new BigInteger(em2).toString(16));
|
||||||
System.out.println("m1+m2 密文的和的解:" + Paillier.decrypt(add,privateKey));
|
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);
|
final byte[] mul = Paillier.multiply(em1, m2, publicKey);
|
||||||
System.out.println("m1*m2 密文的积:" + new BigInteger(mul).toString(16));
|
Assert.assertNotNull(new BigInteger(mul).toString(16));
|
||||||
System.out.println("m1*m2 密文的积的解:" + Paillier.decrypt(mul,privateKey));
|
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啊微软";
|
final String s = "123456dfsgsdg!@#%!@@#$!#%是豆腐干山豆根v啊微软";
|
||||||
System.out.println("字符串明文: "+s);
|
|
||||||
|
|
||||||
byte[] encrypt = Paillier.encryptString(s, publicKey);
|
final byte[] encrypt = Paillier.encryptString(s, publicKey);
|
||||||
System.out.println("字符串密文: "+ new BigInteger(encrypt).toString(16) );
|
Assert.assertNotNull(new BigInteger(encrypt).toString(16));
|
||||||
|
//Console.log("字符串密文: "+ new BigInteger(encrypt).toString(16) );
|
||||||
|
|
||||||
String decrypt = Paillier.decryptString(encrypt, privateKey);
|
final String decrypt = Paillier.decryptString(encrypt, privateKey);
|
||||||
System.out.println("字符串密文解密: "+ decrypt);
|
Assert.assertEquals(s, decrypt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user