Setting Eclipse up for BouncyCastle Crypto

Apr 21, 2008

I spent a very long time fighting with Eclipse and the EclipseME plugin in order to do a J2ME app using the Bouncy Castle crypto libraries. Here are the steps that finally worked for me in Ubuntu 7.04 using Eclipse 3.1, EclipseME and the latest Bouncy Castle:

  1. Make sure the source folders and output folders are set to "src" and "bin" respectively. This is important: my habit is to use the project folder for both source and class files, but your built packages will have errors if you do this. EclipseME seems to put all the contents of the "source" directory (which, if it's the package directory, includes things like "deployed" and ".settings" and other stuff) in your jar, which causes problems.

    • Go to: Project -> Java Build Path -> Source. Click "Add Folder", and select "src" (create it if you need to). Remove the project folder from the build path.
    • Next, select "projectFolder/bin" as the default output folder.
    • Go to Window -> Preferences -> J2ME. Set "bin/deployed" as the deployment directory.
  2. Install and set up the BouncyCastle crypto library.

    • Download the bouncycastle j2me files from http://www.bouncycastle.org/latestreleases.html. The easiest way is to download the complete package (named something like "crypto-139.tar.gz"). Expand the archive, and look for the file "cldcclasses.zip". This is the library the J2ME apps will use.
    • Add the clcd_classes.zip library to your project: Project -> Properties -> Java Build Path -> Libraries.
    • Be sure to check "cldc_classes.zip" under "Order and Export" in Project -> Properties -> Java Build Path. It must be built with your package for obfuscation, etc. to work.
  3. Set up obfuscation. BouncyCastle includes some classes that are reimplementations of system classes (such as java.security.SecureRandom and java.lang.BigInteger). You will receive runtime security errors if your application tries to add these classes to the system. To avoid this, it is necessary to obfuscate the classes (which renames them, and places them in the default package).

    • Install ProGuard. Note that EclipseME doesn't seem to work right with proguard from the debian/ubuntu package repository, you probably have to download it manually from http://proguard.sourceforge.net. Extract the archive, and set up the ProGuard preferences in eclipse.
    • Go to: Window -> Preferences -> J2ME -> Packaging -> Obfuscation. Under "Proguard Root Directory", put the root directory of the proguard files downloaded from sourceforge (it should contain "lib", "src", "examples", "docs", etc). While there, also check the box so that the specified arguments include "-dontusemixedcaseclassnames -dontnote -defaultpackage ''". Ensure that "Proguard Keep Expressions" includes "public class * extends javax.microedition.midlet.MIDlet".
  4. Now you are ready to write your crypto code! But note that your development process and debugging are now different. Because bouncycastle depends on obfuscation for the code to run at all, and obfuscation only runs during the "packaging" stage, you can no longer simply run your emulated MIDlet with the WTK emulator to debug. Instead, you must use the following steps to test your program:

    • Right-click on the project folder, and select "J2ME -> Create Obfuscated Package".
    • Select "Run" from the "Run" menu (the first time, you can't just do "Run last launched" - you need to edit the configuration). Check the "Jad URL" radio button, and put in the path to the built JAD file (project/bin/deployed/yourJad.jad). Finally, click "Run", and you can run your project. The emulator will start listing the applications present in your JAD/JAR, and you have to launch one to test it.

Hi, I think your example is excellent, I can not wait for the full code to be posted.

I have and incomplete example to generate Public(Certificate) and Private Keys using PHP,

And then reading in the Public key into J2ME, and doing some encryption.

My Decryption is not working yet, since I am unable to read the private key.

My Encryption might not work as the encrypted TEXT has not been decrypted in PHP.

I am just stuck and too pressed for time; ( I have to move on to something easier )

If you want my existing code to see if you can solve the remainder of the puzzle, please let me know. (wayneforrest@gmail.com)

Regards.

Comments are closed for this entry.