Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA:算法:如何在加密值中执行减法运算_Java_Algorithm_Logic - Fatal编程技术网

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

JAVA:算法:如何在加密值中执行减法运算,java,algorithm,logic,Java,Algorithm,Logic,我有一个Paillier同态算法的代码,其中乘法和加法在加密值中执行 我需要实现减法和除法,至少减法的实现方式与加密值中的加法操作相同 我试过了,但没有得到预期的结果 守则是— 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). */

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

我需要实现减法和除法,至少减法的实现方式与加密值中的加法操作相同

我试过了,但没有得到预期的结果

守则是—

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
至少给我一个纠正逻辑的方法。 谢谢

使用javascript的苍白密码系统的真正甜美的演示

我使用这个演示作为加法-a+-B的一种。只要| A |大于| B |就行了。当| B |更大时,它给了我一个看似加密的数字

另一个问题是,它没有给出明确的答案或算法


编辑:

加密值中的减法

我用逻辑A+-B得到了部分答案

例如:-

你得到的输出

decrypted Result:100
逻辑中的问题是,如果B>A,那么您将得到一个错误的结果,如

8731467600700263693874424644022096065561525426062533141985672767376667419304955165494349828630814277855336552046469491700406386664031693931688991036643291

这是模运算,答案实际上是正确的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

事实上,我需要在Android中实现这一点,所以Javascript可能不是一个更好的解决方案,但我将尝试您的逻辑…………感谢您的帮助我提供了一个指向演示的链接,作为测试输入和逻辑的方法:我想您的逻辑应该是这样的a+-B,所以您会得到结果。如果A和B都是-ve,则输出应该是加密的数字。两种情况下A都应该是更大的数字NB:-基于我上面发布的代码。
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