Exemple #1
0
  public static void testICDE() {
    // Number of total operations
    int numberOfTests = 5;
    // Length of the p, note that n=p.q
    int lengthp = 512;

    Paillier esystem = new Paillier();
    Random rd = new Random();
    PaillierPrivateKey key = KeyGen.PaillierKey(lengthp, 122333356);
    esystem.setDecryptEncrypt(key);
    // let's test our algorithm by encrypting and decrypting few instances

    long start = System.currentTimeMillis();
    for (int i = 0; i < numberOfTests; i++) {
      BigInteger m1 = BigInteger.valueOf(Math.abs(rd.nextLong()));
      BigInteger m2 = BigInteger.valueOf(Math.abs(rd.nextLong()));
      BigInteger c1 = esystem.encrypt(m1);
      BigInteger c2 = esystem.encrypt(m2);
      BigInteger c3 = esystem.multiply(c1, m2);
      c1 = esystem.add(c1, c2);
      c1 = esystem.add(c1, c3);

      esystem.decrypt(c1);
    }
    long stop = System.currentTimeMillis();
    System.out.println(
        "Running time per comparison in milliseconds: " + ((stop - start) / numberOfTests));
  }
Exemple #2
0
  /**
   * Decrypts the given ciphertext.
   *
   * @param c Ciphertext as BigInteger c
   * @return Decrypted value D(c) as BigInteger
   */
  public BigInteger decrypt(BigInteger c) {
    // Check whether everything is set for doing decryption
    if (decryptMode == false) throw new IllegalStateException(this.notReadyForDecryption);
    if (!(key.inModNSPlusOne(c))) throw new IllegalArgumentException("c must be less than n^2");

    BigInteger c1 = null;

    // first we calculate c^d mod n^2
    c1 = c.modPow(deckey.getD(), deckey.getNSPlusOne());

    // after we calculate c1=c^d mod n^2 = (1+n)^(m*d mod n)
    // we now find (c1-1)/n=m*d mod n
    // therefore m= d^-1*(c1-1)/n mod n
    // TODO: Is this true?
    return (deckey.getDInvs().multiply((c1.subtract(BigInteger.ONE)).divide(deckey.getN())))
        .mod(deckey.getN());
  }
Exemple #3
0
  /** This main method basically tests the different features of the Paillier encryption */
  public static void test() {
    Random rd = new Random();
    long num = 0;
    long num1 = 0;
    BigInteger m = null;
    BigInteger c = null;
    int numberOfTests = 10;
    int j = 0;
    BigInteger decryption = null;
    Paillier esystem = new Paillier();
    PaillierPrivateKey key = KeyGen.PaillierKey(512, 122333356);
    esystem.setDecryptEncrypt(key);
    // let's test our algorithm by encrypting and decrypting a few instances
    for (int i = 0; i < numberOfTests; i++) {
      num = Math.abs(rd.nextLong());
      m = BigInteger.valueOf(num);
      System.out.println("number chosen  : " + m.toString());
      c = esystem.encrypt(m);
      System.out.println("encrypted value: " + c.toString());
      decryption = esystem.decrypt(c);
      System.out.println("decrypted value: " + decryption.toString());
      if (m.compareTo(decryption) == 0) {
        System.out.println("OK");
        j++;
      } else System.out.println("PROBLEM");
    }
    System.out.println(
        "out of "
            + (new Integer(numberOfTests)).toString()
            + "random encryption,# many of "
            + (new Integer(j)).toString()
            + " has passed");
    // Let us check the commutative properties of the paillier encryption
    System.out.println("Checking the additive properteries of the Paillier encryption");
    //   Obviously 1+0=1
    System.out.println(
        "1+0="
            + (esystem.decrypt(esystem.add(esystem.encryptone(), esystem.encryptzero())))
                .toString());
    // 1+1=2
    System.out.println(
        "1+1="
            + (esystem.decrypt(
                    esystem.add(esystem.encrypt(BigInteger.ONE), esystem.encrypt(BigInteger.ONE))))
                .toString());

    // 1+1+1=3
    System.out.println(
        "1+1+1="
            + (esystem.decrypt(
                    esystem.add(
                        esystem.add(
                            esystem.encrypt(BigInteger.ONE), esystem.encrypt(BigInteger.ONE)),
                        esystem.encrypt(BigInteger.ONE))))
                .toString());

    // 0+0=0
    System.out.println(
        "0+0="
            + (esystem.decrypt(
                    esystem.add(
                        esystem.encrypt(BigInteger.ZERO), esystem.encrypt(BigInteger.ZERO))))
                .toString());
    // 1+-1=0
    System.out.println(
        "1+-1="
            + (esystem.decrypt(
                    esystem.add(
                        esystem.encrypt(BigInteger.valueOf(-1).mod(key.getN())),
                        esystem.encrypt(BigInteger.ONE))))
                .toString());

    do {
      num = rd.nextLong();
    } while (key.inModN(BigInteger.valueOf(num)) == false);
    do {
      num1 = rd.nextLong();
    } while (key.inModN(BigInteger.valueOf(num1)) == false);
    BigInteger numplusnum1 = BigInteger.valueOf(num).add(BigInteger.valueOf(num1));
    BigInteger summodnsquare = numplusnum1.mod(key.getN());
    // D(E(num)+E(num1))=num+num1
    System.out.println(numplusnum1.toString());
    System.out.println(
        summodnsquare.toString()
            + "=\n"
            + esystem
                .decrypt(
                    esystem.add(
                        esystem.encrypt(BigInteger.valueOf(num)),
                        esystem.encrypt(BigInteger.valueOf(num1))))
                .toString());
    // Let us check the multiplicative properties
    System.out.println("Checking the multiplicative properties");
    // D(multiply(E(2),3))=6
    System.out.println(
        "6="
            + esystem.decrypt(
                esystem.multiply(
                    esystem.add(esystem.encrypt(BigInteger.ONE), esystem.encrypt(BigInteger.ONE)),
                    3)));
  }