?
Solved

Problems with: SunJCE

Posted on 2003-03-02
20
Medium Priority
?
2,523 Views
Last Modified: 2013-11-24
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


               
0
Comment
Question by:efratb
  • 9
  • 7
  • 3
  • +1
20 Comments
 

Expert Comment

by:tuxmaniac
ID: 8051586
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() + ")");
   }

0
 
LVL 35

Expert Comment

by:girionis
ID: 8051685
 You can also take a look here:

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

Author Comment

by:efratb
ID: 8051887
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  
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 8051999
 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?
0
 

Author Comment

by:efratb
ID: 8052127
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 8052931
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 8055009
Try changing the tomcat startup script to include all 4 jar files in tomcats classpath.
0
 

Author Comment

by:efratb
ID: 8055307
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
0
 
LVL 92

Expert Comment

by:objects
ID: 8055630
> 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.
0
 

Author Comment

by:efratb
ID: 8055638
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
0
 
LVL 92

Expert Comment

by:objects
ID: 8055697
0
 

Author Comment

by:efratb
ID: 8055716
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  
0
 

Author Comment

by:efratb
ID: 8055741
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...
0
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 8055769
seems like u may have some clash with 1.3 and 1.4, do you have both installed on the same machine?
0
 
LVL 92

Expert Comment

by:objects
ID: 8055779
> Could that be the problem?

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

Author Comment

by:efratb
ID: 8056297
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
0
 
LVL 92

Expert Comment

by:objects
ID: 8060812
Good to hear you got it fixed :-)
0
 

Author Comment

by:efratb
ID: 8063662
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
0
 
LVL 92

Expert Comment

by:objects
ID: 8069723
How do you pass the bytes?
0
 

Author Comment

by:efratb
ID: 8070344
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  


0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question