Click here to Skip to main content
15,886,654 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

How can I lock a file in Windows and only give permission to a software to open it?
I have developed some scripts for a special hardware. Another software (which has written by a big company) reads my scripts and execute them on the device. I'm going to distribute the scripts and the customers shouldn't see the content of scripts or be able to copy them. I can't encrypt the scripts because if I encrypt them then the software can't read them (it doesn't have the option of reading an encrypted file).
If anyone has an idea for doing this please help me. Thank you in advance.
Posted
Comments
Prasad Khandekar 1-Jul-13 10:21am    
I am not so sure, but this won't be possible. You can give permissions to a user but not to a software. What you can try instead is to give a particular user under which Big Companies program runs, a read only and execute permissions for the folder containing your script. Also create one more account with read/write permissions for this folder and keep that account with you, You will need to update your scripts. Revoke all permissions for administrator account on the script folder.

But again I really doubt customer will allow you do such a configuration. Check what all other options available with Big Companies program which run your scripts.

Regards,

Here are some Win32 methods. I'm not sure about aware about similar methods in C#.
Here is an example to start an application from memory, by reading exe file contents from memory.

http://www.rohitab.com/discuss/topic/31681-c-run-program-from-memory-and-not-file/

Encrypted script in an executable.
Place your script in encrypted form within an application( as a resource ), whenever you need to run the script, decrypt that script and write to disk as script file. Start that script,and lock that script file by following method.

C++
HANDLE hFile1 = CreateFile(_T("Script.js"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);

    //Lock the first 1024 bytes
    BOOL bLocked = LockFile(hFile1, 0, 0, 1024, 0);
 
Share this answer
 
v2
Comments
Santhosh G_ 1-Jul-13 11:24am    
Start script, and after that lock the file.
// Decript script from your application and write to D:\\script.js[an example]
ShellExecute(0, L"open", L"D:\\script.js", NULL, NULL, SW_SHOWNORMAL);
HANDLE hFile1 = CreateFile(_T("d:\\script.js"), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
//Lock the first 1024 bytes
BOOL bLocked = LockFile(hFile1, 0, 0, 1024, 0);

After execution of script delete file from the path.
kasra515 1-Jul-13 11:24am    
If I lock the script, will the third-party software be able to read from it?
Santhosh G_ 1-Jul-13 11:47am    
If third party application need to run/start your script you have to provide your script as such.
If possible, Provide an application to third party, request them to run that application, and that application will prepare the script and execute the script.
You really can't achieve what you want here. If the information is on their disk, it's as easy as booting into Linux via a Live CD and you can ignore all Windows file permissions. If you encrypt it, you have to keep the decryption key some where in your program, and it can be decrypted if someone wants to make a little effort (or, just grab the decrypted file from memory).

Instead, your best option is to look into an obsfucator / minifier for your particular scripting language (for example, here's a JavaScript one[^]). Then, they would still have access to it, but understanding / modifying it would be difficult.
 
Share this answer
 
v2
Comments
kasra515 1-Jul-13 13:37pm    
Unfortunately the language I'm using doesn't have an obsfucator / minifier and only supports plain/text.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900