我正在使用 Bouncy CaSTLe 加密字符串以将它们发送到我的 java web 服务,在那里它们被解密,当消息到达服务器时我得到一个 BadPaddingException
,任何人都知道如何正确添加在 J2ME 上使用 Bouncy CaSTLe 填充 RSA 密码?
这是客户端的加密代码:
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;
}
最佳答案
而不是使用 RSAEngine
直接使用 PKCS1Encoding
类并用
PKCS1Encoding engine = new PKCS1Encoding(new RSAEngine());
关于blackberry - 在 BlackBerry 上使用 Bouncy CaSTLe 进行 RSA 填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5262696/