RSA Padding与黑莓上的Bouncy Castle

时间:2011-03-10 16:29:24

标签: blackberry encryption java-me rsa bouncycastle

我正在使用Bouncy Castle来加密字符串以将它们发送到我们解密的java Web服务,当消息到达服务器时我得到BadPaddingException,任何人都知道如何正确地将填充添加到在J2ME上使用带有Bouncy Castle的RSA Cipher?

这是客户端上的加密代码:

public byte[] Encrypt(byte[] data)
  {
     RSAKeyParameters publicKey = new RSAKeyParameters(false, new BigInteger(_publicKeyModulus), new BigInteger(_publicKeyExponent));
     RSAEngine engine = new RSAEngine();
     engine.init(true, publicKey);

     byte[] output = engine.processBlock(data, 0, data.length);

     return output;
  } 

这就是我解密服务器端的方式:

public byte[] Decrypt(byte[] data)
    {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] cipherData = cipher.doFinal(data);
            return cipherData;
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
        } catch(IllegalBlockSizeException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
        } catch(InvalidKeyException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
        } catch(BadPaddingException ex) {
            Logger.getLogger(EncryptorDecryptor.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

1 个答案:

答案 0 :(得分:7)

不使用RSAEngine直接使用PKCS1Encoding类,而是使用

构建它
PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());