0

Locker Apps is a Folder Lock application, how can I lock files or folders on Windows using Java?

Here is what I tried:

File file = new File(file path);
file.setExecutable(false); 
file.setWritable(false); 
file.setReadable(false);

But only file.setwritable(false); works - I can't rewrite the file. Other methods do nothing to the file.

byxor
  • 5,930
  • 4
  • 27
  • 44

1 Answers1

0

These methods are simply setting the file system access permissions for the file. In order to understand them, you need to understand the OS / file system access control rules.

For example, on Linux file.setWriteable(false) is equivalent to running chmod u-w file to clear the "owner write" access bit:

Note:

  1. chmod can only change any permissions on the file if you are the owner of the file, or if you are running as root.

  2. The owner write permission means that owner (or root) can execute the file, or not. If you are neither, then that permission does not affect you.

So there are at least two possible reasons why setWriteable may not be "working".

  • You are not executing your Java program as the file owner or root. In that case, setWriteable will return false. But note that you don't check the result!

  • You are not trying to write the file as the file's owner. In that case, the access but you just removed (in Java) does not affect you.


Solutions:

There is a newer / better API for setting file permissions in Java in the java.nio.file.Files class:

public static Path setPosixFilePermissions(
         Path path,
         Set<PosixFilePermission> perms)
     throws IOException

This allows you to set / clear all avail POSIX file permissions in one call, and it with throw an exception if it can't for any reason.

Many file systems also support more sophisticated ACL mechanisms ....

HOWEVER

None of this is equivalent to the functionality provided by a File Locker app. Why? Because any attempt to block file access via file permissions or ACLs can trivially be reversed by someone with root access. Provided you know what it is doing at a technical level.

What the Windows "Folder Lock" app can apparently encrypt files or hide them. Files can be marked as hidden natively in Java 7 and later by setting the "dos:hidden" attribute; see https://stackoverflow.com/a/42816082/139985. It is probably also using permissions and ACLs to restrict access, that can be doing on Java too. It may also be doing other things. The documentation says (unhelpfully!):

Once the files are locked, it becomes protected in every possible way and can only be accessed through the Folder Lock security application.

That sounds like marketing rather than truth, and is almost certainly untrue. However figuring out what the app is actually doing, would require me to get a Windows machine, download and install the software, and then violate the license by attempting to reverse engineer it.

But note that the above method for "hiding" is Windows specific. On Linux / Mac OS you "hide" a file from some commands by making the first character of the file name a dot (".") character.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • so what File locker apps use to lock a file? – sherif shawqi Apr 04 '18 at 18:01
  • They typically use encryption. Which specific file locker app are you talking about? – Stephen C Apr 04 '18 at 22:29
  • Folder locker, and it got the two options - encryption or lock file- , if you use file locking you can't access it using windows file explorer.- like it doesn't exist- but it's accessible when using any other OS if you using two on your machine. – sherif shawqi Apr 05 '18 at 11:12