I'm developing some app in J2ME which send and receives encrypted messages, I found the code below to encrypt it and convert to String but it doesn't work and throw exception, how should I do this? and how should I convert String to byte[] when I want to do decryption? thanks
byte[] plainArray = message.getBytes();
try {
byte[] keyBytes = "SECRET_1SECRET_2SECRET_3".getBytes();
// key = new KeyParameter(keyBytes);
AESEngine engine = new AESEngine();
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(engine);
cipher.init(true, new KeyParameter(keyBytes));
byte[] cipherBytes = new byte[cipher.getOutputSize(plainArray.length)];
int cipherLength = cipher.processBytes(plainArray, 0, plainArray.length, cipherBytes, 0);
cipher.doFinal(cipherBytes, cipherLength);
String result2 = org.apache.commons.codec.binary.Base64.encodeBase64String(cipherBytes);
formSender.append(result2);
} catch (Exception e) {
}
Exception :
TRACE: , Exception caught in Display class java.lang.Error: ClassFormatError: 56 at SSMS.EncShow(), bci=173 at SSMS.commandAction(), bci=16 at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=44 at com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=80 at com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(), bci=31 at com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(), bci=48 at com.sun.midp.chameleon.CWindow.keyInput(), bci=38 at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=32 at com.sun.midp.lcdui.DisplayEventListener.process(), bci=294 at com.sun.midp.events.EventQueue.run(), bci=177 at java.lang.Thread.run(Thread.java:722)
According to http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassFormatError.html "Thrown when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file".
Your org.apache.commons version might be too new for Java ME, which is based on Java 1.3. Be sure to use older versions of org.apache.commons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With