256bit解密解密包含

256bit解密解密包含

本文介绍了无法使用AES 256bit解密解密包含+符号的文本:javax.crypto.BadPaddingException:给定最终块未正确填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在JSP页面内解密含有'+'符号的文本,我收到以下错误javax.crypto.BadPaddingException:给定最终块未正确填充。 >

但是,如果我从Eclipse运行或者将代码转换为可执行Jar,代码可以正常工作。



JARS used:
local_policy.jar
US_export_policy.jar



以下是我的Java代码

  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;

私有静态密码密码;

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());
返回新的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中通常是无效的。如果传递QueryString上的数据,您将需要URL编码Base64编码数据。此外,加号 + 在QueryString中具有语义含义。 URL的另一个原因是编码你的数据(可能是你的问题的原因。)


Hi I am not able to decrypt a text containing '+' symbol inside the JSP page, I get the following error javax.crypto.BadPaddingException: Given final block not properly padded.

However the code works fine if I run from Eclipse or if I convert the code into Executable Jar.

JARS used : local_policy.jar US_export_policy.jar

Below is my Java code

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);
    }
}
解决方案

I can't speak to the Java code but Base64 strings are frequently not valid across a QueryString. You will need to URL Encode your Base64 encoded data if passing the data on the QueryString. Also the plus sign + has a semantic meaning within the QueryString. Yet another reason to URL Encode your data (and possible cause of your issue.)

这篇关于无法使用AES 256bit解密解密包含+符号的文本:javax.crypto.BadPaddingException:给定最终块未正确填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:49