简体   繁体   English

AES128解密:javax.crypto.badpaddingexception pad块损坏

[英]AES128 Decryption :javax.crypto.badpaddingexception pad block corrupted

I try to decrypt an encrypted data that I receive from a web service. 我尝试解密从Web服务收到的加密数据。

The encryption is done using AES 128 . 加密使用AES 128完成。

I use the following code to decrypt the data: 我使用以下代码来解密数据:

public static String decrypt(String strToDecrypt)
{       
    try
    {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); //AES/CBC/PKCS7Padding
        SecretKeySpec secretKey = new SecretKeySpec(AppConstants.AESEncryptionKey.getBytes("UTF8"), "AES");
        int blockSize = cipher.getBlockSize();
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(new byte[blockSize])); //new IvParameterSpec(new byte[16])
        byte decBytes[] = cipher.doFinal(Base64.decode(strToDecrypt, 0));
        // byte decBytes[] = cipher.doFinal(Base64.decodeBase64(strToDecrypt));
        String decStr = new String(decBytes);
        System.out.println("After decryption :" + decStr);
        return decStr;
    }
    catch (Exception e)
    {
        System.out.println("Exception in decryption : " + e.getMessage());
    }
    return null;
}

At

cipher.doFinal() cipher.doFinal()

I got the following Exception: 我得到以下例外:

javax.crypto.badpaddingexception pad block corrupted javax.crypto.badpaddingexception pad块损坏

I went through my post but ended up with no solution. 我经历了我的帖子,但最终没有解决方案。 I am badly stuck over here. 我被困在这里。

SecureRandom sr = SecureRandom.getInstance("SHA1PRNG","Crypto");

works perfectly 工作得很好

Note: This code works only on devices up to Android 6. Starting with Android 7.0 the "Crypto" provider has been removed , therefore this code will fail. 注意:此代码仅适用于Android 6以下的设备。从Android 7.0开始, "Crypto" 提供程序已被删除 ,因此此代码将失败。

AES keys should consist of random data. AES密钥应包含随机数据。 If you store them as a String then you are likely to loose information, especially if you use encodings such as UTF-8. 如果将它们存储为String,则可能会丢失信息,尤其是在使用UTF-8等编码时。 Your line: 你的路线:

AppConstants.AESEncryptionKey.getBytes("UTF8")

Makes it likely that you've lost data during conversion to/from a string. 使您在转换为字符串/从字符串转换过程中丢失数据的可能性很小。 Use hexadecimals instead if you require a string, or simply store the key as a byte array. 如果需要字符串,则使用十六进制,或者只将字符串存储为字节数组。


Note that this answer doesn't indicate any security related hints. 请注意,此答案并未指出任何与安全相关的提示。 In general you only want to derive keys or store them in containers. 通常,您只想导出密钥或将它们存储在容器中。 You don't want to use CBC over an insecure channel either. 您也不想在不安全的渠道上使用CBC。

在我的情况下问题来了,因为加密密钥和解密密钥都是不同的,当我检查两个键具有相同的值然后问题没有来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 文件解密失败:javax.crypto.BadPaddingException:填充块已损坏 - Decryption of file fails: javax.crypto.BadPaddingException: pad block corrupted 错误:javax.crypto.BadPaddingException:解密时填充块损坏 - Error : javax.crypto.BadPaddingException: pad block corrupted while Decryption javax.crypto.BadPaddingException:pad块损坏 - javax.crypto.BadPaddingException: pad block corrupted Java Blowfish CBC 解密 javax.crypto.BadPaddingException:填充块已损坏 - Java Blowfish CBC Decryption javax.crypto.BadPaddingException: pad block corrupted javax.crypto.BadPaddingException:填充块损坏的异常 - javax.crypto.BadPaddingException: pad block corrupted exception javax.crypto.BadPaddingException:填充块有时损坏 - javax.crypto.BadPaddingException: pad block corrupted sometimes Android javax.crypto.BadPaddingException:填充块已损坏 - Android javax.crypto.BadPaddingException: pad block corrupted 函数解密引发javax.crypto.BadPaddingException:android中的类SimpleCrypto中的填充块已损坏 - function decrypt throws javax.crypto.BadPaddingException: pad block corrupted in class SimpleCrypto in android 解密图像时,给出javax.crypto.BadPaddingException:填充块损坏的Android - When Decrypting Image, gives javax.crypto.BadPaddingException: pad block corrupted Android javax.crypto.BadPaddingException AES - javax.crypto.BadPaddingException AES
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM