JAVA:算法:如何在加密值中执行减法运算

时间:2015-02-18 06:35:24

标签: java algorithm logic

我有一个Paillier同态算法代码,其中乘法加法在加密值中执行。

我需要实施减法除法(至少减法),方法与实施 Addition 相同(在加密值中操作) ,

我曾经尝试过,但没有得到预期的输出,

代码是 -

import java.math.*;
import java.util.*;


public class Paillier {

/**
* p and q are two large primes. 
* lambda = lcm(p-1, q-1) = (p-1)*(q-1)/gcd(p-1, q-1).
*/
private BigInteger p, q, lambda;
/**
* n = p*q, where p and q are two large primes.
*/
public BigInteger n;
/**
* nsquare = n*n
*/
public BigInteger nsquare;
/**
* a random integer in Z*_{n^2} where gcd (L(g^lambda mod n^2), n) = 1.
*/
private BigInteger g;
/**
* number of bits of modulus
*/
private int bitLength;

/**
* Constructs an instance of the Paillier cryptosystem.
* @param bitLengthVal number of bits of modulus
* @param certainty The probability that the new BigInteger represents a prime number will exceed (1 - 2^(-certainty)). The execution time of this constructor is proportional to the value of this parameter.
*/
public Paillier(int bitLengthVal, int certainty) {
KeyGeneration(bitLengthVal, certainty);
}

/**
* Constructs an instance of the Paillier cryptosystem with 512 bits of modulus and at least 1-2^(-64) certainty of primes generation.
*/
public Paillier() {
KeyGeneration(512, 64);
}

/**
* Sets up the public key and private key.
* @param bitLengthVal number of bits of modulus.
* @param certainty The probability that the new BigInteger represents a prime number will exceed (1 - 2^(-certainty)). The execution time of this constructor is proportional to the value of this parameter.
*/
public void KeyGeneration(int bitLengthVal, int certainty) {
bitLength = bitLengthVal;
/*Constructs two randomly generated positive BigIntegers that are probably prime, with the specified bitLength and certainty.*/
p = new BigInteger(bitLength / 2, certainty, new Random());
q = new BigInteger(bitLength / 2, certainty, new Random());

n = p.multiply(q);
nsquare = n.multiply(n);

g = new BigInteger("2");
lambda = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)).divide(
p.subtract(BigInteger.ONE).gcd(q.subtract(BigInteger.ONE)));
/* check whether g is good.*/
if (g.modPow(lambda, nsquare).subtract(BigInteger.ONE).divide(n).gcd(n).intValue() != 1) {
System.out.println("g is not good. Choose g again.");
System.exit(1);
}
}

/**
* Encrypts plaintext m. ciphertext c = g^m * r^n mod n^2. This function explicitly requires random input r to help with encryption.
* @param m plaintext as a BigInteger
* @param r random plaintext to help with encryption
* @return ciphertext as a BigInteger
*/
public BigInteger Encryption(BigInteger m, BigInteger r) {
return g.modPow(m, nsquare).multiply(r.modPow(n, nsquare)).mod(nsquare);
}

/**
* Encrypts plaintext m. ciphertext c = g^m * r^n mod n^2. This function automatically generates random input r (to help with encryption).
* @param m plaintext as a BigInteger
* @return ciphertext as a BigInteger
*/
public BigInteger Encryption(BigInteger m) {
BigInteger r = new BigInteger(bitLength, new Random());
return g.modPow(m, nsquare).multiply(r.modPow(n, nsquare)).mod(nsquare);

}

/**
* Decrypts ciphertext c. plaintext m = L(c^lambda mod n^2) * u mod n, where u = (L(g^lambda mod n^2))^(-1) mod n.
* @param c ciphertext as a BigInteger
* @return plaintext as a BigInteger
*/
public BigInteger Decryption(BigInteger c) {
BigInteger u = g.modPow(lambda, nsquare).subtract(BigInteger.ONE).divide(n).modInverse(n);
return c.modPow(lambda, nsquare).subtract(BigInteger.ONE).divide(n).multiply(u).mod(n);
}

