How to use Bouncy Castle Lightweight Cryptographic API in NetBeans for J2ME (midlets) projects

The Bouncy Castle Crypto API for Java provides a lightweight cryptography API that works with everything from the J2ME to the JDK 1.6 platform.

The API provides cryptographic functions for Java JDK 1.1 to 1.6 applications and for J2ME (mobile applications) midlets. The API can be downloaded from the Bouncy Castle latest releases page. A common mistake is to download and use the API for the JDK (bcprov-jdk16–145.jar for example) and not for J2ME. In this scenario the API will require some Java functions that are not available on the J2ME platform. In most cases you will receive a preverify error:

Error preverifying class org.bouncycastle.asn1.ASN1InputStream
java/lang/NoClassDefFoundError: java/io/FilterInputStream

You could avoid this error by using the NetBeans obfuscator and some additional settings like the ones from the end of this post. But, this could take time, and the solution is realy simple.

The API for the J2ME platform is also on the Bouncy Castle latest releases page, in the Sources and Javadoc category. You recognize the zip archive because it has the j2me word in its name –

In order to use Bouncy Castle Crypto API crypto libraries for J2ME applications you have to add two zip files


to your project using Project settings -> Libraries and Resources -> Add jar/zip. The 2 zip files are part of (available on the Bouncy Castle latest releases page –  last checked on September2010) in the zips folder:

  • lcrypto-j2me-145/zips/
  • lcrypto-j2me-145/zips/

After that go to Project Properties -> Build -> Obfuscation -> Select High Obfuscation Level (level 9) and add following to Additional Obfuscation Settings.

  • -dontskipnonpubliclibraryclasses
  • -dontskipnonpubliclibraryclassmembers
  • -ignorewarnings
NetBeans Obfuscation Options
NetBeans Obfuscation Options

By obfuscation you also reduce the size of the distribution jar, because classes that are not used are not included in the built.