Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

J2ME Bouncy Castle get AES encryption result as String

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)

like image 282
ePezhman Avatar asked Nov 28 '25 08:11

ePezhman


1 Answers

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.

like image 145
Telmo Pimentel Mota Avatar answered Nov 30 '25 21:11

Telmo Pimentel Mota