Link to home
Create AccountLog in
Java

Java

--

Questions

--

Followers

Top Experts

Avatar of efratb
efratb

Problems with: SunJCE
Hi,

I have a java application, from which I launch a web application. While doing it I encode the name & password in the following way:

Provider sunJce = new SunJCE();
Security.addProvider(sunJce);
byte[] passwordInBytes = password.getBytes("UTF-8");
skspec = new SecretKeySpec(passwordInBytes, "Blowfish");
cipher = javax.crypto.Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
...

And I decode it in a servlet on the web-side.
It works fine from the java application (it launches the URL & the encrypted parameters), but when trying to decode it on the web application I get the following exception:
java.lang.NoClassDefFoundError: com/sun/crypto/provider/SunJCE
(I fail when trying to do: CipherEncryption cipher = new CipherEncryption(key);)

I tried to copy the sunjce_provider.jar from the jsdk to the lib directories of the tomcat & my web applicatoin, but then I get "method not found" exeptions.

Do you have any idea what could be the problem?

Thanks,
Efrat


               

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of tuxmaniactuxmaniac

set CLASSPATH to include the JCE jar files.The JCE comes with a provider called SunJCE.


You have 2 ways to do it :

Way 1:

You have to add a line like the one that follows:

security.provider.n=com.sun.crypto.provider.SunJCE

where n is one more than the last security.provider entry in the ".\lib\security\java.security" file of your server jdk

Way 2:

You can add providers dynamically (such as, at runtime) using code like the following:

try {
     Security.addProvider(new com.sun.crypto.provider.SunJCE());
    }
    catch(Exception e) {
     System.err.println("Error loading security provider (" +
                  e.getMessage() + ")");
   }


Avatar of girionisgirionis🇬🇷

 You can also take a look here:

http://java.sun.com/products/jce/jce122_install.html

Avatar of efratbefratb

ASKER

Hi,

Thanks for your help, but it didn't work.
I tried to add the provider in the java.security, or in a dynamic way in the code, but it didn't work.

I'm using tomcat, so I tried to copy the following jars to the lib directory at the tomcat-home: US_export_policy.jar, sunjce_provider.jar, local_policy.jar & jce.jar. But it didn't help.
(I still get NoSuchMethodError or NoClassDefFound).

Maybe I don't have the most updated jars? (I copied the jar files from the j2sdk1.4.0 that I'm using to the tomcat lib directory).
Do you have any idea?
 
Thanks,
Efrat  

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of girionisgirionis🇬🇷

 Which version of Tomcat are you using? Are you getting the NoSuchMethod.. or the NoClassDef... exception (or both)? It seems to me that the NoSuchMethodError refers to an older/newer verson of the package you are trying to use. Have you checked if Tomcat comes with its own provider?

Avatar of efratbefratb

ASKER

I'm using tomcat 3.2 (Yes, I know it's very old...)
At first I added to its classpath only the jce.jar & the sunjce_provider.jar and then I got a NoClassDefError. After I added the 2 other policy jars to the tomcat classpath I get:
java.lang.NoSuchMethodError
     at javax.crypto.SunJCE_d.a(DashoA6275)
     at javax.crypto.SunJCE_d.a(DashoA6275)
     at javax.crypto.SunJCE_d.verify(DashoA6275)
     at javax.crypto.SunJCE_b.f(DashoA6275)
     at javax.crypto.SunJCE_b.(DashoA6275)
     at javax.crypto.Cipher.getInstance(DashoA6275)

Thanks a lot,
Efrat

Avatar of girionisgirionis🇬🇷

It's always godo not to mix jar files. Can you just add the jce.jar and sunjce_provider.jar in your classpath (or TOmcat's /lib or java's /ext folder) and post the exact NoClassDef error?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Mick BarryMick Barry🇦🇺

Try changing the tomcat startup script to include all 4 jar files in tomcats classpath.

Avatar of efratbefratb

ASKER

For some reason, I can't reproduce the noClassDefError, even when I remove the 2 policy jars I get methodNotFound.

