为了保护应用程序存储的安全,我使用
EncryptedStorage
对存储进行了加密,如此处所述。也就是说,
EncryptedStorage.install("your-pass-encryption-key")
我创建了一个文本文件,供另一台设备中的同一应用程序共享和打开/使用。
问题是该文件无法在其他设备中打开/使用。它说这个文件已加密。
如何解密它以便能够在另一设备的同一应用程序中打开或使用它?
编辑
我执行了以下操作来创建要共享的文件作为 .txt 文件,以便能够使用
plain/text
mime 类型来共享它。
String databasePath = Display.getInstance().getDatabasePath("TestDB.db");
String mimeType = "application/vnd.sqlite3";
mimeType = "application/x-sqlite3";
mimeType = "plain/text";
try {
FileSystemStorage fss = FileSystemStorage.getInstance();
InputStream is = fss.openInputStream(databasePath);
//Copy from FSS to Storage
Util.copy(is, Storage.getInstance().createOutputStream("TestDB.txt"));
String fileToSharePath = FileSystemStorage.getInstance().getAppHomePath() + "TestDB.txt";
Log.p("FileToSharePath " + fileToSharePath);
Display.getInstance().share(fileToSharePath, null, mimeType);
} catch (IOException e) {
Log.p("Share error: " + e);
}
我执行了以下操作,在另一台设备中使用
FileChooser
浏览共享文件,并将其另存为应用程序数据库目录中的 .db 文件。
FileChooser.showOpenDialog("txt", ev -> {
if (ev == null) {
Log.p("no file selected");
} else {
String filePath = (String) ev.getSource();
Log.p("FilePath " + filePath);
try {
FileSystemStorage fss = FileSystemStorage.getInstance();
InputStream is = fss.openInputStream(filePath);
String databasePath = Display.getInstance().getDatabasePath("TestDB.db");
OutputStream os = FileSystemStorage.getInstance().openOutputStream(databasePath);
Util.copy(is, os);
} catch (IOException e) {
Log.p("Create DB error: " + e);
}
}
});
您在这一行中使用
Storage
而不是 FileSystemStorage
:
Util.copy(is, Storage.getInstance().createOutputStream("TestDB.txt"));
这会加密文件,请注意,这无论如何都不能在设备上一致地工作...但是然后您再次使用
FileSystemStorage
来读取数据(已加密)。
String fileToSharePath = FileSystemStorage.getInstance().getAppHomePath() + "TestDB.txt";
解决方案是使用
FileSystemStorage
来分享您想要分享的所有内容。用这种方式加密数据库文件无论如何是行不通的。