Java Android 加密“垫块损坏”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19957052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:49:48  来源:igfitidea点击:

Android encryption "pad block corrupted" exception

javaandroidexceptionencryptionaes

提问by Esqarrouth

In this code, this line is causing an exception:

在此代码中,此行导致异常:

clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));

clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));

javax.crypto.BadPaddingException: pad block corrupted

javax.crypto.BadPaddingException: pad block corrupted

I got the code from: http://www.techrepublic.com/blog/software-engineer/attention-android-developers-keep-user-data-safe/

我从以下位置获得代码:http: //www.techrepublic.com/blog/software-engineer/attention-android-developers-keep-user-data-safe/

Any ideas?

有任何想法吗?

    private String decrypt (String encryptedText) {
        byte[] clearText = null;
        try {
            SecretKeySpec ks = new SecretKeySpec(getKey(), "AES");
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, ks);
            clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));
            return new String(clearText, "UTF-8");
        } catch (Exception e) {
            return null;
        }
    }

Details: I am encrypting it on the android as well

详细信息:我也在 android 上对其进行加密

采纳答案by Esqarrouth

owlstead's advice was helpful, but for this case when using the code in

owlstead 的建议很有帮助,但在这种情况下,在使用代码时

Attention Android developers: Keep user data safe http://www.techrepublic.com/blog/software-engineer/attention-android-developers-keep-user-data-safe/

Android 开发者注意:保护用户数据安全 http://www.techrepublic.com/blog/software-engineer/attention-android-developers-keep-user-data-safe/

I made some changes to the code that might be helpful for other people in the future. I completely deleted the getkey method.

我对代码进行了一些更改,这些更改将来可能对其他人有所帮助。我完全删除了 getkey 方法。

private static String seed;

/**
 * Encrypts the text. 
 * @param clearText The text you want to encrypt
 * @return Encrypted data if successful, or null if unsucessful
 */
protected String encrypt(String clearText) {
    byte[] encryptedText = null;
    try {
        byte[] keyData = seed.getBytes();
        SecretKey ks = new SecretKeySpec(keyData, "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE, ks);
        encryptedText = c.doFinal(clearText.getBytes("UTF-8"));
        return Base64.encodeToString(encryptedText, Base64.DEFAULT);
    } catch (Exception e) {
        return null;
    }
}

/**
 * Decrypts the text
 * @param encryptedText The text you want to encrypt
 * @return Decrypted data if successful, or null if unsucessful
 */
protected String decrypt (String encryptedText) {
    byte[] clearText = null;
    try {
        byte[] keyData = seed.getBytes();
        SecretKey ks = new SecretKeySpec(keyData, "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.DECRYPT_MODE, ks);
        clearText = c.doFinal(Base64.decode(encryptedText, Base64.DEFAULT));
        return new String(clearText, "UTF-8");
    } catch (Exception e) {
        return null;
    }
}   

回答by Maarten Bodewes

Java + Android + Encryption + Exception means just one thing normally, somebody is using the SecureRandomclass again as a key derivation function. This fails when the SecureRandomimplementation of "SHA1PRNG"does not behave as the one in Sun's implementation in Java SE. Especially if the seed is addedto the state of the random number generator instead of the seed being used as a starting point of the PRNG.

Java + Android + Encryption + Exception 通常意味着一件事,有人SecureRandom再次使用该类作为关键派生函数。当 的SecureRandom实现与"SHA1PRNG"Sun 在 Java SE 中的实现不同时,这将失败。特别是如果将种子添加到随机数生成器的状态而不是将种子用作 PRNG 的起点时。

Basically, simply use SecretKey aesKey = new SecretKeySpec(byte[] keyData, "AES")instead, or - if you start off with a password - try and generate the key using PBKDF2.

基本上,只需使用SecretKey aesKey = new SecretKeySpec(byte[] keyData, "AES"),或者 - 如果您从密码开始 - 尝试使用 PBKDF2 生成密钥。

回答by siddharth patel

I Reffred From this : https://androidfreetutorial.wordpress.com/2017/03/14/android-encryptiondecryption-with-aes-algorithm/

我引用了这个:https://androidfreetutorial.wordpress.com/2017/03/14/android-encryptiondecryption-with-aes-algorithm/

Change to "AES" From "AES/ECB/PKCS7Padding";

从“ AES/ECB/PKCS7Padding”改为“ AES”;

回答by BaiJiFeiLong

For me, the problem is in getKey()

对我来说,问题出在 getKey()

Make sure that two invocation of getKey()return the same value.

确保两次调用getKey()返回相同的值。

I used new SecureRandom(password.getBytes())to generate key. It worked on Windows, but on Android, it returned different value for different call.

我曾经new SecureRandom(password.getBytes())生成密钥。它在 Windows 上工作,但在 Android 上,它为不同的调用返回不同的值。