【问题标题】:J2ME Bouncy Castle get AES encryption result as StringJ2ME Bouncy Castle 以字符串形式获取 AES 加密结果
【发布时间】:2012-06-30 00:30:52
【问题描述】:

我正在 J2ME 中开发一些发送和接收加密消息的应用程序,我发现下面的代码将其加密并转换为字符串,但它不起作用并抛出异常,我应该怎么做?当我想解密时,我应该如何将 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) {
        } 

例外:

TRACE: , 捕获到异常 在显示类 java.lang.Error: ClassFormatError: 56 at SSMS.EncShow(), bci=173 at SSMS.commandAction(), bci=16 at javax.microedition.lcdui.Display$ChameleonTunnel.callScreenListener(), bci=44 在 com.sun.midp.chameleon.layers.SoftButtonLayer.processCommand(), bci=80 在 com.sun.midp.chameleon.layers.SoftButtonLayer.soft1(),bci=31 在 com.sun.midp.chameleon.layers.SoftButtonLayer.keyInput(),bci=48 在 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)

【问题讨论】:

    标签: java java-me base64 bouncycastle


    【解决方案1】:

    根据http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ClassFormatError.html“当 Java 虚拟机尝试读取类文件并确定该文件格式错误或无法解释为类文件时抛出”。

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

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 1970-01-01
      • 2011-01-26
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多