Android DES Decrypt badpaddingexception:pad block corrupted [英] Android DES Decrypt badpaddingexception: pad block corrupted

查看:143
本文介绍了Android DES Decrypt badpaddingexception:pad block corrupted的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经审查了论坛上的所有条目,我仍然找不到我的问题的解决方案。
我必须测量使用DES编码和解码文本所需的时间,并与其他算法进行比较。

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.

当我运行代码时,我有这个错误: BadPaddingException:pad block corrupted 。当我调试时,代码在这一行失败:

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);

我使用Base64编码/解码字符串< - > byte []

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

任何想法?

谢谢

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);
    }


推荐答案

密文块不计算为有效的纯文本。如果最后一个密文块被破坏或密钥不正确,就会发生这种情况。对于CBC模式,如果第二个到最后一个密文被更改(但是您使用ECB模式加密)也会发生。

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).

在您的情况下, derivedKeyDES()总是生成随机密钥。虽然我们没有获得对安全方法的实际调用,但我认为使用不同的密钥进行加密和解密。在这种情况下,结果的纯文本不会包含有效的填充字节的机率很高。

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答案肯定指向您的代码中的错误,拧紧你的时间并返回纯文本两次,但它不会删除 BadPaddingException

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.

这篇关于Android DES Decrypt badpaddingexception:pad block corrupted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