I changed the tomcat statup script (batch file) to include the 4 jars as follows:

:staticClasspath
echo Setting your CLASSPATH statically.
if exist "%TOMCAT_HOME%\lib\ant.jar" set CP=%CP%;%TOMCAT_HOME%\lib\ant.jar
if exist "%TOMCAT_HOME%\lib\jasper.jar" set CP=%CP%;%TOMCAT_HOME%\lib\jasper.jar
if exist "%TOMCAT_HOME%\lib\jaxp.jar" set CP=%CP%;%TOMCAT_HOME%\lib\jaxp.jar
if exist "%TOMCAT_HOME%\lib\parser.jar" set CP=%CP%;%TOMCAT_HOME%\lib\parser.jar
if exist "%TOMCAT_HOME%\lib\servlet.jar" set CP=%CP%;%TOMCAT_HOME%\lib\servlet.jar
if exist "%TOMCAT_HOME%\lib\webserver.jar" set CP=%CP%;%TOMCAT_HOME%\lib\webserver.jar
if exist "%TOMCAT_HOME%\lib\jce.jar" set CP=%CP%;%TOMCAT_HOME%\lib\jce.jar
if exist "%TOMCAT_HOME%\lib\US_export_policy.jar" set CP=%CP%;%TOMCAT_HOME%\lib\US_export_policy.jar
if exist "%TOMCAT_HOME%\lib\local_policy.jar" set CP=%CP%;%TOMCAT_HOME%\lib\local_policy.jar
if exist "%TOMCAT_HOME%\lib\sunjce_provider.jar" set CP=%CP%;%TOMCAT_HOME%\lib\sunjce_provider.jar

That's the only change I did in tomcat files. Should I change it in another place?
(I also work with Apache, but I guess it has nothing to do with it).

Thanks a lot!
Efrat

Avatar of Mick BarryMick Barry🇦🇺

> I copied the jar files from the j2sdk1.4.0

This could be the problem. Try instead downloading the standalone version of JCE and using it instead.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of efratbefratb

ASKER

For some reason, I can't reproduce the noClassDefError, even when I remove the 2 policy jars I get methodNotFound.

I changed the tomcat statup script (batch file) to include the 4 jars as follows:

:staticClasspath
echo Setting your CLASSPATH statically.
if exist "%TOMCAT_HOME%\lib\ant.jar" set CP=%CP%;%TOMCAT_HOME%\lib\ant.jar
if exist "%TOMCAT_HOME%\lib\jasper.jar" set CP=%CP%;%TOMCAT_HOME%\lib\jasper.jar
if exist "%TOMCAT_HOME%\lib\jaxp.jar" set CP=%CP%;%TOMCAT_HOME%\lib\jaxp.jar
if exist "%TOMCAT_HOME%\lib\parser.jar" set CP=%CP%;%TOMCAT_HOME%\lib\parser.jar
if exist "%TOMCAT_HOME%\lib\servlet.jar" set CP=%CP%;%TOMCAT_HOME%\lib\servlet.jar
if exist "%TOMCAT_HOME%\lib\webserver.jar" set CP=%CP%;%TOMCAT_HOME%\lib\webserver.jar
if exist "%TOMCAT_HOME%\lib\jce.jar" set CP=%CP%;%TOMCAT_HOME%\lib\jce.jar
if exist "%TOMCAT_HOME%\lib\US_export_policy.jar" set CP=%CP%;%TOMCAT_HOME%\lib\US_export_policy.jar
if exist "%TOMCAT_HOME%\lib\local_policy.jar" set CP=%CP%;%TOMCAT_HOME%\lib\local_policy.jar
if exist "%TOMCAT_HOME%\lib\sunjce_provider.jar" set CP=%CP%;%TOMCAT_HOME%\lib\sunjce_provider.jar

That's the only change I did in tomcat files. Should I change it in another place?
(I also work with Apache, but I guess it has nothing to do with it).

Thanks a lot!
Efrat

Avatar of efratbefratb

ASKER

