【问题标题】:Work with encrypted file and provide them with FileProvider使用加密文件并为它们提供 FileProvider
【发布时间】:2019-11-25 15:23:40
【问题描述】:

在我的 Android 应用程序中,我需要从 FileProvider 公开一些文件。没有加密,这很容易:我只需将FileProvider 添加到manifest.xml

<provider
   android:name="androidx.core.content.FileProvider"
   android:authorities="${applicationId}.provider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
     android:name="android.support.FILE_PROVIDER_PATHS"
     android:resource="@xml/provider_paths" />
</provider>

我需要接收一些文件,加密它们(将其秘密存储在数据库中),然后使用 FileProvider 公开它们。我该怎么做?

谢谢

【问题讨论】:

  • 加密文件仍然是文件,对吧?或者您是否希望有一些特定的自动化,以便您收到解密的文件?
  • 我需要在本地文件系统上存储一个加密文件,但我希望导出解密文件。
  • 我猜这个技巧隐含在文档的这一部分中:“如果你想覆盖 FileProvider 方法的任何默认行为,扩展 FileProvider 并在 &lt;provider&gt; 元素的 android:name 属性中使用完全限定的类名。“但我没有尝试过,所以我不能确定。
  • 否则ContentProvider 可能更有意义...
  • 问题(和目标答案)是如何编写从加密文件开始提供未加密文件的 FileProvider/ContentProvider 吗?我也会对此感兴趣。

标签: java android cryptography android-fileprovider fileprovider


【解决方案1】:

您可以滚动您自己的FileProvider,例如this question。根据您要提供的文件类型和应该查看它的应用程序,您可以选择一种策略,这在this question 中进行了一些讨论。

例如,Word 应用程序可以很好地使用管道ParcelFileDescriptor,对于图像,您可能需要创建文件的临时副本并提供它。

这是一个如何查找 Word 文件和类似文件的示例:

@Nullable
@Override
public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) throws FileNotFoundException {
    ParcelFileDescriptor[] pipe = null;
    try {
        pipe = ParcelFileDescriptor.createReliablePipe();
    } catch (IOException e) {
        Log.d(TAG, "Error creating pipe", e);
    }
    if (mode.contains("r")) {
        FileInputStream fis = FileEncryptionWrapper.getEncryptedFileInputStream(getContext(), uri);
        new PipeFeederThread(fis, new ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])).start();
        return pipe[0];
    } else if (mode.contains("w")) {
        FileOutputStream fos = FileEncryptionWrapper.getEncryptedFileOutputStream(getContext(), uri);
        new PipeFeederThread(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]), fos).start();
        return pipe[1];
    }
    return null;
}

它使用PipeFeederThread 将内容从您的 Stream 获取到读/写端:

static class PipeFeederThread extends Thread {
    InputStream in;
    OutputStream out;

    PipeFeederThread(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    @Override
    public void run() {
        byte[] buf = new byte[8192];
        int len;

        try {
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e(TAG, "PipeFeederThread: Data transfer failed:", e);
        }
    }
}

FileProvider 也需要在AndroidManifest.xml 中声明:

<provider
    android:name=".InternalFileProvider"
    android:authorities="com.android.prototypes.encryptedimagefileprovider.InternalFileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

还有file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
   <files-path name="files" path="./" />
</paths>

不幸的是,到目前为止,我还没有找到一个好的“一刀切”的解决方案,并且仍在寻找。似乎还没有以干净一致的方式解决将不同类型的加密文件导出到其他应用程序的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2016-01-10
    • 2012-07-19
    • 2017-07-28
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多