I'm using 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 a BadPaddingException
, anybody know how to properly add the padding to an RSA Cipher with Bouncy Castle on 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 this is how I decrypt it 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;
}
Instead of using RSAEngine
directly use the PKCS1Encoding
class and construct it with
PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());