在java中传递一个大整数值

时间:2013-08-16 00:32:16

标签: java

public class A{

    long set_time,ext_time,ident_time;
    long set_time2,ext_time2,ident_time2;
    BigInteger p,q,phiN,e,d,modu;
    KeyPairGenerator keygen;
    KeyPair keypair;
    RSAPublicKey publicKey;
    RSAPrivateCrtKey privateKey;
    MessageDigest H,G;
    String TM;

    public void Setup(int k)
    {
         SecureRandom random = new SecureRandom();
         try
         {
            set_time2 = System.nanoTime();

            keygen = KeyPairGenerator.getInstance("RSA");
            keygen.initialize(k, random);
            keypair = keygen.genKeyPair();
            privateKey = (RSAPrivateCrtKey)keypair.getPrivate();
            publicKey = (RSAPublicKey)keypair.getPublic();

            p = privateKey.getPrimeP();
            q = privateKey.getPrimeQ();
            phiN = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
            e = privateKey.getPublicExponent();
            modu = privateKey.getModulus();
            d = e.modInverse(phiN);

            set_time = System.nanoTime() - set_time2;  

         }
         catch(Exception e){
            System.out.println("\nError in Setup: " + e.getMessage());
        }

    }
    public BigInteger getModu()
    {
        System.out.println(modu);
        return modu;
    }


    public static void main(String[] args)throws Exception {
       A callpkg = new A();
       callpkg.Setup(2048);     
       callpkg.getModu();
       B callchild = new B();
       callchild.KeyDer();
    }
}

public class B extends A{

    long set_time,ext_time,ident_time;
    long set_time2,ext_time2,ident_time2;
    BigInteger id_hash,N,x,t,bigT,c,s;
    MessageDigest H,G;
    String TM;
    String ID = "email", M="Hello world";

    public void KeyDer()
    {
         try
         {                        
                SHIBS abc = new Function();
                ext_time2 = System.nanoTime();

                H = MessageDigest.getInstance("SHA-512");
                H.update(ID.getBytes());
                id_hash = new BigInteger(H.digest());
                A call = new A();
                BigInteger aaa = call.getModu();
                System.out.println(aaa);
                N = privateKey.getModulus();
                x = id_hash.modPow(d, N);

                ext_time = System.nanoTime() - ext_time2;
         }
         catch(Exception e){
            System.out.println("\nError in KeyDer: " + e.getMessage());
        }
    }
}

我是编程新手。第一次在网上发帖,抱歉有很多错误 NPE发生在System.out.println(" \ n在KeyDer中出错:" + e.getMessage()); 我需要使用N = privateKey.getModulus(),所以我必须调用privateKey.getModulus()或获取变量N.

2 个答案:

答案 0 :(得分:0)

您从未调用过setup()方法 因此,您的所有字段均为null

答案 1 :(得分:0)

您正在创建A的实例并调用setup - 这很好。

然后,您将创建一个扩展A的B实例。此扩展与首先创建的实例不同。所以有两个选择:

  1. setup()方法

    中调用B实例上的main
    B callchild = new B();
    callchild.Setup((2048);     
    callchild.getModu();
    callchild.KeyDer();
    
  2. B的构造函数中添加对setup()
  3. 的调用

    编辑以下内容适用于原始帖子

    privateKey是A类方法setup()的局部变量;

    制作变量RSAPrivateCrtKey privateKey或变量

    BigInteger p,q,phiN,e,d,modu; 
    

    有类范围,即在设置之外定义它们,并确保调用setUp()