views:

929

answers:

3

Hi friends,

I am trying to write a small application using bouncycastle algorithm, from the http://tinyurl.com/ylclavn (BouncyCastleProvider.java) it says we have to import and add the provider during runtime by the following code

import org.bouncycastle.jce.provider.BouncyCastleProvider; Security.addProvider(new BouncyCastleProvider());

error - The import org.bouncycastle cannot be resolved; during import error - BouncyCastleProvider cannot be resolved to a type; when calling addProvider

I though bouncycastle is not provided with the Android 1.6 SDK, so thought of installing separately. how should i do this? If Bouncycastle is shipped along with SDK, what should i do to avoid these errors? I am using Android 1.6, eclipse-V3.4.0 on winXP . Thanks in advance

+1  A: 

I'm not familiar with this particular library. However, here are general instructions on how to include a library delivered as a 'jar' file into an Android project.

Download the jar file and put it somewhere on your workstation. You may want to put in the root directory of the project your are installing it in, or maybe in a 'lib' directory in the root.

In Eclipse, select Project->Properties, then select Java Build Path. Then click Add External Jars, navigate to where you put the .jar file, select it and click Open.

Now type or paste some code that attempts to use the classes in the jar. If you are lucky a light bulb icon will appear in the left margin. Clicking on this will prompt you to add the correct Import statement to the top of your .java file.

There are still things that can go wrong at this point. The library may make use of java.* or javax.* content not supplied by Android (it has just a subset of these libraries). Also it may have additional library dependencies of its own. There are other reasons why the .jar may not be compatible with the Android platform.

Note also that it will increase the size of your .apk to accommodate the new content.

Jim Blackler
i tried to add the bouncy castle jar and tried to getinstance from secretKeyFactory but it throws an exception no such Algorithm
Vamsi
That sounds like a local exception to that library, not a library inclusion issue.
Jim Blackler
+1  A: 

You shouldn't need to explicitly add BouncyCastle as a provider. As you say, it's already included with Android.

Here's what I do to get a BouncyCastle AES cipher,

SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC");

If you look in BouncyCastleProvider.java you'll see a reference to PBEWithSHA256And256BitAES-CBC-BC along with a few other ciphers provided by BouncyCastle.

Adrian
it throws an exception NoSuchAlgorithm
Vamsi
Strange. I'm not sure what the problem is. Here's the very code I run, http://upm.svn.sourceforge.net/viewvc/upm/android/trunk/src/com/u17od/upm/crypto/EncryptionService.java?view=markup. Works on Android 1.5 and 2.1.
Adrian
A: 

If BoncyCastleProvider is not in every release of Android.

You should check this backward-compatibility article.

Macarse