java - 使用加密文件并为它们提供 FileProvider

标签 java android cryptography android-fileprovider fileprovider

在我的 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>

我需要接收一些文件,对它们进行加密(将其 secret 存储在数据库中),然后使用 FileProvider 公开它们。我怎样才能做到这一点?

谢谢

最佳答案

您可以像 this question 中那样推出自己的 FileProvider 。根据您想要提供的文件类型以及应该查看该文件的应用程序,您可以选择一种策略,这在 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>

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

关于java - 使用加密文件并为它们提供 FileProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59035057/

相关文章:

java - 启动完成后 BroadcastReceiver 不工作

android - Google 电视模拟器 AVD 系统镜像

node.js - Electron 在单独的线程上运行加密 Diffie Hellman key 生成

android - 9patch 图像拉伸(stretch)不正确

c# - 如何将熵引入编码的 Base64 字符串?

cryptography - 多个 sha1 哈希的 Sha1 哈希 -> 安全识别文件?

Java - 通过数组存储的碰撞检测碰撞

java - Spring XML 配置还在使用吗?

java - 将 JavaScriptObject 转换为 gwt 小部件

android - 如何在 Android Studio 中指定 View 相对于其宽度的高度?