J2ME Bouncy Castle将AES加密结果作为String获取

ePe*_*man 3 java base64 bouncycastle java-me

我正在J2ME中开发一些发送和接收加密消息的应用程序,我发现下面的代码加密它并转换为String但它不起作用并抛出异常,我应该怎么做?当我想要解密时,我应该如何将String转换为byte []?谢谢

 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) {
        } 
Run Code Online (Sandbox Code Playgroud)

例外:

TRACE:,Exception陷入Display类java.lang.Error:ClassFormatError:56 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位于com.sun.midp.lcdui.DisplayEventListener.process(),bci = 294位于com.sun.midp.events.EventQueue.run(),bci = 177位于java.lang.Thread.run(Thread.java: 722)

Tel*_*ota 5

根据http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassFormatError.html "抛出Java虚拟机尝试读取类文件并确定文件格式错误或否则不能被解释为类文件".

您的org.apache.commons版本对于基于Java 1.3的Java ME来说可能太新了.请务必使用旧版本的org.apache.commons.