3

是否有具有密钥生成、加密和解密功能的 ElGamal 椭圆曲线密码系统的简单实现(使用 Java BigInteger);一个可以用来在讲座中向大学生解释的东西?

例如,Paillier 加密函数可以在不失一般性的情况下编码为:

public BigInteger encrypt(BigInteger input) throws PaillierException  {
    if(!isInZN(input)) {
        throw new PaillierException(PaillierException.TYPE_PLAINTEXT_NOT_IN_ZN, input);
    }
    BigInteger plaintext = handleNegative(input);
    BigInteger r = randomInZStarN();
    return (((n.multiply(plaintext).add(BigInteger.ONE)).multiply(r.modPow(n, nSquared)))).mod(nSquared);
}

其中包含优化 g=(1+n),使得密文 c = (1 + mn) r^n mod n^2。

不要建议 Java 7 原生实现或 BouncyCastle 实现,因为我不需要真实世界的符合 JCA 的复杂实现。

谢谢。

4

2 回答 2

1

在这里看看一个德国人的文凭作品
http://www.cdc.informatik.tu-darmstadt.de/reports/reports/crnjak.diplom.ps

于 2013-08-10T04:16:30.307 回答
1

看一下这个。(根据您的需要进行修改)

//X9ECParameters ecParams = NISTNamedCurves.getByName("B-571");
        X9ECParameters ecParams = NISTNamedCurves.getByName("B-163");
        //X9ECParameters ecParams = NISTNamedCurves.getByName("P-384");
        //X9ECParameters ecParams = NISTNamedCurves.getByName("P-521");
        BigInteger privKey = new BigInteger("38e1", 16);
        ECPoint pubKey = ecParams.getG().multiply(privKey);

        System.out.println("Available curves:\n");

        int counter = 0;
        for ( Enumeration e = NISTNamedCurves.getNames(); e.hasMoreElements(); ){
            if (counter == 3) {
                counter = -1;
                System.out.println( e.nextElement().toString() );
            }else{
                System.out.print( e.nextElement().toString() + "    ");
            }
             counter++;
        }

//      System.out.println(privKey.toString(16));

//      for (int i = 1; i < 30; i++) {
//          ECPoint test = ecParams.getG().multiply(
//                  new BigInteger(Integer.toHexString(i), 16));
//          System.out.println("X: " + test.getX().toBigInteger().toString(10)
//                  + "\n" + "Y: " + test.getY().toBigInteger().toString(10)
//                  + "\n");
//      }

        //Encryption "a" = first Point

        ECPoint pMsg = ecParams.getG().multiply(
                new BigInteger(Integer.toHexString(2), 16));

        System.out.println("\n\n--------------------------------------------------------------------------------------------------------------------------------------------------");

        System.out.println("Selected curve:\n");

        System.out.println("Curve:     " + ecParams.getCurve().getFieldSize() + "\n");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");

        System.out.println("Point:     " + "X: " + pMsg.getX().toBigInteger().toString(16) + "\n" + "           Y: " + pMsg.getY().toBigInteger().toString(16) + "\n");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");

        ECPoint one;
        ECPoint two;

        BigInteger random = new BigInteger(16, new SecureRandom());

        /* g^r */
        one = ecParams.getG().multiply(random);
        /* pk^r+m */
        two = pMsg.add(pubKey.multiply(random));

        //encryptedData edM = new encryptedData(one, two);

        System.out.println("Encrypted:\n");

        System.out.println("One:      " + "X: " + one.getX().toBigInteger().toString(16) + "\n" + "           Y: " + one.getY().toBigInteger().toString(16) + "\n");
        System.out.println("Two:      " + "X: " + two.getX().toBigInteger().toString(16) + "\n" + "           Y: " + two.getY().toBigInteger().toString(16) + "\n");

        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------");

        //Decryption

        ECPoint decrypted = edM.eData.subtract(edM.gInR.multiply(privKey));

        System.out.println("Decrypted: " + "X: " + decrypted.getX().toBigInteger().toString(16) + "\n" + "           Y: " + decrypted.getY().toBigInteger().toString(16) + "\n");
于 2013-09-04T18:42:40.067 回答