Paillier同态加密算法 java实现

import org.jetbrains.annotations.NotNull;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Random;

public class Painkiller {
    public BigInteger P,Q;
    public BigInteger N;
    public BigInteger NSQ;
    public BigInteger lamda;
    public int bitLength;
    public BigInteger g;
    public BigInteger u;
    public Painkiller(){
        Key(64,64);
    }
    public Painkiller(int bitLength,int certainty){
        Key(bitLength,certainty);
    }
    public Painkiller(BigInteger N,BigInteger lamda,int bitLength,BigInteger u){
        this.N=N;
        this.lamda=lamda;
        this.bitLength=bitLength;
        this.u=u;
    }
    public void Key(int bitLengthVal, int certainty) {
        bitLength = bitLengthVal;
        this.P = new BigInteger(bitLength / 2, certainty, new Random());
        this.Q = new BigInteger(bitLength / 2, certainty, new Random());
        this.N = P.multiply(Q);
        this.NSQ = N.multiply(N);
        this.g = N.add(new BigInteger("1"));
        this.lamda = P.subtract(BigInteger.ONE).multiply(Q.subtract(BigInteger.ONE))
                .divide(P.subtract(BigInteger.ONE).gcd(Q.subtract(BigInteger.ONE)));
        this.u = g.modPow(this.lamda, this.NSQ).subtract(BigInteger.ONE).divide(this.N).modInverse(this.N);
        if (this.g.modPow(this.lamda, this.NSQ).subtract(BigInteger.ONE).divide(this.N).gcd(this.N).intValue() != 1) {
            System.out.println("g的选取不合适!");
            System.exit(1);
        }
    }
    public BigInteger En(BigInteger m, @NotNull BigInteger r) {
        return g.modPow(m, this.NSQ).multiply(r.modPow(this.N, this.NSQ)).mod(this.NSQ);
    }
    public BigInteger En(BigInteger m) {
        BigInteger r = new BigInteger(bitLength, new Random());
//        System.out.println("random:"+r);
//        System.out.println(("g.modPow(m,NSQ:")+g.modPow(m,this.NSQ));
//        System.out.println("g.modPow(m,NSQ).mul(r.modPow(N,NSQ)):"+g.modPow(m,this.NSQ).multiply(r.modPow(this.N,this.NSQ)));
        return g.modPow(m, this.NSQ).multiply(r.modPow(this.N, this.NSQ)).mod(this.NSQ);
    }
    public BigInteger De(@NotNull BigInteger c) {
        return c.modPow(this.lamda, this.NSQ).subtract(BigInteger.ONE).divide(this.N).multiply(this.u).mod(this.N);
    }
    public BigInteger En(@NotNull String message){
        String encoded = Base64.getEncoder().encodeToString(message.getBytes());
        byte []encoded_b = encoded.getBytes(StandardCharsets.UTF_8);
        StringBuilder message_b = new StringBuilder();
        for(byte val:encoded_b){
//            System.out.printf("%03d",val);
            message_b.append(String.format("%03d", val));
        }
        return this.En(new BigInteger(String.valueOf(message_b)));
    }
    public String DeIfEnByString(BigInteger c){
        c = De(c);
        BigInteger md = new BigInteger("1000");
        System.out.println(c.toString().length());
        byte []bigIntToByteArr = new byte[(c.toString().length()/3+(c.toString().length()%3==0?0:1))];
        System.out.println(bigIntToByteArr.length);
        for(int i=0;i

你可能感兴趣的:(java,算法,同态加密)