String encryption with Jasypt library

Tian picture Tian · Oct 25, 2010 · Viewed 10.6k times · Source

I want to encrypt a string, but the standard Java libraries are too complicated for me.

So I turned to Jasypt labriry. It's pretty simple to use and understan. However when I import the library to Eclipse 3.6 and when I try encrypt a string like "Hello" with the password "123", it always comes up with an error. I'm not sure what I'mm doing wrong but I think it also happens when I use other libraries in Eclipse.

Source:

import org.jasypt.util.text.BasicTextEncryptor;

public class Main {

    static BasicTextEncryptor textEncryptor = new BasicTextEncryptor();

    public static void main(String[] args) {
        System.out.println("Hello World");
        textEncryptor.setPassword("123");
        System.out.println(textEncryptor.encrypt("Hello World"));
    }
}

The error message:

java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
 at java.lang.ClassLoader.defineClass1(Native Method)
 at java.lang.ClassLoader.defineClassCond(Unknown Source)
 at java.lang.ClassLoader.defineClass(Unknown Source)
 at java.security.SecureClassLoader.defineClass(Unknown Source)
 at java.net.URLClassLoader.defineClass(Unknown Source)
 at java.net.URLClassLoader.access$000(Unknown Source)
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at org.jasypt.util.text.BasicTextEncryptor.<init>(BasicTextEncryptor.java:67)
 at eMain.<clinit>(eMain.java:4)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 ... 14 more

Answer

aioobe picture aioobe · Oct 25, 2010

The library you imported depends on another library containing org/apache/commons/lang/exception/NestableRuntimeException. This is located in the Apache Commons Lang library.

In fact, if you downloaded JASYPT from http://sourceforge.net/projects/jasypt/files/ you'll get a zip file containing a lib-folder with these files:

  • commons-codec-1.1.jar
  • commons-lang-2.1.jar
  • jasypt-1.6.jar

You should include all of these in your project.

I tried it and your little sample program works fine (and prints the following)

Hello World
v09l9j/BIeSoMkQXc2CY0VIJLlLAQTYq