我正在使用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;
}
答案 0 :(得分:7)
不使用RSAEngine
直接使用PKCS1Encoding
类,而是使用
PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());