jQuery in Action, 2nd edition*
The moose likes Java Micro Edition and the fly likes Running Bouncy Castle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Running Bouncy Castle" Watch "Running Bouncy Castle" New topic
Author

Running Bouncy Castle

Paul Murtagh
Greenhorn

Joined: Mar 02, 2004
Posts: 6
I am just after purchasing Enterprise J2ME, I'm relitivly new to MIDlet development and have encountered a problem in running the Bouncycastle sample Applications in the "Crypto Recipes" chapter.
Downloaded at:
http://www.enterprisej2me.com/objects/software/Enterprise_J2ME_Code/chapter20/MobileSecurity.zip
The bouncycastle application is compiling ok for me and when it is ran, I get the list of midlets (AESLite etc) up on screen as per usual, however when I try to run an individual application I get the follwing error :"Uncaught exception java/lang/NoClassDefFoundError: java/security/SecureRandom: Cannot create class in system package ."
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
When I wrote that chapter, I tested on WTK 1.0.4, which has a security loophole that allows applications to create classes in system packages. It has been plugged since WTK 2.0. Now, you have to obfuscate your JAR file to change the packages to non-conflicting names before you can run it.
Or, if you use a refactoring IDE (IDEA or Eclipse), you can refactor the name of the offending package to something else. It would just work.
cheers
Michael


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Paul Murtagh
Greenhorn

Joined: Mar 02, 2004
Posts: 6
I downloaded the 1.0.4 toolkit and that gave me the following error
javac: source release 1.4 requires target release 1.4,
anyway I would prefer to stick with the latest version toolkit.
I'm quite a novice to MIDlet development so I'm not sure how to go about obfuscating a jar file, also I presume you mean the "bc116.jar" file?
PS Thanks for the quick response!
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
1. To use 1.0.4, you need to manually set the compiler "target" to 1.1. This is a known issue -- I guess they just do not bother to fix.
2. The jar file to obfuscate is your distribution JAR (the one which your jad file refers to).
cheers
Paul Murtagh
Greenhorn

Joined: Mar 02, 2004
Posts: 6
Obfuscated the jar file and still getting same error!! Any further suggestions? -just cant figure out what is going wrong, I have not modified anything at all, I'm just trying to get the given example running.
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Did you obfuscate the entire JAR file (both your code and library code)? Did you configure the obfuscator to change package names (should be default)? Try expand your "obfuscated" jar file and see what's inside. It should not contain the "java" package.
Paul Murtagh
Greenhorn

Joined: Mar 02, 2004
Posts: 6
I opened up the obfuscated JAR file and it "does" still contain the java package, what the obfuscator did was rename the math and security dirs (ie the two dirs contained in java) to "a.class".
In fact the error that I originally recieved, ie:
"Uncaught exception java/lang/NoClassDefFoundError: java/security/SecureRandom: Cannot create class in system package ."
is now reading (note the "a"):
"Uncaught exception java/lang/NoClassDefFoundError: java/security/a: Cannot create class in system package ."
I am using the Proguard obfuscator toolkit plugin in case that makes any difference.
If it wouldn't be too much hassle could you try running the BouncyCastle app on the wtk2.1 to see if you can run it cause I've been at it for a week now and still no joy!
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Originally posted by Paul Murtagh:
If it wouldn't be too much hassle could you try running the BouncyCastle app on the wtk2.1 to see if you can run it cause I've been at it for a week now and still no joy!

Yes, it did run. I use JODE to obfuscate it though.
cheers
Michael
YP Lucas
Greenhorn

Joined: Apr 23, 2004
Posts: 2
Originally posted by Michael Yuan:

Yes, it did run. I use JODE to obfuscate it though.
cheers
Michael

I have trouble when I use JODE to obfuscate my JAR file. Would you please paste your scripts of myproject.jos for me? Or describing how to use JODE to obfuscate the MIDlets in detail is better. Thanks a lot!
Jon Eaves
Greenhorn

Joined: Apr 04, 2004
Posts: 12
I can't help with JODE, but I know that both ProGuard and Retroguard can be used very successfully with the BC libraries.
When you're developing with BC, the simplest way to work is to unpack the lw-api jar to somewhere sensible and then add that to your classpath.
Then when you create your MIDlet jar file, I create a tmp.jar first, then obfuscate to create my final version.
Here's the ant target that I use for this.

