AES Decryption错误:android pad块损坏

时间:2013-12-18 10:07:59

标签: java android encryption aes badpaddingexception

我环顾堆栈找到问题的答案,但没有任何工作。 我试图实现的是加密在ASYNCtask中下载的XML文件,然后解密它。

我已经检查了什么:

- 加密和解密时生成的密钥相同,并保存在与Base64共享的参考资料中。

-IV是相同的,因为此时它的静态变量用于测试目的。

-Cipher设置为AES / CBC / PKCS5Padding

-Key设置为AES

错误出现在decryptXml()的第一行:
byte [] decrypted = cipher.doFinal(bytes);

我完全没有想法,似乎没有任何工作。我希望你的一些人能在我的代码中找到错误。谢谢你的帮助!

CODE:

genetateKey()

    SharedPreferences sharedPreferences = context.getSharedPreferences(GENERATED_KEY, Context.MODE_PRIVATE);
    String keyStr = sharedPreferences.getString(GENERATED_KEY, null);
    if (keyStr == null) {
        final int outputKeyLength = 128;
        SecureRandom secureRandom = new SecureRandom();
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(outputKeyLength, secureRandom);
        SecretKey key = keyGenerator.generateKey();
        byte[] bytes = key.getEncoded();
        keyStr = Base64.encodeToString(bytes, Base64.DEFAULT);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(GENERATED_KEY, keyStr);
        editor.commit();
        return key.toString();
    }  else {
        return keyStr;
    }

XML加密:

    connection = (HttpURLConnection) url.openConnection();
    connection.connect();
    SecretKey secretKey = getSecretKey(context);
    SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    spec = generateIv(cipher.getBlockSize());
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, spec);

    input = connection.getInputStream();
    cis = new CipherInputStream(input, cipher);

    String FILEPATH = context.getFilesDir().getParentFile().getPath();
    File file = new File(FILEPATH, "/download/" + id + "/");
       if (!file.exists()) {
    file.mkdirs();
    }

    xmlFile = new File(FILEPATH + "/download/" + id + "/", "xmldata.xml");
    output = new FileOutputStream(xmlFile);
    cos = new CipherOutputStream(output, cipher);
    byte data[] = new byte[4096];
    int count;
    while ((count = cis.read(data)) != -1) {
       if (isCancelled()) throw new TaskCanceledException();
          cos.write(data, 0, count);
          progress = -1;
          publishProgress();
    }
    if (isCancelled()) throw new TaskCanceledException();

解密:

public String decryptXml() {
    String data = null;
    File file = new File(context.getFilesDir().getParentFile().getPath() + "/download/" + id + "/xmldata.xml");
    int size = (int) file.length();
    byte[] bytes = new byte[size];
 try {
        SecretKeySpec secretKeySpec = new SecretKeySpec(getSecretKey(context).getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, DownloadBookAsyncTask.spec);
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        bis.read(bytes, 0, bytes.length);
        bis.close();
        byte[] decrypted = cipher.doFinal(bytes);
    }

getSecretKey():

public SecretKey getSecretKey(Context context){
    SharedPreferences sharedPreferences = context.getSharedPreferences(DashboardFragment.GENERATED_KEY, Context.MODE_PRIVATE);
    String stringKey = sharedPreferences.getString(DashboardFragment.GENERATED_KEY, null);
    byte[] encodedKey = Base64.decode(stringKey, Base64.DEFAULT);
    return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
}

修改

添加IV生成器方法

public AlgorithmParameterSpec generateIv(int size) throws NoSuchAlgorithmException {
    AlgorithmParameterSpec ivspec;
    byte[] iv = new byte[size];
    new SecureRandom().nextBytes(iv);
    ivspec = new IvParameterSpec(iv);
    return ivspec;
}

2 个答案:

答案 0 :(得分:3)

好的,我发现了这个问题。 我的代码不起作用的原因是我在加密中使用了CipherInputStream而我不应该这样做。 我还重做了整个解密方法,现在看起来像这样:

  byte[] wholeFileByte = null;
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, DownloadBookAsyncTask.ivspec);
    File file = new File(context.getFilesDir().getParentFile().getPath() + "/download/" + id + "/xmldata.xml");
    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    byte data[] = new byte[4096];
    int count;
    while ((count = cis.read(data)) != -1) {
        bos.write(data, 0, count);
    }

    if(cis != null)
        cis.close();
    if(bos != null)
        bos.close();
    if(fis != null)
        fis.close();

    wholeFileByte = bos.toByteArray();
    String kk = new String(wholeFileByte, "UTF-8");

我认为我犯的其他错误是我在解密中使用了doFinal,即使Cipher已经解密了,这也是我的一些错误的根源。

感谢@GariBN,因为你把我放在了正确的轨道上,当我的代表允许我这样做时,我会向你投票:)

答案 1 :(得分:1)

您创建IV以加密明文。 我不确定你是否使用相同的IV来解密密文。

通常,您希望将IV连接到密文,并在解密时读取它(前16个字节),然后使用用于初始化的IV的密码解密所有其他字节(密文)加密。

例如,如果使用以下方法加密:

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, spec);

首先,尝试使用以下方法解密(稍后)

cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, spec);
cipher.doFinal(bytes)

如果你成功了,那么问题可能是因为不合适的IV,你可以轻松修复它。

相关问题