0

以下の Diffie-Hellman 鍵交換アルゴリズムを実装するコードの書き方を学びましたが、私のコードは最も効率的ではないと感じています。誰でも私のコードを修正できますか...?

import java.math.BigInteger;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Random;;

    public class DH_key {
        static class DHGenParameterSpec implements AlgorithmParameterSpec{
            static BigInteger p;
            static BigInteger g;
            static int a,b;
            static BigInteger A,B;
        static BigInteger getPrimeP_G() {
            Random rand1= new Random(System.currentTimeMillis());
            Random rand2= new Random(System.currentTimeMillis()*10);

            p= BigInteger.probablePrime(32, rand1);
            g= BigInteger.probablePrime(32, rand2);
            System.out.println(""+p+","+g);
            return null;
        }
        static int getExponent() {
            SecureRandom ranGen1 = new SecureRandom();

            a= ranGen1.nextInt(1000);
            b= ranGen1.nextInt(1000);
            System.out.println(a+"__"+b);
            return 0 ;

        }
        public static Object pow (){
            //g.pow(a);
            A = g.pow(getExponent()).mod(getPrimeP_G());
            B = g.pow(b).mod(p);

            return null;        
        }



    public static void main(String[]args){
        //System.out.println(DHGenParameterSpec.getPrimeP_G());
        DHGenParameterSpec.getPrimeP_G();
        DHGenParameterSpec.getExponent();
        A = g.pow(a).mod(p);
        B = g.pow(b).mod(p);

        BigInteger Sa,Sb;
        Sa=B.pow(a).mod(p);
        Sb=A.pow(b).mod(p);

            System.out.println(""+A+"__"+B);
                System.out.println(""+Sa+"__"+Sb);
        }

        }

    }

上記のコードは Java ルールで適切でしたか??

4

1 に答える 1

1

べき乗剰余を次のように記述しました。

        A = g.pow(getExponent()).mod(getPrimeP_G());
        B = g.pow(b).mod(p);

これは、累乗の中間結果が大きな数になる可能性があるため、非効率的です。modPow代わりに、効率的なアルゴリズムで 2 つの操作を行うメソッドを使用する必要があります。

        A = g.modPow(getExponent(), getPrimeP_G());
        B = g.modPow(b, p);
于 2013-08-10T20:38:24.507 に答える