Алгоритм цифровой подписи ECC не работает в Curve1174 - PullRequest
0 голосов
/ 29 сентября 2019


Мне очень жаль, что я виноват.Я обновляю свой пост.

Здравствуйте, я чувствую себя беспомощным, поэтому я здесь.Я пытаюсь реализовать алгоритм цифровой подписи эллиптической кривой в Java.Я исследовал 4-5 сайтов и попробовал все их реализовать, но без надежды.

Это самая сложная вещь в моем проекте.Если вы мне поможете, я чувствую благодарность и много молюсь за вас.Я без дела смотрел на экран компьютера в течение 3 дней.

Это мой справочник, где я получил свой алгоритм: https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm

Вот мой код.

Основная функция:

 public static void main(String[] args) throws Exception {

    BigInteger privKey = new BigInteger("2745724516635025822557365208402885644404066900627883196685711171635003551842");
    BigInteger pubKeyX = new BigInteger("3490407221567794904593198038072456611391906952823165422546016226941846181139");
    BigInteger pubKeyY = new BigInteger("3425516502002003836403290274098517467200292853479736771973411613924724560216");
    ECPoint pubKey = new ECPoint(pubKeyX, pubKeyY);

    String message = "hello world";
    BigInteger signOfMessage = new BigInteger("372506899180426434818389796841307943416967607371698304426758798769819064201"); 
    // I calculated first what is the sign of message then pass it to verify at the same time

    ECCsign.operate(message, privKey);
    ECCverify.operate(message, signOfMessage, pubKey);

}


Класс подписи:

public class ECCsign { 
public static final BigInteger p = new BigInteger("3618502788666131106986593281521497120414687020801267626233049500247285301239"); 

public static void operate(String message,BigInteger privateKey){

    // HASH OF MESSAGE IN INTEGER
    BigInteger messageInBigInteger = new BigInteger(HASHsha256.operate(message).getBytes()); 

    //  CHOOSE TOTALLY RANDOM NUMBER AND PASS IT TO CURVE
    /* long rand = (long) (Math.random()*1000000000); */
    long rand = 36337539; // selected to observe temporaryly
    BigInteger totallyRandom = new BigInteger(String.valueOf(rand));
    ECPoint randomlyCurvePoint = ECCprocess.multiplying(null,totallyRandom);

    // DETERMINE R
    BigInteger r = randomlyCurvePoint.getAffineX().mod(p);

    // CALCULATE DIGITAL SIGNATURE
    BigInteger signature = (totallyRandom.modInverse(p)).multiply((messageInBigInteger).add((r.multiply(privateKey))));
    signature = signature.mod(p);

    System.out.println("Signature is = "+signature);
    System.out.println("r = "+r);
    System.out.println();

    }
}

Проверочный класс:

public class ECCverify {
public static final BigInteger p = new BigInteger("3618502788666131106986593281521497120414687020801267626233049500247285301239"); 

public static void operate(String message,BigInteger sign,ECPoint publicKey){

    // WE KNOW R AFTER SIGNING
    BigInteger r = new BigInteger("834203581969436604289304727037177631365477523265271157077027635446523009901");

    // HASHED MESSAGE IN BIGINTEGER
    BigInteger messageInBigInteger = new BigInteger(HASHsha256.operate(message).getBytes());

    // INVERSE OF THE SIGNATURE IN CURVE
    BigInteger sInverse = sign.modInverse(p);

    BigInteger u1 = (messageInBigInteger.multiply(sInverse)).mod(p);
    BigInteger u2 = (r.multiply(sInverse)).mod(p);

    ECPoint point1 = ECCprocess.multiplying(null, u1); // null = Generator
    ECPoint point2 = ECCprocess.multiplying(publicKey, u2);

    ECPoint resultPoint = ECCprocess.adding(point1, point2);

    System.out.println("x1 (We hope that eqaul to r)= "+resultPoint.getAffineX().mod(p));

    }
}


Класс умножения и сложения Curve1174 [проверено, что функции верны]

public class ECCprocess {
// C1174

// OPERATORS
public static ECPoint adding(ECPoint point1,ECPoint point2){
    ECPoint resultPoint;

    CONSTANTlist constantList = new CONSTANTlist();

    BigInteger resultX = (((point1.getAffineX().multiply(point2.getAffineY())).add(point2.getAffineX().multiply(point1.getAffineY()))).multiply(((new BigInteger("1")).add(((BigInteger)constantList.getConstant("dConstant")).multiply(point1.getAffineX().multiply(point2.getAffineX().multiply(point1.getAffineY().multiply(point2.getAffineY())))))).modInverse((BigInteger)constantList.getConstant("pConstant")))).mod((BigInteger)constantList.getConstant("pConstant"));
    BigInteger resultY = (((point1.getAffineY().multiply(point2.getAffineY())).subtract(point2.getAffineX().multiply(point1.getAffineX()))).multiply(((new BigInteger("1")).subtract(((BigInteger)constantList.getConstant("dConstant")).multiply(point1.getAffineX().multiply(point2.getAffineX().multiply(point1.getAffineY().multiply(point2.getAffineY())))))).modInverse((BigInteger)constantList.getConstant("pConstant")))).mod((BigInteger)constantList.getConstant("pConstant"));

    resultPoint = new ECPoint(resultX, resultY);
    return resultPoint;

}

public static ECPoint multiplying(ECPoint generatorPoint,BigInteger getPublicKey){
    BigInteger publicKey = new BigInteger(getPublicKey.toString());
    CONSTANTlist constantList = new CONSTANTlist();
    generatorPoint = (generatorPoint == null) ? (ECPoint) constantList.getConstant("generatorPoint") : generatorPoint;
    ECPoint resultPoint = new ECPoint(new BigInteger("0"), new BigInteger("1"));

    for(int i=0;;i++){
        ECPoint temporaryPoint = (ECPoint) constantList.getConstant("generatorPoint");
        BigInteger keepValue = new BigInteger("1");short powbase2;
        for(powbase2=1;;powbase2+=1){
            keepValue = keepValue.multiply(new BigInteger("2"));
            if(publicKey.compareTo(keepValue)<0) break;
        }
        keepValue = keepValue.divide(new BigInteger("2"));powbase2-=1;
        publicKey = publicKey.subtract(keepValue);
        for(short k=0;k<powbase2;k++){
            temporaryPoint = ECCprocess.adding(temporaryPoint, temporaryPoint);
        }
        resultPoint = ECCprocess.adding(resultPoint, temporaryPoint);
        if(publicKey.compareTo(new BigInteger("32768")) < 0 ) break;
    }

    for(short m=0,n=Short.valueOf(publicKey.toString());m<n;m++){
        resultPoint = ECCprocess.adding(resultPoint,(ECPoint)constantList.getConstant("generatorPoint"));
    }

    return resultPoint;

}
}


Наконец, функция статического управления HASHsha256 хеширует свои параметры.

Спасибо вам

...