new_c_user
new_c_user

Reputation: 133

Run time exception using jsp servlet and bouncy castle

I have the following piece of servlet code (running on Apache Tomcat v7.0) that tries to get user input from a jsp page and subsequently try to AES encrypt it (implemented in my class AES_BC using bouncy castle). I'm instantiating my AES_BC class just for testing in the method myname(). The server comes up fine and I can supply some input, but when myname() is invoked in doGet, I get a run time exception. The code and exception are given below:

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);
      }
    }

The exception looks like the following:

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)

When I add a main method to my AES_BC class and test it standalone, it works absolutely fine. I have been scratching my head to figure out how to fix this. Any help will be greatly appreciated.

Upvotes: 0

Views: 437

Answers (2)

Paulius Matulionis
Paulius Matulionis

Reputation: 23413

Your exception is pretty obvious:

java.lang.ClassNotFoundException: org.bouncycastle.crypto.BlockCipher

You haven't got necessary jar in your classpath, if you are using maven just add a dependency with the compile scope or otherwise find the missing jar on the internet and add it to your WEB-INF/lib folder.

Upvotes: 3

Alaa Abuzaghleh
Alaa Abuzaghleh

Reputation: 1009

download the jar file mention in this page, put it in you /Tomcat/webapps/YourApp/WEB-INF/lib and I think you problem will solved at leat you will not have this exception http://www.java2s.com/Code/Jar/l/Downloadlcryptojdk16143jar.htm

Upvotes: 1

Related Questions