As you can see, you obfuscate, then preverify, then create the jar file.
If people are interested, contact me directly and I can send you the entire Ant build file (not that long)
YP Lucas
Greenhorn

Joined: Apr 23, 2004
Posts: 2
Thanks for the response. I have found a simplest way to obfuscate using ProGuard:
1. run the ProGuardgui:
java -jar proguardgui.jar -nosplash
2. Before processing the obfucation, you should tick the "Default package" and untick
"Use mixed-case class names" at the Output panel.
3. click the "process" button, and the obfucation is OK.
4. preverify the output of Proguard.
It's easy and it does work.
Originally posted by Jon Eaves:
I can't help with JODE, but I know that both ProGuard and Retroguard can be used very successfully with the BC libraries.
When you're developing with BC, the simplest way to work is to unpack the lw-api jar to somewhere sensible and then add that to your classpath.
Then when you create your MIDlet jar file, I create a tmp.jar first, then obfuscate to create my final version.
Here's the ant target that I use for this.

As you can see, you obfuscate, then preverify, then create the jar file.
If people are interested, contact me directly and I can send you the entire Ant build file (not that long)
Sunny Toh
Greenhorn

Joined: May 05, 2004
Posts: 3
Hi all,
I am trying to run the bouncycastle example and is using WTK2.1 and ProGuard2.1 and using the ProGuardWTKGlue.java file suggested in the sun developers forum to setup ProGuard with my WTK2.1 However, when I try to obfuscate using the WTK, I get alot of warning messages about unresolved references to superclasses or interfaces and 1 unresolved reference to program class members. It says that my class files appear to be inconsistent. The codes for ProGuardWTKGlue.java is included below. Can someone please help me with this? Thanks alot for all your help in advance.
/* License
*
* Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
import java.io.*;
import com.sun.kvem.environment.Obfuscator;
public class ProGuardWTKGlue
implements Obfuscator {
public void createScriptFile(
File jadFilename, File projectDir) {
try {
File scriptFile = new File(projectDir, "script.pro");
OutputStream rawOut = new FileOutputStream(scriptFile);
PrintWriter out = new PrintWriter(rawOut);
out.println("-keep public class * extends " +
"javax.microedition.midlet.MIDlet");
out.flush();
out.close();
}
catch (IOException ioe) {
System.out.println(ioe);
}
}
public void run(File jarFileObfuscated, String wtkBinDir,
String wtkLibDir, String jarFilename,
String projectDir, String classPath, String emptyApi) {
// Create the command to run ProGuard.
StringBuffer buffer = new StringBuffer();
buffer.append("java -jar ");
buffer.append(wtkBinDir);
buffer.append("proguard.jar ");
buffer.append("-libraryjars ");
buffer.append(wtkBinDir);
buffer.append("..");
buffer.append(File.separatorChar);
buffer.append("lib");
buffer.append(File.separatorChar);
buffer.append("midpapi20.jar;");
buffer.append(wtkBinDir);
buffer.append("..");
buffer.append(File.separatorChar);
buffer.append("apps");
buffer.append(File.separatorChar);
buffer.append("BouncyCastle");
buffer.append(File.separatorChar);
buffer.append("lib");
buffer.append(File.separatorChar);
buffer.append("bc116.jar ");
buffer.append("-injars ");
buffer.append(jarFilename);
buffer.append(' ');
buffer.append("-outjar ");
buffer.append(jarFileObfuscated.getPath());
buffer.append(' ');
buffer.append("-overloadaggressively ");
buffer.append("-dontusemixedcaseclassnames ");
buffer.append('@');
buffer.append(projectDir);
buffer.append(File.separatorChar);
buffer.append("script.pro");
String command = buffer.toString();
System.out.println(command);
// Run the ProGuard command.
Runtime runtime = Runtime.getRuntime();
try {
Process p = runtime.exec(command);
redirect(p.getErrorStream(), System.out);
redirect(p.getInputStream(), System.out);
}
catch (IOException ioe) {
System.out.println(ioe);
}
// Now add the resource files to the JAR.
buffer = new StringBuffer();
buffer.append("jar uvf ");
buffer.append(jarFileObfuscated.getPath());
buffer.append(" -C ");
buffer.append(projectDir);
buffer.append(File.separatorChar);
buffer.append("res .");
command = buffer.toString();
System.out.println(command);
// Run the jar command.
try {
Process p = runtime.exec(command);
redirect(p.getInputStream(), System.out);
}
catch (IOException ioe) {
System.out.println(ioe);
}
}
private void redirect(InputStream rawIn, OutputStream rawOut)
throws IOException {
BufferedReader in = new BufferedReader(
new InputStreamReader(rawIn));
PrintWriter out = new PrintWriter(rawOut);
String line;
while ((line = in.readLine()) != null)
out.println(line);
in.close();
out.flush();
}
}
Sunny Toh
Greenhorn

Joined: May 05, 2004
Posts: 3
Ahh...finally managed to figure it out. =) I didnt include the rt.jar file as one of the library.jar files to be used.
Pik Master
Greenhorn

Joined: Mar 22, 2005
Posts: 1
Thank you, it helped me a lot.

Those of all you new to J2ME development I encourage to read my experience with making WTK and BouncyCastle working. You can find it on
http://pikmaster.webpark.pl/java/

But there are still some questions I want to ask.

I've been stuck on the problem of running totally obfuscated MIDlet from Eclipse. I am using the EclipseME plugin, but I cannot properly obfuscate the MIDlet to work without "Cannot create class in system package" . Even obfuscated .jar made with EclipseME and then transfered with .jad to WTK doesn't work (I can't find the entry-point class),

Is someone using Eclipse to develop J2ME using BouncyCastle (java.math.BigInter in specific) and could share his experience with me?


Super Pik Master
Esteban Debique
Greenhorn

Joined: Apr 04, 2005
Posts: 3
Can any of you help me with the obfuscation of the sample file from the Yuan book. I took his 'work' folder renamed it "BouncyCastleTest" then I tried to obfuscate and then package however when I try to do this it is still giving me these errors:

Uncaught exception java/lang/NoClassDefFoundError: java/security/SecureRandom: Cannot create class in system package .

Help me please!!!
gnana sekar
Greenhorn

Joined: May 29, 2006
Posts: 4
hi,
Regarding the topic what you are discussing is much useful to.
Now iam facing the same problem as like Sunny Toh told lots of warnings and while run the proguard.pro, even i included rt.jar in .pro file.

And while running i got like
Note ublicate definition of library class bouncy castle ...etc .
Note ublicate definition of library class java.security.SecureRandom etc..

i am getting the error like while run the application
java.lang.NoClassDefFoundError: java/security/a: Cannot create class in system package (after obfuscation)
solution for my problem please.....
Battula lakshman
Greenhorn

Joined: Apr 24, 2006
Posts: 9
hi all,
I have fallowed the steps you discussed in your postings. and i am unable to resolve the Exception i am getting. I did the obfuscation also still i am getting the same Exception.
can any body please tell me the way to resolve the Exception.
I am getting the fallowing Exception
java.lang.NoClassDefFoundError: java/security/SecureRandom: Cannot create class in system package
at org.bouncycastle.crypto.encodings.PKCS1Encoding.init(+35)
at Cryptor.doEncrypt(+64)
at BouncyTest$CryptCanvas.keyPressed(+45)
at javax.microedition.lcdui.Canvas.callKeyPressed(+19)
at javax.microedition.lcdui.Display$DisplayAccessor.keyEvent(+198)
at javax.microedition.lcdui.Display$DisplayManagerImpl.keyEvent(+11)
at com.sun.midp.lcdui.DefaultEventHandler.keyEvent(+121)
at com.sun.midp.lcdui.AutomatedEventHandler.keyEvent(+210)
at com.sun.midp.lcdui.DefaultEventHandler$QueuedEventHandler.run(+178)
Thanks in advance
lakshman
hyungi dibble
Ranch Hand

Joined: May 29, 2006
Posts: 42
I think the example work if you follow the direction.
Battula lakshman
Greenhorn

Joined: Apr 24, 2006
Posts: 9
hi,
I fallowed the everystep mentioned in the Posting the problem was it was giving the fallowing Exception
java.lang.NoClassDefFoundError: java/security/a: Cannot create class in system package
I unable to resolve that Exception.
Can you please tell me the way to avoid that Exception.
Thanks for your reply
lakshman
Anuradha Karunamuni
Ranch Hand

Joined: Dec 08, 2007
Posts: 64
I got the same problem...I renamed the java.* packages like Yuan said and then it worked fine...


"Simplicity is the ultimate sophistication..." ~Leonardo da Vinci~
SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running Bouncy Castle