我有以下一段servlet代码(在Apache Tomcat v7.0上运行),试图从jsp页面获取用户输入,然后尝试AES加密它(在我的类AES_BC中使用充气城堡实现)。我正在实例化我的AES_BC类,只是为了在方法myname()中进行测试。服务器很好,我可以提供一些输入,但是当在doGet中调用myname()时,我得到一个运行时异常。代码和例外情况如下:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import aes_bc.AES_BC;
@WebServlet(name = "EncryptServlet", urlPatterns = {"/Encrypt"})
public class EncryptServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
// reading the user input
String query= request.getParameter("color");
String color = "blue";
PrintWriter out = response.getWriter();
myname();
System.out.println("color:"+color);
System.out.println("user query:"+query);
byte [] in_bytes = query.getBytes();
String tsMsg = query + new SimpleDateFormat("yyyyMMddhhmm").format(new Date());
System.out.println("time stamped message:"+tsMsg+"\n");
out.println (
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" + "http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
"<head> \n" +
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> \n" +
"<title> Bouncy Castle Encryption/Crypto JS decryption </title> \n" +
"</head> \n" +
"<body> \n" +
"<font size=\"9px\" color=\"" + color + "\">" +
"Plain text query:" + query +
"</font> \n" +
"<li>" + "Timestamped query:" + tsMsg +
"</li> \n" +
"</body> \n" +
"</html>"
);
}
public void myname(){
byte[] enc_key = "0123456789abcdefghijklmn".getBytes();
AES_BC aesencrypt = new AES_BC(enc_key);
String msg1 = "test msg";
byte [] in_bytes = msg1.getBytes();
System.out.println("message:"+msg1);
}
}
例外情况如下:
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.NoClassDefFoundError: org/bouncycastle/crypto/BlockCipher
EncryptServlet.myname(EncryptServlet.java:59)
EncryptServlet.doGet(EncryptServlet.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
root cause
java.lang.ClassNotFoundException: org.bouncycastle.crypto.BlockCipher
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
EncryptServlet.myname(EncryptServlet.java:59)
EncryptServlet.doGet(EncryptServlet.java:26)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
当我将一个main方法添加到我的AES_BC类并独立测试时,它可以正常工作。我一直在摸索着弄清楚如何解决这个问题。任何帮助将不胜感激。
答案 0 :(得分:3)
您的异常非常明显:
java.lang.ClassNotFoundException: org.bouncycastle.crypto.BlockCipher
你的类路径中没有必要的jar,如果你正在使用maven,只需在compile
范围内添加一个依赖项,或者在互联网上找到丢失的jar并将其添加到你的{{1文件夹。
答案 1 :(得分:1)
下载此页面中提到的jar文件,把它放在你的/ Tomcat / webapps / YourApp / WEB-INF / lib中,我认为你的问题会解决,你不会有这个异常http://www.java2s.com/Code/Jar/l/Downloadlcryptojdk16143jar.htm