解密错误填充错误

Decryption Error bad padding

我正在尝试在两个代理之间发送加密消息。我有一个包含信息的字符串,我将其转换为字节对其进行加密,然后再次转换为字符串以发送消息。收到消息但是,在接收代理处我收到以下异常

javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:354)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:380)
at javax.crypto.Cipher.doFinal(Cipher.java:2121)
at Hi.action(Hi.java:72)
at jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344)
at jade.core.Agent$ActiveLifeCycle.execute(Agent.java:1532)
at jade.core.Agent.run(Agent.java:1471)
at java.lang.Thread.run(Unknown Source)

我在同一个容器中尝试了代理的代码,它工作正常,但是,如果它们在不同的容器中,它就不行了。

我是这样加密邮件的:

String msg1="Message from bob 1"; // message
MSGBOB = cipher.doFinal(msg1.getBytes("ISO-8859-1")); // encryption
msg.setContent(new String (MSGBOB,"ISO-8859-1")); // conversion to string

我是这样解密的:

mm = msg.getContent().getBytes("ISO-8859-1");// received message 
m = new String(cipher.doFinal(mm),"ISO-8859-1"); // decryption

对加密输出使用 base64 编码,不要使用 new String(),因为某些字节值无法正确表示为字符串。所以当再次反转为字节时,它将不是正确的加密值

我的意思是:

String msg1="Message from bob 1"; // message
MSGBOB = cipher.doFinal(msg1.getBytes("ISO-8859-1")); // encryption
msg.setContent(Base64.encode(MSGBOB)); // conversion to string
This is how I decrypt it :

mm = Base64.decode(msg.getContent());// received message 
m = new String(cipher.doFinal(mm),"ISO-8859-1"); // decryption