Java Network

Encrypting/Decrypting text messages with Bouncy Castle Crypto API

Bouncy Castle Crypto API is a lightweight cryptography API in Java. It also have a J2ME version. This tip will give you an example of encryption and decryption of messages using Bouncy Castle Crypto API.

Let’s start with the utility class we will use in our midlet to encrypt/decrypt arbitrary text messages. Encryptor class below allows you to encrypt and decrypt arbitrary messages using encryptString and decryptString methods:

Also note at the time of this writing that some version of Bouncy Castle Crypto API is not compiled for MIDP 2.0. When you tried to compile this example with Bouncy Castle Crypto API, you may get errors. (Some of the test classes use some classes not available to J2ME which is only available to J2SE) In that case, you may try other versions of the API to eliminate this problem.

If the above method still does not work for you, we recommend you to add full source code of the API to your project and eliminate problems one by one by removing some test classes and removing unnecessary but problematic parts of the library. (Note that you should also check the license before distributing the changes you made!)