RSA Padding with Bouncy Castle on BlackBerry

I use Bouncy Castle to encrypt strings to send them to my java web service, where they are decrypted when the message reaches the server, I get BadPaddingException, does anyone know how to correctly add the RSA Cipher add-on from Bouncy Castle to J2ME?

This is the encryption code on the client:

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;
  } 

And here is how I decrypt it on the server side:

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;
    }
+3
source share
1 answer

Instead of using RSAEnginedirectly, use a class PKCS1Encodingand build it with

PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());
+7
source

All Articles