Как исправить Нет такого файла или каталога в Android - PullRequest
0 голосов
/ 01 августа 2020

В моем приложении у меня есть Расшифровать файл с AES256 и CB C.
Я написал ниже коды, чтобы проверить, существует ли файл.
Но при запуске приложения он показывает мне ошибку и не может открыть мой файл!
Я использовал Log.e, чтобы показать путь к файлам, и он показывает мне этот путь. но он говорит, что не могу открыть файл!

Мой код:

    private SecureRandom r;
    private byte[] _iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    r = new SecureRandom();
    _iv = new byte[16];
    r.nextBytes(_iv);

String inputFile = getRootDirPath(context) + "/"+bookName;
                String outPutFile = getRootDirPath(context) + "/BookFile_716798_decrypt3.html";

                File file = new File(inputFile);
                if (file.exists()) {
                    try {
                        decrypt(inputFile, encryptionPassword, outPutFile, "");
                        Log.e("DecryptLog", "0");
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e("DecryptLog", "1 : " + e.getMessage());
                        Log.e("DecryptLog", "\nPath : " + inputFile);
                    } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                        Log.e("DecryptLog", "2 : " + e.getMessage());
                    } catch (NoSuchPaddingException e) {
                        e.printStackTrace();
                        Log.e("DecryptLog", "3 : " + e.getMessage());
                    } catch (InvalidKeyException e) {
                        e.printStackTrace();
                        Log.e("DecryptLog", "4 : " + e.getMessage());
                    }
                } else {
                    Toast.makeText(context, "Not", Toast.LENGTH_SHORT).show();
                }
}

    public String getRootDirPath(Context context) {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            File file = ContextCompat.getExternalFilesDirs(context.getApplicationContext(),
                    null)[0];
            return file.getAbsolutePath();
        } else {
            return context.getApplicationContext().getFilesDir().getAbsolutePath();
        }
    }

    private void decrypt(String path, String password, String _initVector, String outPath)
            throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
        FileInputStream fis = new FileInputStream(path);
        FileOutputStream fos = new FileOutputStream(outPath);
        byte[] key = (password).getBytes("UTF-8");
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16);
        SecretKeySpec sks = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

        try {
            cipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(_iv));
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        CipherInputStream cis = new CipherInputStream(fis, cipher);
        int b;
        byte[] d = new byte[8];
        while ((b = cis.read(d)) != -1) {
            fos.write(d, 0, b);
        }
        fos.flush();
        fos.close();
        cis.close();
    }

После запуска приложение покажет мне эту ошибку :

2020-08-01 08:03:59.324 28560-28560/com.app.appE/DecryptLog: 1 : : open failed: ENOENT (No such file or directory)
2020-08-01 08:03:59.324 28560-28560/com.app.app E/DecryptLog: Path : /storage/emulated/0/Android/data/com.app.app/files/BookFile_716798.html
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...