无法解密包含+;使用AES 256bit解密的符号:javax.crypto.BadPaddingException:给定的最终块未正确填充
您好,我无法解密JSP页面中包含“+”符号的文本,我得到以下错误javax.crypto.BadPaddingException:给定的最后一个块没有正确填充 但是,如果我从Eclipse运行,或者如果我将代码转换为可执行Jar,那么代码就可以正常工作 使用的罐子: local_policy.jar 美国出口政策.jar 下面是我的Java代码无法解密包含+;使用AES 256bit解密的符号:javax.crypto.BadPaddingException:给定的最终块未正确填充,java,jsp,encryption,aes,Java,Jsp,Encryption,Aes,您好,我无法解密JSP页面中包含“+”符号的文本,我得到以下错误javax.crypto.BadPaddingException:给定的最后一个块没有正确填充 但是,如果我从Eclipse运行,或者如果我将代码转换为可执行Jar,那么代码就可以正常工作 使用的罐子: local_policy.jar 美国出口政策.jar 下面是我的Java代码 import java.security.Key; import javax.crypto.Cipher; import javax.crypto.s
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Decrypt256bit {
private static Key key;
private static Cipher cipher;
static {
key = new SecretKeySpec("P@ssw0Rd!@#**&&&P@ssw0Rd!@#**&&&".getBytes(), "AES");
try {
cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING","SunJCE");
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encryptData(String plainText) {
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
public static String decryptData(String encryptedValue) {
try {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
int maxKeyLen = Cipher.getMaxAllowedKeyLength("AES");
System.out.println("Length==="+maxKeyLen);
return new String(cipher.doFinal(decordedValue));
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
我无法与Java代码对话,但Base64字符串在QueryString中通常无效。如果在查询字符串上传递数据,则需要对Base64编码的数据进行URL编码。加号
+
在查询字符串中也有语义含义。还有另一个对您的数据进行URL编码的原因(以及您的问题的可能原因)
字符串数据=URLEncoder.encode(myBase64)
您声明解密有问题,但发布了执行加密的代码。您期望得到什么回报?抱歉,这是我解密公共静态字符串解密数据(字符串加密值){try{cipher.init(cipher.DECRYPT_模式,key);byte[]decordedValue=new BASE64Decoder().decodeBuffer(encryptedValue);int maxKeyLen=cipher.getMaxAllowedKeyLength(“AES”);System.out.println(“长度==”+maxKeyLen);返回新字符串(cipher.doFinal(decordedValue));}catch(异常e){抛出新的IllegalArgumentException(e);}请勿在注释中发布超过一条语句的代码,请将其编辑到您的问题中。编码和解码基本正常(您应该为
String.getBytes()和new String()指定显式UTF-8
编码)。您是否以某种方式将base64编码的值嵌入到JSP中?您想说什么?当转换为jar时,上面的代码将解密包含+符号的字符串,但在JSP页面中包含相同的代码时,不会发生这种情况。@user3473013您是否确实尝试过使用此答案?它甚至提供了您需要使用的代码。Ind在上面的回答中,你不明白什么。