Android AES расшифровывает BadPaddingException - PullRequest
0 голосов
/ 12 апреля 2020

Я пытаюсь зашифровать файлы с помощью aes. Расшифровываю. Работает нормально, хорошо расшифровывается. Но иногда это дает мне BadPaddingException. Объясните: при тестировании приложения я пытаюсь зашифровать и расшифровать файл 10 раз. Хорошо работает 8 раз. me BadPaddingException дважды Это зашифрованный код

public  void enn(String path , String name ,byte[] salt ,byte[] IV) throws IOException,InvalidKeyException,InvalidAlgorithmParameterException,BadPaddingException,InvalidParameterSpecException,IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException ,InvalidKeySpecException {
    FileInputStream inFile = new FileInputStream(path);
    FileOutputStream outFile = new FileOutputStream(path+".mine");
    String password = pass;
    IvParameterSpec ivSpec = new IvParameterSpec(IV);
    SecretKeyFactory factory = SecretKeyFactory
        .getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536,256);
    SecretKey secretKey = factory.generateSecret(keySpec);
    SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret,ivSpec);
    byte[] input = new byte[64];
    int bytesRead;
    while ((bytesRead = inFile.read(input)) != -1) {
        byte[] output = cipher.update(input, 0, bytesRead);
        if (output != null)
            outFile.write(output);
    }
    byte[] output = cipher.doFinal();
    if (output != null)
        outFile.write(output);
    outFile.flush();
    outFile.close();
    inFile.close();
    File fl= new File(path);
    fl.delete();
}
...