3
votes

I have NoClassDefFoundError after some time on my WebApps with BC classes:

java.lang.NoClassDefFoundError: org/bouncycastle/util/Pack  
  at org.bouncycastle.crypto.engines.AESFastEngine.unpackBlock(Unknown Source)
  at org.bouncycastle.crypto.engines.AESFastEngine.processBlock(Unknown Source)
  at org.bouncycastle.crypto.modes.CBCBlockCipher.decryptBlock(Unknown Source)
  at org.bouncycastle.crypto.modes.CBCBlockCipher.processBlock(Unknown Source)
  at org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher.processBytes(Unknown Source)
  at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher$BufferedGenericBlockCipher.processBytes(Unknown Source)
  at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineUpdate(Unknown Source)
  at javax.crypto.Cipher.update(DashoA13*..)...

After a tomcat restart, the error disappears and reappears after 1 or 2 days.

The BC jar hasn't been tampered with.

BC is registered and used like this :

// registration
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null)
{
  Security.addProvider(new BouncyCastleProvider());
}

SecretKey secretKey = new SecretKeySpec("_mykey__mykey__mykey__mykey__myk".getBytes(), "AES");
IvParameterSpec iv = new IvParameterSpec("_iv__iv__iv__iv_".getBytes());
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

System.out.println(cipher.getProvider()); // prints "BC version 1.53"

// encryption
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
cipher.update("clearContent".getBytes());
byte[] cipheredContent = cipher.doFinal();

// decryption
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
cipher.update(cipheredContent);
byte[] clearContent = cipher.doFinal();

System.out.println(new String(clearContent)); // prints "clearContent"

Here are the components :

  • java 6
  • tomcat 6
  • bcprov-jdk15on-153.jar in the WEB-INF/lib folder
  • jce6 unlimited strength

Am I missing something ?

looks like some horrible classloading issue. If your app has a starting point, I'd recommend you to add some code during the app startup to force the classloader to load BC classesLeo
@Leo tried it with no success when doing it in ServletContextListener.contextInitialized(ServletContextEvent event)neomega
it seems to me problem with your jar file. Download this jar file againGhayel
@Ghayel if it was a problem with the jar, it would never work. It works perfectly after the server start and start throwing the error after some days.neomega
I said it because if the jar was okay then it never through this error. may be some class or classes missing in this jarGhayel