解密错误填充错误 [英] Decryption Error bad padding

查看:205
本文介绍了解密错误填充错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在两个代理之间发送加密的消息。我有一个字符串,包含我转换为字节的信息加密它,然后再次字符串发送消息。收到消息后,在接收代理我收到以下异常

  javax.crypto.BadPaddingException:解密错误
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)
在com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:380)
在javax.crypto.Cipher.doFinal(Cipher.java:2121)
在Hi $ 1.action(Hi.java:72)
在jade.core.behaviours.Behaviour.actionWrapper(Behaviour.java:344)
在jade.core.Agent $ ActiveLifeCycle.execute(代理.java:1532)
在jade.core.Agent.run(Agent.java:1471)
在java.lang.Thread.run(未知来源)

我尝试在同一个容器中的代理程序的代码,但它工作正常,但如果它们不在不同的容器上。



这是我如何加密消息:

  String msg1 =Messag e从bob 1; // message 
MSGBOB = cipher.doFinal(msg1.getBytes(ISO-8859-1)); // encryption
msg.setContent(new String(MSGBOB,ISO-8859-1)); //转换为字符串

这是我如何解密它:

  mm = msg.getContent()。getBytes(ISO-8859-1); //收到的消息
m = new String(cipher.doFinal毫米), ISO-8859-1); //解密


解决方案

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



这是我的意思:

  String msg1 =来自bob 1的消息; // message 
MSGBOB = cipher.doFinal(msg1.getBytes(ISO-8859-1)); // encryption
msg.setContent(Base64.encode(MSGBOB)); //转换为字符串
这是我如何解密它:

mm = Base64.decode(msg.getContent()); //收到的消息
m = new String .doFinal(毫米), ISO-8859-1); //解密


I am trying to send encrypted messages between two agents .I have a string that contains information that I convert to bytes encrypt it and then to string again to send the message. Messages are received however, at the receiving agent I get the following exception

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$1.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)

I tried the code for agents in the same container and it works fine however, if they are on different container it doesn't.

This is how I encrypt the message:

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

This is how I decrypt it :

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

解决方案

use base64 encoding for the output of the encryption, don't use new String() as some byte-values will not be represented correctly as string. so when reversed to bytes again it will not be the correct ciphered value

here is what i mean:

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

这篇关于解密错误填充错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