hello all,

I am new to J2ME development rather this is my first code i have written in J2me for encryption and decryption using bouncycastle .
It is giving the error which i am unable to find solution for... plz help... :sad:

This is the error it is showing in netbeans
rror preverifying class javax.crypto.interfaces.DHPrivateKey
java/lang/IncompatibleClassChangeError: Implementing class
C:\Documents and Settings\Abhijeet.p\My Documents\NetBeansProjects\encryptText\nbproject\build-impl.xml:470: Preverification failed with error code 1.
BUILD FAILED (total time: 2 seconds)

and this is the code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package hello;

import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.util.encoders.Hex;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author abhijeet.p
 */
public class EncryptText extends MIDlet implements CommandListener
{
private Display display; // Reference to Display object
private TextBox tbTest; // Main textbox
private Command cmExit; // Command to exit
private Command cmEncrypt; // Command to encrypt text
private Command cmDecrypt; // Command to decrypt text
private PaddedBufferedBlockCipher cipher = null; // The cipher
private String key = "x-392kla%3$*1f";
public EncryptText()
{
display = Display.getDisplay(this);
// Create the Commands.
cmExit = new Command("Exit", Command.EXIT, 1);
cmEncrypt = new Command("Encrypt", Command.SCREEN, 2);
cmDecrypt = new Command("Decrypt", Command.SCREEN, 3);
tbTest = new TextBox("Text to encrypt/decrypt",
"IBM developerworks", 250, TextField.ANY);
tbTest.addCommand(cmExit);
tbTest.addCommand(cmEncrypt);
tbTest.addCommand(cmDecrypt);
tbTest.setCommandListener(this);
// Create a new cipher (engine) for encrypting/decrypting
cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()));
// Create a key
key = new String("x-392kla%3$*1f");
}
    public void startApp()
{
display.setCurrent(tbTest);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmExit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == cmEncrypt) // Encrypt text
{
encryptData();
}
else if (c == cmDecrypt) // Decrypt text
{
decryptData();
}
}
    private void encryptData()
{
// Get the contents from the textbox
// Get the contents from the textbox
byte[] inBytes = tbTest.getString().getBytes();
// Initialize the cipher. 'true' specifies encrypting
cipher.init(true, new KeyParameter(key.getBytes()));
// Determine the minimum output buffer size
byte[] outBytes = new byte[cipher.getOutputSize(inBytes.length)];
// 'len' is the actual size returned
int len = cipher.processBytes(inBytes, 0, inBytes.length, outBytes, 0);
try
{
// Process the last block in the buffer, starting at 'len' location
cipher.doFinal(outBytes, len);
tbTest.setString(new String(Hex.encode(outBytes)));
// Debug message
System.out.println("encrypted: " + new String(Hex.encode(outBytes)));
}
catch(CryptoException e)
{
System.out.println("Exception: " + e.toString());
}
}
private void decryptData()
{
// Get the text to decrypt from the textbox
// Get the text to decrypt from the textbox
byte[] inBytes = Hex.decode(tbTest.getString().getBytes());
// Initialize the cipher. 'false' specifies decrypting
cipher.init(false, new KeyParameter(key.getBytes()));
// Determine the minimum output buffer size
byte[] outBytes = new byte[cipher.getOutputSize(inBytes.length)];
// 'len' is the actual size returned
int len = cipher.processBytes(inBytes, 0, inBytes.length, outBytes, 0);
try
{
// Process the last block in the buffer, starting at 'len' location
cipher.doFinal(outBytes, len);
// Update the textbox with the decrypted string
tbTest.setString(new String(outBytes).trim());
System.out.println("decrypted: " + new String(outBytes).trim());
}
catch(CryptoException e)
{
System.out.println("Exception: " + e.toString());
    }
    }
}

One of the thinks I hate about NetBeans is all this stuff that is added to build-impl.xml without user knowledge. You need to open that file and see what is on the line 470 where the error is reported. If you have problem solve it then post fraction of that xml so we can see what is going on

One of the thinks I hate about NetBeans is all this stuff that is added to build-impl.xml without user knowledge. You need to open that file and see what is on the line 470 where the error is reported. If you have problem solve it then post fraction of that xml so we can see what is going on

Thanks Peter.
Sorry for the inconvenience caused due to the way i posted the problem was in a hurry and was the first time I was posting on any forum.
Getting back to the problem the xml file has the following code on line 470

<nb-preverify srcdir="${preverify.sources.dir}" destdir="${preverify.classes.dir}" classpath="${platform.bootclasspath}:${extra.classpath}" configuration="${platform.configuration}" platformhome="${platform.home}" platformtype="${platform.type}" commandline="${platform.preverifycommandline}"/>

Sorry again
The code is not coming as code template :sad:

Hmmm I was hoping that line will have some file name or something that may got wrong for whatever reason.
The last thing we can do is for you to put your project in ZIP file and post it if you want to (You need to "Use Advanced Editor" to be able access Attachment Manager that is located below post editing area) and then I can have look what is wrong

Hmmm I was hoping that line will have some file name or something that may got wrong for whatever reason.
The last thing we can do is for you to put your project in ZIP file and post it if you want to (You need to "Use Advanced Editor" to be able access Attachment Manager that is located below post editing area) and then I can have look what is wrong

Hello Peter,
Thanks for your reply
I am trying to attach my project as Zip but its saying upload failed :'( also i tried the same code in eclipse it is giving the following error there as well . I m doomed :|

Uncaught exception java/lang/NoClassDefFoundError: org/bouncycastle/crypto/CryptoException.:@:$

Try it again, if upload fails let me know will try something else

Try it again, if upload fails let me know will try something else

Hey Peter I did the encryption using RSA and its working and my frnd used cldc_crypto.zip for the above xample and it is working at his place.

Thanks for all your help really appreciated.:cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.