【问题标题】:Elliptic curve ElGamal Java implementation椭圆曲线 ElGamal Java 实现
【发布时间】:2012-08-25 22:37:10
【问题描述】:

是否有一个简单的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 的复杂实现。

谢谢。

【问题讨论】:

    标签: java cryptography elliptic-curve elgamal


    【解决方案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");
    

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 谢谢!不是说它对实际代码很重要,但是你知道这篇论文是否有英文版?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多