我差不多完成了我的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代码。
答案 0 :(得分: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);
您收到NullPointerException
,因为您的任何变量pk
,sCValue
,pCValue
都是null
。
顺便说一句,您的变量sCValue
和pCValue
在何处初始化?
答案 1 :(得分:1)
你的stacktrace说你的BigIntegers有一个问题(pk,un或pw中的一个是null)
答案 2 :(得分:0)
在这种情况下会发生NullPointerException,因为您将值null
传递给BigInteger
的构造函数。
stacktrace会告诉您调用它的行号:DHServlet.java:158
。
假设该行是以下
biPK = new BigInteger(pk);
然后pk
为空。
答案 3 :(得分:0)
您可能正在使用null
字符串来实例化BigInteger
。任何或所有请求参数都是null
。
pk=request.getParameter("publicK");
un=request.getParameter("username");
pw=request.getParameter("password");
答案 4 :(得分:0)
其中一个变量:pk, sCValue or pCValue
是null
,因为我不知道Servlet
中的 158 行是什么?
您不能将BigInteger
实例化为null作为参数。