Android DES Decrypt badpaddingexception: pad block corrupted

痴心易碎 提交于 2019-12-09 14:12:30

问题


First of all, I've reviewed all the entries on the forum, and I still can not find a solution to my problem. I have to measure the time it takes to encode and decode a text with DES, and make a comparison with other algorithms.

When I run the code, I have this error: BadPaddingException: pad block corrupted. When I debug, the code fails in this line:

  byte [] plaintext = cipher.doFinal (cipherBytes);

I use class Base64 to encode/decode String <--> byte[]

Any idea?

thanks

private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
private static int KEY_LENGTH = 64;

 public static SecretKey deriveKeyDES() {
        try {
            long start = System.currentTimeMillis();

            KeyGenerator kgen = KeyGenerator.getInstance("DES");
            kgen.init(KEY_LENGTH);
            SecretKey result = kgen.generateKey();

            long elapsed = System.currentTimeMillis() - start;
            return result;

        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } 
    }


    public static String encrypt(String plaintext, SecretKey key) {
        try {

            long start = System.currentTimeMillis();
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding")

            cipher.init(Cipher.ENCRYPT_MODE, key);

            byte[] cipherText = cipher.doFinal(plaintext.getBytes("UTF-8"));

            long elapsed = System.currentTimeMillis() - start;

            return toBase64(cipherText);

        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toBase64(byte[] bytes) {
        return Base64.encodeToString(bytes, Base64.NO_WRAP).trim();
    }


    public static String decrypt(String ciphertext, SecretKey key) {
        try {
            byte[] cipherBytes = fromBase64(ciphertext);

                long start = System.currentTimeMillis();
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

            cipher.init(Cipher.DECRYPT_MODE, key);
            cipher.update(cipherBytes);

             // This is where I get exception
            byte[] plaintext = cipher.doFinal(cipherBytes);

            String plainrStr = new String(plaintext, "UTF-8").trim();
            long elapsed = System.currentTimeMillis() - start;

            return plainrStr;
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] fromBase64(String base64) {
        return Base64.decode(base64, Base64.NO_WRAP);
    }

回答1:


Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, key);
cipher.update(cipherBytes);

// byte[] plaintext = cipher.doFinal(cipherBytes);
//                                   ^-- You shouldn't pass cipherBytes twice.
//                                   v-- instead use the parameter-less method:
byte[] plaintext    = cipher.doFinal();



回答2:


Padding exception occur when the last cipher text block does not compute to valid plain text. This would happen if last ciphertext block is corrupted or the key is incorrect. For CBC mode it would also happen if the second to last ciphertext was altered (but you are using ECB mode encryption).

In your case, the deriveKeyDES() is always generating a random key. Although we didn't get the actual calls to the security methods, I would presume you use a different key for encryption and decryption. In that case there is a very high chance that the resulting plain text does not contain valid padding bytes.

Rasmus answer certainly points to an error in your code, and it would screw up your timings and return a the plain text two times, but it would not remove the BadPaddingException.




回答3:


I had the same problem in one source code, and IllegalBlockSizeException in another one. Solved this two problems by return encoding data like:

 public String encrypt(String input) {
    try {
        byte[] inputBytes = input.getBytes("UTF-8");
        byte[] enc = encryptCipher.doFinal(inputBytes);
        // and problem was in return encoding. That's how i fixed it 
        return Base64.encodeToString(enc,Base64.DEFAULT);
        .....
          }
        }

Give u a code for decrypt:

 public String decrypt(String input) {
    try {
        byte[] dec = Base64.decode(input.getBytes(), Base64.DEFAULT);
        //here had exception
        byte[] utf8 = decryptCipher.doFinal(dec);
        return new String(utf8,"UTF8");
    } catch (IOException | BadPaddingException | IllegalBlockSizeException e) {
        e.printStackTrace();
    }
    return null;
}

I should submit, that had BadPaddingException and IllegalBlockSizeException only in decrypt method byte[] utf8 = decryptCipher.doFinal(dec); (u had exeption in the same place: byte[] plaintext = cipher.doFinal(cipherBytes);), but real wrong is in encrypt method(return value)

That's why i recommend u to use that code in encrypt method:

return Base64.encodeToString(enc,Base64.DEFAULT);

P.S Tried to a give full answer on your question.



来源:https://stackoverflow.com/questions/12110459/android-des-decrypt-badpaddingexception-pad-block-corrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!