1

我几乎完成了我的 DH 密钥交换应用程序。我现在正在尝试在我的应用程序上验证我的客户端。但是,我的服务器上不断出现空指针异常。我将问题追溯到我的大整数变量,但我没有看到任何问题。

我正在尝试向客户端发送 1 或 0 的响应。1 表示已认证,0 表示未认证

我将显示发生错误的部分

DHServlet.java

public class DHServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String pk2;
private static String pCValue;
private static String gCValue;
private static String sCValue;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
   // PrintWriter out2=response.getWriter();
    DH getDH = new DH();



    try {
    String un,pw,pk;

    Random randomGenerator = new Random();

    long pValue = randomGenerator.nextInt(100000000);
    long gValue = randomGenerator.nextInt(100000000);

    boolean checkPrime = getDH.isPrime(pValue);

    while(checkPrime == false)
    {
        long generatePValue2 = randomGenerator.nextInt(10000000);
        boolean checkPrime2 = getDH.isPrime(generatePValue2);
        if(checkPrime2==true)
        {
            pValue=generatePValue2;
            break;
        }
    }

   long checkSP = (pValue*2)+1;
   boolean checkSafePrime = getDH.isPrime(checkSP);

   while(checkSafePrime == false)
   {
       long generatePValue3 =  randomGenerator.nextInt(100000000);
       boolean checkSpInLoop = getDH.isPrime(generatePValue3);
       long checkSP2 = (generatePValue3*2)+1;
       boolean checkSafePrimeInLoop = getDH.isPrime(checkSP2);

       if(checkSpInLoop ==  true && checkSafePrimeInLoop == true)
       {
           pValue = generatePValue3;
           break;
       }
   }

   while(gValue>pValue)
   {
       long gValue2=randomGenerator.nextInt(100000000);
       if(gValue2<pValue)
       {
           gValue=gValue2;
           break;
       }
   }

   long getDivisor =(pValue-1)/2;
   BigInteger bi1,bi2,bi3,bi4;

   bi1=BigInteger.valueOf(getDivisor);
   bi2=BigInteger.valueOf(pValue);
   bi3=BigInteger.valueOf(gValue);
   bi4=bi3.modPow(bi1,bi2);

   long calculatedValue = bi4.longValue();

   while(calculatedValue == 1)
   {
       long gValue3=randomGenerator.nextInt(100000000);
       long getDivisorInLoop = (pValue-1)/2;
       BigInteger bi5,bi6,bi7,bi8;

       bi5=BigInteger.valueOf(getDivisorInLoop);
       bi6=BigInteger.valueOf(pValue);
       bi7=BigInteger.valueOf(gValue3);
       bi8=bi7.modPow(bi5, bi6);

       long calculatedValueInLoop = bi8.longValue();

       if(calculatedValueInLoop != 1)
       {
           gValue=gValue3;
           break;
       }

   }

   BigInteger generatorValue,primeValue,skA,biPKA;
   generatorValue = BigInteger.valueOf(gValue);
   primeValue = BigInteger.valueOf(pValue);
   long secretKeyA = getDH.generateSKA();
   skA=BigInteger.valueOf(secretKeyA);
   biPKA =getDH.generatePkA(generatorValue, primeValue, secretKeyA);
   System.out.println("The public key of server is "+biPKA);
   System.out.println("The generator value is "+generatorValue);
   System.out.println("The prime value is "+primeValue);
   System.out.println("the secret key of the server is "+skA);

   long PKA = biPKA.longValue();

   out.print(pValue+":"+gValue+":"+PKA);


   pk2=biPKA.toString();
   pCValue=primeValue.toString();
   gCValue=generatorValue.toString();
   sCValue=skA.toString();




    } finally {            
        out.close();
    }
}



protected void processRequest2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    DH getDH2=new DH();
    String un,pw,pk,shareK;

    try
    {
        pk=request.getParameter("publicK");
        un=request.getParameter("username");
        pw=request.getParameter("password");
        shareK=request.getParameter("sharedKey");

        String publicKey = pk;

        BigInteger biPK,biSK,biP,sKey;
        biPK = new BigInteger(publicKey);
        biSK = new BigInteger(sCValue);
        biP = new BigInteger(pCValue);

        System.out.println(biPK);
        sKey = getDH2.calculateSharedKey(biPK, biSK, biP);
        String sharedServerKey = (sKey.toString()).trim();
        System.out.println("the shared key is "+sharedServerKey);
        if(un.equalsIgnoreCase("larry")&& pw.equals("123") && sharedServerKey == shareK)
        {
            out.print(1);

        }
        else
        {
            out.print(0);
        }

    }finally
    {
        out.close();
    }



}




/**
 * @see HttpServlet#HttpServlet()
 */
public DHServlet() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest(request,response);
    processRequest2(request,response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    processRequest2(request,response);
}

}

我的Tomcat日志:

    Jul 12, 2013 10:10:07 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [DHServlet] in context with path [/MyServletProject] threw exception
java.lang.NullPointerException
    at java.math.BigInteger.<init>(Unknown Source)
    at java.math.BigInteger.<init>(Unknown Source)
    at DHServlet.processRequest2(DHServlet.java:158)
    at DHServlet.doGet(DHServlet.java:202)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

希望你们能帮我解决这个问题,我只是难住了。提前致谢。

编辑:我更新了我的 servlet 代码。

4

5 回答 5

2

例外很明显:

    pk=request.getParameter("publicK");
    un=request.getParameter("username");
    pw=request.getParameter("password");
    shareK=request.getParameter("sharedKey");

    BigInteger biPK,biSK,biP,sKey;
    biPK = new BigInteger(pk);
    biSK = new BigInteger(sCValue);
    biP = new BigInteger(pCValue);

您得到 aNullPointerException因为您的任何变量pk, sCValue,pCValue都是null

顺便说一句,你的变量在哪里sCValuepCValue初始化?

于 2013-07-12T14:19:10.997 回答
1

您的堆栈跟踪表明您的 BigInteger 之一有问题(pk、un 或 pw 之一为空)

于 2013-07-12T14:19:02.067 回答
0

在这种情况下,会发生 NullPointerException,因为您将 的值传递null给 的构造函数BigInteger

堆栈跟踪会告诉您调用它的行号:DHServlet.java:158.

假设该行如下

biPK = new BigInteger(pk);

然后pk为空。

于 2013-07-12T14:19:13.947 回答
0

您可能正在使用nullString 来实例化BigInteger.Any 或 All 的请求参数是null.

pk=request.getParameter("publicK");
un=request.getParameter("username");
pw=request.getParameter("password");
于 2013-07-12T14:19:55.823 回答
0

这些变量之一:pk, sCValue or pCValuenull因为我不知道第158Servlet是什么?

您不能将 a 实例化为BigIntegernull 作为参数。

于 2013-07-12T14:20:27.083 回答