我知道 Android 在创建文件时支持 MODE_PRIVATE 标志,这样其他应用程序就无法访问它,但是有没有办法添加数字签名或应用程序可以在启动时检查的内容?
我正在开发一个用作在线服务支持功能的应用程序,因此它能够从运行该服务的服务器下载数据。然而,这应该在使用应用程序之前执行,并且仅当设备上的数据与最新数据(保存在服务器上)不同时才执行。
我最初的想法是检查文件大小是否匹配或上次修改日期是否匹配。因此,如果我使用 FileOutputStream
并在打开文件进行写入时设置标志 MODE_PRIVATE,是否会阻止用户从文件管理器或任何其他应用程序访问/删除文件?
如果没有,您能否建议一种在 MainActivity
的 onDestroy()
方法中向文件添加数字签名的方法以及一种检查方法如果文件中的签名与应用程序创建的签名匹配(尽管该文件也会保存到文件中,可能是 sharedpreferences.xml
)?
最佳答案
I know that Android supports MODE_PRIVATE flag when creating a file so that no other application can access it
这是内部存储的默认行为。
however is there a way to like add a digital signature or something that the application can check on startup?
欢迎您为文件创建数字签名。欢迎任何修改文件的人修改数字签名。
will that prevent the user from accessing/deleting the file(s) from file manager or any other app?
普通用户将无法访问内部存储中的文件。 root 设备的用户可以选择访问内部存储。
If not, could you suggest a method to add a digital signature to the file(s) in the onDestroy() method of the MainActivity and a method to check if the signature in the file(s) matches with the one that the application made (although that would've also been saved to a file, perhaps sharedpreferences.xml) ?
那将是无用的,因为修改文件的人可以将签名修改为有效。毕竟,您的应用程序是进行签名的应用程序,因此私钥必须存在于应用程序或设备上,这意味着谁可以访问文件,谁就可以访问 key 。
对于您没有修改而只是下载的文件,您可以为在服务器 上创建的文件创建数字签名(其中保存有私钥)。然后,您的应用程序只需要公钥来验证签名。但是,如果您的应用正在修改文件,这将不起作用。
关于java - 如何保证应用数据的完整性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18113014/