Java
--
Questions
--
Followers
Top Experts
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(sunJc
byte[] passwordInBytes = password.getBytes("UTF-8")
skspec = new SecretKeySpec(passwordInBy
cipher = javax.crypto.Cipher.getIns
...
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.NoClassDefFoundE
(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.
You have 2 ways to do it :
Way 1:
You have to add a line like the one that follows:
security.provider.n=com.su
where n is one more than the last security.provider entry in the ".\lib\security\java.secur
Way 2:
You can add providers dynamically (such as, at runtime) using code like the following:
try {
Security.addProvider(new com.sun.crypto.provider.Su
}
catch(Exception e) {
System.err.println("Error loading security provider (" +
e.getMessage() + ")");
}
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






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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.NoSuchMethodErro
at javax.crypto.SunJCE_d.a(Da
at javax.crypto.SunJCE_d.a(Da
at javax.crypto.SunJCE_d.veri
at javax.crypto.SunJCE_b.f(Da
at javax.crypto.SunJCE_b.(Das
at javax.crypto.Cipher.getIns
Thanks a lot,
Efrat

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.
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
if exist "%TOMCAT_HOME%\lib\jasper.
if exist "%TOMCAT_HOME%\lib\jaxp.ja
if exist "%TOMCAT_HOME%\lib\parser.
if exist "%TOMCAT_HOME%\lib\servlet
if exist "%TOMCAT_HOME%\lib\webserv
if exist "%TOMCAT_HOME%\lib\jce.jar
if exist "%TOMCAT_HOME%\lib\US_expo
if exist "%TOMCAT_HOME%\lib\local_p
if exist "%TOMCAT_HOME%\lib\sunjce_
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
This could be the problem. Try instead downloading the standalone version of JCE and using it instead.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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
if exist "%TOMCAT_HOME%\lib\jasper.
if exist "%TOMCAT_HOME%\lib\jaxp.ja
if exist "%TOMCAT_HOME%\lib\parser.
if exist "%TOMCAT_HOME%\lib\servlet
if exist "%TOMCAT_HOME%\lib\webserv
if exist "%TOMCAT_HOME%\lib\jce.jar
if exist "%TOMCAT_HOME%\lib\US_expo
if exist "%TOMCAT_HOME%\lib\local_p
if exist "%TOMCAT_HOME%\lib\sunjce_
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
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.NoSuchMethodErro
at javax.crypto.SunJCE_b.(Das
at javax.crypto.Cipher.a(Dash
at javax.crypto.Cipher.getIns
Did I download the right files?
Thanks,
Efrat

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.
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...
yes. 1.4 includes jce so you don't need to install anything.
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






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
It works ok on PC, but on UNIX I get the following exception when trying to decrypt on the servlet side:
javax.crypto.BadPaddingExc
at com.sun.crypto.provider.Bl
at com.sun.crypto.provider.Bl
at javax.crypto.Cipher.doFina
Do you have any idea what could be the problem?
Thanks,
Efrat

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.
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(passwordInBy
cipher = javax.crypto.Cipher.getIns
}
catch(Exception e)
{
e.printStackTrace();
}
}
public byte[] encrypt(byte[] plain)
{
try
{
cipher.init(javax.crypto.C
return cipher.doFinal(plain);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
public byte[] decrypt(byte[] ciphered)
{
try
{
cipher.init(javax.crypto.C
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(nocControll
String nameEncodedString = cipher.getEncoder().encode
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()
byte[] decryptedNameArray = cipher.decrypt(decodedName
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
--
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.