/**
* main function
* @param str intput string
*/
public static void main(String[] str) {
/* instantiating an object of Paillier cryptosystem*/
Paillier paillier = new Paillier();
/* instantiating two plaintext msgs*/
BigInteger m1 = new BigInteger("20");
BigInteger m2 = new BigInteger("60");
/* encryption*/
BigInteger em1 = paillier.Encryption(m1);
BigInteger em2 = paillier.Encryption(m2);
/* printout encrypted text*/
System.out.println("En Result1="+em1);
System.out.println("En Result2="+em2);
/* printout decrypted text */
System.out.println("Dec Result1="+paillier.Decryption(em1).toString());
System.out.println("Dec Result2="+paillier.Decryption(em2).toString());

/* test homomorphic properties -> D(E(m1)*E(m2) mod n^2) = (m1 + m2) mod n */
BigInteger product_em1em2 = em1.multiply(em2).mod(paillier.nsquare);
BigInteger sum_m1m2 = m1.add(m2).mod(paillier.n);
System.out.println("original sum: " + sum_m1m2.toString());
System.out.println("decrypted sum: " + paillier.Decryption(product_em1em2).toString());

/* test homomorphic properties -> D(E(m1)^m2 mod n^2) = (m1*m2) mod n */
BigInteger expo_em1m2 = em1.modPow(m2, paillier.nsquare);
BigInteger prod_m1m2 = m1.multiply(m2).mod(paillier.n);
System.out.println("original product: " + prod_m1m2.toString());
System.out.println("decrypted product: " +   paillier.Decryption(expo_em1m2).toString());
}
}   

我的尝试就像这样

BigInteger sub_m1m2 = em1.subtract(em2).mod(paillier.nsquare);
System.out.println("original result: " + sub_m1m2.toString());
System.out.println("decrypted result: " +   paillier.Decryption(sub_m1m2).toString());

但我得到了输出

original result: 10293905733728092798312442334016670915623579115754799359001594575265037444846358865955447355482410894470382250544497599054315984048041610855086875271446876264557306222798287261349828881510552405863659535527193007559975944762965701441522047569562634431616852659521060136138153805278432999456874447872696991987
decrypted result: 5329364443144332451351943891466568608903938067239264159825694002586308632766687575085696305071770727742308734013289113539866112418155659162072887614141015

Expected output
 -40

至少给我一个纠正逻辑的方法。 感谢

3 个答案:

答案 0 :(得分:0)

JS Paillier demo

Pallier密码系统使用javascript的真实甜蜜演示。

我将此演示用于减法作为一种加法 - A +( - B)。这与| A |一样长大于| B |。当| B |时,它给了我一个看似加密的数字更大。

另一个link它没有给出明确的答案或算法

修改:Paillier Division is possible under conditions

答案 1 :(得分:0)

加密价值的减法

我得到了逻辑 A +( - B)

的部分答案

例如: -

BigInteger m1 = new BigInteger("200");
BigInteger m2 = new BigInteger("-100");

BigInteger em1 = paillier.Encryption(m1);
BigInteger em2 = paillier.Encryption(m2);

BigInteger product_em1em2 = em1.multiply(em2).mod(paillier.nsquare);
System.out.println("decrypted Result: " + paillier.Decryption(product_em1em2).toString());

你得到的输出

decrypted Result:100

逻辑中的问题是 B>一个,那么你会得到一个错误的结果,如

8731467600700263693874424644022096065561525426062533141985672767376667419304955165494349828630814277855336552046469491700406386664031693931688991036643291

答案 2 :(得分:0)

这是模块化算术,答案实际上是正确的mod n。如果在这种情况下需要签名的答案,则在b> a的情况下,可以通过取结果n来获得答案。完整示例,

a=100, b=200
a-b = a + (-b)

public n: 189073144844662281547025573708278983341
private lambda: 94536572422331140759761643656524564142
[A] = 14186854275058373459357566990685202758595128512711002543934861918137101534797
[B] = 27267516796978636717778486386991056397778775888737369113501780748148589365346
[A + B] = 23137393292983120368330149560489233461976816700037321139211632742863926737409
res = 189073144844662281547025573708278983241
res - n = -100
相关问题