Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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 Paillier加密算法_Java_Encryption_Text - Fatal编程技术网

Java Paillier加密算法

Java Paillier加密算法,java,encryption,text,Java,Encryption,Text,有人能告诉我如何为我上传的文件实现paillier算法吗?这是我的代码 import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.math.*; import java.util.*; import java.lang.*; public class Paillier { //*******************************paillier encryp

有人能告诉我如何为我上传的文件实现paillier算法吗?这是我的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.*;
import java.util.*;
import java.lang.*;

public class Paillier {
//*******************************paillier encryption******************************
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 1024 or 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);
}


//********************************file part main************************
  public static void main(String[] args) {
    //******************paillier***************
    Paillier paillier = new Paillier();


    //********************file part**************
    File file = new File(args[0]);//path of file
    if (!file.exists()) {
      System.out.println(args[0] + " does not exist.");
      return;
    }
    if (!(file.isFile() && file.canRead())) {
      System.out.println(file.getName() + " cannot be read from.");
      return;
    }
    try {
      FileInputStream fis = new FileInputStream(file);
      char msg;
String m1;
      while (fis.available() > 0) {
        msg = (char) fis.read();
      // System.out.print(msg);

     m1=Character.toString(msg);
     //System.out.print(m1);//plain text
BigInteger bi = new BigInteger(m1.getBytes());
//System.out.print(bi);//bit converted text
//System.out.print(new String(bi.toByteArray()));//regained plain text 
/* instantiating two plaintext msgs*/

BigInteger m = new BigInteger("bi");
BigInteger m2 = new BigInteger("1");
/* encryption*/
BigInteger em1 = paillier.Encryption(m);
BigInteger em2 = paillier.Encryption(m2);
System.out.println(paillier.Decryption(em1).toString());
//System.out.println(paillier.Decryption(em2).toString());

      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

在这里,我想将我的文件字符设置为bigint并进行加密,但我没有这样做的想法。请解决这个问题。我正在发送一个文件sample.txt中的明文,使用命令行参数说“谢谢你”

我想你可能会欣赏我的一个老项目中的三段代码:

私钥生成 这些方法是类
PaillierPrivateKey
的成员

请注意,我使用的不是$\lambda=\varphi(n)$,而是这个数的倍数,
d
,它与1模
n
一致。这样,如果我将密文$m(n+1)r^n$提升到幂
d
,我们只剩下$m(n+1)$

此外,此方法还返回一个私钥。要从这个元组中获取公钥,只需使用
n

/**
 * generate
 * generate a private key for the Paillier cryptosystem where the
 * primes have bitLength bits
 * @param bitLength
 * @return
 */
public static PaillierPrivateKey generate(int bitLength)
{
    BigInteger p = new SafePrime(bitLength);
    BigInteger q = new SafePrime(bitLength);
    return generate( p, q );
}
public static PaillierPrivateKey generate(BigInteger p, BigInteger q)
{
    BigInteger n = p.multiply(q);
    BigInteger m = p.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2))
                    .multiply( q.subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)));
    BigInteger d = m.modInverse(n).multiply(m).mod(n.multiply(n));
    return new PaillierPrivateKey( n, d );
}
加密 这些方法是类
PaillierPublicey
的成员。函数
getProduct()
返回$n$,而
getmodule()
返回$n^2$,而
getGenerator()
返回$n+1$。类构造函数
randomininversiblebiginteger
获取一个模(即$n^2$)并返回范围${1,…,n^2}$中的一个随机整数,并确保它在乘法模$n^2$下是可逆的。(对于大约一千位的实际$n$,随机选择是可以的;对于小玩具示例,您希望过滤掉零因子。)

解密 此方法也是
PaillierPrivateKey
的成员。同样,
getmodule()
返回$n^2$,而
getProduct()
返回$n$。函数
getPrivateExponent()
返回上述漂亮的整数
d

public BigInteger decrypt( BigInteger ciphertext )
{
    return ciphertext.modPow(   getPrivateExponent(),
                                getModulus() )
                            .subtract(BigInteger.ONE)
                            .divide(getProduct());
}

@yyyyyy说了些什么。现在,我要问一个与密码学相关的问题,你为什么要这样做?我想为我的基于云的安全项目的文件尝试paillier加密你是否在使用paillier的同态属性?公钥加密很少用于加密文件。同态配对可能会有所帮助:。
public BigInteger decrypt( BigInteger ciphertext )
{
    return ciphertext.modPow(   getPrivateExponent(),
                                getModulus() )
                            .subtract(BigInteger.ONE)
                            .divide(getProduct());
}