Java RSA加密/解密中的BadPaddingException错误

Java RSA加密/解密中的BadPaddingException错误,java,rsa,padding,encryption,Java,Rsa,Padding,Encryption,我目前面临BadPaddingException错误。我正在将加密数据从客户端发送到服务器。服务器将在收到数据后进行解密。 有人能帮我看一下代码并指出错误吗 客户端 try{ Cipher c = Cipher.getInstance("RSA"); c.init(Cipher.ENCRYPT_MODE, servPubKey); String myMessage = "this is a secret message"; byte[] msgByte = myM

我目前面临BadPaddingException错误。我正在将加密数据从客户端发送到服务器。服务器将在收到数据后进行解密。
有人能帮我看一下代码并指出错误吗

客户端

try{
    Cipher c = Cipher.getInstance("RSA");
    c.init(Cipher.ENCRYPT_MODE, servPubKey);
    String myMessage = "this is a secret message";
    byte[] msgByte = myMessage.getBytes();
    ObjectOutputStream outVehicle3 = new ObjectOutputStream(socket.getOutputStream());
    ParamClass print4 = new ParamClass (cipherText);
    outVehicle3.writeObject(print4);
}  catch (Throwable e) {
    // TODO Auto-generated catch block
    tv.append("\nUnable to perform RSA encryption!");
    e.printStackTrace();
}
服务器端

它说错误发生在下面一行

byte[] dectyptedText = dec.doFinal(print5.cipherText);

提前感谢您的帮助和指导。

我猜您在客户端代码中遗漏了这一行:

byte[] cipherText = c.doFinal(msgByte);
除此之外,最可能的错误是
servPubKey
serverPrivateKey
不匹配(即它们不是同一RSA密钥对的两部分)

检查它们是否匹配的快速方法是打印servPubKey.GetModule()和serverPrivateKey.GetModule(),并检查它们是否相同

byte[] cipherText = c.doFinal(msgByte);