Java 解密对某些加密值无效

Java 解密对某些加密值无效,java,encryption,Java,Encryption,使用AES对某些加密值进行解密不起作用 加密代码为Q5447fX+u4a8RbDAf92oZA== 当我对上述值进行解密时,它抛出javax.crypto.badpattingexception:给定的最后一个块没有正确填充 如何解决这个问题 我的代码在下面 private final static String algorithm = "AES"; private final static byte[] keyValue = new byte[] {'@', 'j', 'a', 'z

使用AES对某些加密值进行解密不起作用

加密代码为Q5447fX+u4a8RbDAf92oZA== 当我对上述值进行解密时,它抛出javax.crypto.badpattingexception:给定的最后一个块没有正确填充

如何解决这个问题

我的代码在下面

private final static String algorithm = "AES";
private final static byte[] keyValue = new byte[]      {'@', 'j', 'a', 'z', 'p', 'a', 'w', 'm', 'd', 'n', 'c', '5', 'y', 'p', 't', '*'};

// generates a secret key
 private static Key generateKey( ) throws Exception {
    Key key = new SecretKeySpec(keyValue, algorithm);
    return key;
}

// Performs Encryption
public static String encrypt(String plainText) throws Exception {
    Key key = generateKey();
    Cipher chiper = Cipher.getInstance(algorithm);
    chiper.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = chiper.doFinal(plainText.getBytes());
    return new BASE64Encoder().encode(encVal);
}

// Performs decryption
public static String decrypt(String encryptedText) throws Exception {
    // generate key 
    Key key = generateKey();
    Cipher chiper = Cipher.getInstance(algorithm);
    chiper.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
    byte[] decValue = chiper.doFinal(decordedValue);
    return new String(decValue);
}

您需要使用与加密相同的密钥进行解密:您无法生成新密钥。@GregS,但OP说,它不仅适用于某些加密值。对于专家来说,这与加密有关。看起来像一个请在你的代码中包括generateKey,但要注意,如果它被重命名,我会尖叫。