I downloaded jce_1_2_2. It includes the jce1_2_2.jar and the 3 other jars (local_policy.jar, sunjce_provider.jar & US_export_policy.jar).

I copied the 4 files to my tomcat-home/lib directory, and changed the tomcat classpath in the tomcat-startup script accordingly.

I still get a similar error:
java.lang.NoSuchMethodError
     at javax.crypto.SunJCE_b.(DashoA6275)
     at javax.crypto.Cipher.a(DashoA6275)
     at javax.crypto.Cipher.getInstance(DashoA6275)

Did I download the right files?

Thanks,
Efrat  

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of efratbefratb

ASKER

I saw that the requirements for jce  1.2.2 are:
JavaTM 2 SDK v 1.2.1, v 1.2.2, or v 1.3.x
JavaTM 2 Runtime Environment v 1.2.1, v 1.2.2, or v 1.3.x

I'm using j2sdk1.4.0 & jre1.4.0. Could that be the problem?
(I don't think it is, becase it works fine on the java application, I have problems with the web application).

Thanks...

ASKER CERTIFIED SOLUTION
Avatar of Mick BarryMick Barry🇦🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Mick BarryMick Barry🇦🇺

> Could that be the problem?

yes. 1.4 includes jce so you don't need to install anything.

Avatar of efratbefratb

ASKER

Objects,
You were right, I had both 1.3 & 1.4 installed on my computer, and the classpath used the 1.3.
I changed it and now everything works fine.

Thanks a lot!
Efrat

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Mick BarryMick Barry🇦🇺

Good to hear you got it fixed :-)

Avatar of efratbefratb

ASKER

I was happy too early...
It works ok on PC, but on UNIX I get the following exception when trying to decrypt on the servlet side:

javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)

Do you have any idea what could be the problem?

Thanks,
Efrat

Avatar of Mick BarryMick Barry🇦🇺

How do you pass the bytes?

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of efratbefratb

ASKER

Well, I tried many versions... Nothing worked.
Here's the class I'm using:

public class CipherEncryption
{
  private SecretKeySpec skspec;
  private Cipher cipher;
  private BASE64Decoder decoder = new BASE64Decoder();
  private BASE64Encoder encoder = new BASE64Encoder();

  public CipherEncryption(String password)
  {
    try
    {
      byte[] passwordInBytes = password.getBytes();
      skspec = new SecretKeySpec(passwordInBytes, "Blowfish");
      cipher = javax.crypto.Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  public byte[] encrypt(byte[] plain)
  {
    try
    {
      cipher.init(javax.crypto.Cipher.ENCRYPT_MODE,skspec);
      return cipher.doFinal(plain);
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }
  public byte[] decrypt(byte[] ciphered)
  {
    try
    {
      cipher.init(javax.crypto.Cipher.DECRYPT_MODE,skspec);
      return cipher.doFinal(ciphered);
    }
    catch(Exception e)
    {
      e.printStackTrace();
      return null;
    }
  }
  public BASE64Decoder getDecoder()
  {
    return decoder;
  }

  public BASE64Encoder getEncoder()
  {
    return encoder;
  }


From the gui application, when I encrypt I do:

                CipherEncryption cipher = new CipherEncryption(key);
                byte[] nameEncryptedArray = cipher.encrypt(nocController.getUserName().getBytes("UTF-8"));
                String nameEncodedString = cipher.getEncoder().encode(nameEncryptedArray);

And pass the nameEncodedString to the servlet. In the servlet I try to decode in the following way:

       CipherEncryption cipher = new CipherEncryption(key);

       String decodedName = new String(cipher.getDecoder().decodeBuffer(userName));
       byte[] decryptedNameArray = cipher.decrypt(decodedName.getBytes());
       userName = new String(decryptedNameArray);

(I'm using the same key in both applications).

I understood that the problem is caused because of the way I'm converting the byte[] to String and the other way around. I tried to write a convertor and do all the conversions myself, but I still got that padding exception.
Any idea?? Please...

Thanks a lot,
Efrat  


Java

Java

--

Questions

--

Followers

Top Experts

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.