?
Solved

Problems with: SunJCE

Posted on 2003-03-02
20
Medium Priority
?
2,457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

765 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