【问题标题】:BadPaddingException Error in RSA enryption/decryptionRSA 加密/解密中的 BadPaddingException 错误
【发布时间】:2011-02-24 12:58:33
【问题描述】:

我目前正面临 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();
}

服务器端

ObjectInputStream inServ3 = new ObjectInputStream(socket.getInputStream());
try{
    ParamClass print5 = (ParamClass) (inServ3.readObject());
    Cipher dec = Cipher.getInstance("RSA");
    dec.init(Cipher.DECRYPT_MODE, serverPrivateKey);
    byte[] dectyptedText = dec.doFinal(print5.cipherText);
    String decipherText = dectyptedText.toString();
    System.out.println("\nDecrypted Message to string: " + decipherText);
}catch (Throwable e) {
    e.printStackTrace();
    System.out.println("Unable to perform RSA decryption!");
}

它表示错误发生在以下行中。

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

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

【问题讨论】:

    标签: java rsa padding encryption


    【解决方案1】:

    我假设您在客户端代码中缺少这一行:

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

    除此之外,最可能的错误是 servPubKeyserverPrivateKey 不匹配(即它们不是同一个 RSA 密钥对的两半)。

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

    【讨论】:

      猜你喜欢
      • 2018-09-15
      • 2020-07-14
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2018-01-17
      • 1970-01-01
      相关资源
      最近更新 更多