Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

Very weird Java error

Started by FreJa Mar 26, 2009 at 11:10 AM 0 replies 1.5k views
Original Post
FreJa
FreJa
Hi, I'm running into a very weird java exception. So, this is the .java:

import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.Cipher;

//
// Public Key cryptography using the RSA algorithm.
public class RSATest {

	public static void main (String[] args) throws Exception {

		//
	// check args and get plaintext
	if (args.length !=1) {
		System.err.println("Usage: java PublicExample text");
		System.exit(1);
	}
	byte[] plainText = args[0].getBytes("UTF8");

	//
	// generate an RSA key
	System.out.println( "\nStart generating RSA key" );
	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
	keyGen.initialize(1024);
	KeyPair key = keyGen.generateKeyPair();
	System.out.println( "Finish generating RSA key" );

	//
	// get an RSA cipher object and print the provider
	Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
	System.out.println( "\n" + cipher.getProvider().getInfo() );

	//
	// encrypt the plaintext using the public key
	System.out.println( "\nStart encryption" );
	cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
	byte[] cipherText = cipher.doFinal(plainText);
	System.out.println( "Finish encryption: " );

	System.out.println( ByteArrayUtils.writeByteArray(cipherText) );
	//
	// decrypt the ciphertext using the private key
	System.out.println( "\nStart decryption" );
	cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
	byte[] newPlainText = cipher.doFinal(cipherText);
	System.out.println( "Finish decryption: " );
	System.out.println( new String(newPlainText, "UTF8") );
	}
}

And the corresponding servlet:

import java.io.IOException;
import java.io.PrintWriter;
import java.security.KeyPair;
import java.security.KeyPairGenerator;

import javax.crypto.Cipher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class RSATestServlet extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

    }

    public void destroy() {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>RSA Test Servlet</title>");
        out.println("</head>");
        out.println("<body>");

		try {
/*			// add security provider
			java.security.Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
*/
			byte[] plainText = "cleartext".getBytes("UTF8");

			//
			// generate an RSA key
			out.println( "\nStart generating RSA key" + "
"); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair key = keyGen.generateKeyPair(); out.println( "Finish generating RSA key" + "
"); // // get an RSA cipher object and print the provider Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); out.println( "\n" + cipher.getProvider().getInfo() + "
" ); // // encrypt the plaintext using the public key out.println( "\nStart encryption" + "
" ); cipher.init(Cipher.ENCRYPT_MODE, key.getPublic()); byte[] cipherText = cipher.doFinal(plainText); out.println( "Finish encryption: " ); out.println( ByteArrayUtils.writeByteArray(cipherText) + "
"); // // decrypt the ciphertext using the private key out.println( "\nStart decryption" + "
" ); cipher.init(Cipher.DECRYPT_MODE, key.getPrivate()); byte[] newPlainText = cipher.doFinal(cipherText); out.println( "Finish decryption: " ); out.println( new String(newPlainText, "UTF8") + "
" ); } catch(Throwable t) { out.println(t); } out.println("</body>"); out.println("</html>"); out.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Now, running the application from the terminal (btw, I'm using MacOS 10.5), everything works fine. But running from safari, on tomcat, I get this: The error:

java.security.NoSuchAlgorithmException: Cannot find any provider supporting RSA/ECB/PKCS1Padding

And the root cause...

java.lang.NoClassDefFoundError: com/sun/crypto/provider/SunJCE
	java.lang.Class.getDeclaredConstructors0(Native Method)
	java.lang.Class.privateGetDeclaredConstructors(Class.java:2357)
	java.lang.Class.getConstructor0(Class.java:2671)
	java.lang.Class.newInstance0(Class.java:321)
	java.lang.Class.newInstance(Class.java:303)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:535)
	org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
	org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:790)
	org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:709)
	org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:572)
	org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
	java.lang.Thread.run(Thread.java:613)

Before you ask, I believe I do have my java.security correct:

...
security.provider.5=com.sun.crypto.provider.SunJCE
...
Any help? Many thanks
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Antheus
Antheus
Putting exception into google returns this.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.