Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have xml file with my project and i create setup. but after creating setup the xml file will be read and write. So i want to protect the xml file while creating setup or some other way.
How can i do that?

Please Suggest me.....
Posted
Comments
Sinisa Hajnal 17-Sep-14 2:19am    
Set its attributes to ReadOnly and Hidden. Encrypt it. If it is part of the installation, put it in the setup...depends on what you need to do and what the file is for.
Sergey Alexandrovich Kryukov 17-Sep-14 2:44am    
First advice is totally useless. ReadOnly and Hidden don't provide any protection at all, can be changed by anyone, regardless of permission. Encryption needs some explanation and may not be enough.

I'll answer...

—SA
Sinisa Hajnal 17-Sep-14 2:50am    
The question is unclear. Hiding may be enough if he knows the users. Explanation of cryptography can wait until he clarifies. I'll let you answer, but don't disparage others suggestions until it is clear what is needed. You're asking why in the next comment just as I did. Although now that I read my comment, I see that I actually never asked :(
Sergey Alexandrovich Kryukov 17-Sep-14 3:22am    
I agree with you. I did not want to disparage your suggestions, but, in all cases, first part of it is useless, regardless of the requirements. It's the best to never touch these attributes which are the legacy obsolete attributes, superseded by file system permissions. Just think about it.

Besides, the question is clear enough to be answered already. Even though some detail are unknown to us, all cases can be covered. If you read my answer, perhaps you will agree.

Thank you.
—SA
Sergey Alexandrovich Kryukov 17-Sep-14 2:42am    
Protect from what? read? write? both? and why?
—SA

1 solution

Please see my comment to the question. You can consider: 1) reviewing you idea; maybe you don't really need any protection; 2) cryptography, 3) setting file permission; 4) some combination of the previous approaches.

First of all, you need to understand that you hardly can develop 100% protection, just because whatever your application can do with this file, some experienced user can do, too. You cannot give your application more permission than you user can get. So, your "protection" could be more of some fool-proof feature, pretty strong in this sense, but not in the sense of total protection.

I would first consider cryptography. I would advise to combine two techniques: cryptographic hash function and encryption. Encryption will, first of all, protect the file from reading and understanding its content. In addition, hash will make any random change to the file cryptographically infeasible. When a user change anything in the file, it will be detected by your code and the file will be considered broken; they use will have to re-install the application.

So, please see:
http://en.wikipedia.org/wiki/Cryptographic_hash_function[^],
http://en.wikipedia.org/wiki/Cryptography[^].

Here is what you can do: 1) calculate cryptographic hash function of your file, 2) put the hash value and the file in the same stream, one piece after another, 3) encrypt the result of it; 4) store the encryption key(s) in your application in the forms of static readonly array of bytes. I hope you understand how to revert this operation and restore the file. As to the hash value, you read it from the stream, read/decrypt the original file, calculate its hash and compare with the has your stored. Any difference means that the file was broken.

The selection of particular algorithms is not really critical, but for encryption, a symmetric algorithm (see the links below) will be enough.

You have it all in .NET FCL:
http://msdn.microsoft.com/en-us/library/System.Security.Cryptography%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.security.cryptography.asymmetricalgorithm%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.security.cryptography.symmetricalgorithm%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.security.cryptography.hashalgorithm%28v=vs.110%29.aspx[^].

Now, about the use of file permissions. Again, you should remember that you cannot give your application more permission than the user can get. So, I would not take it seriously. But for the fool-proof protection, the following schema could work: you give administrative permissions to the file (for reading, writing, or both). It means that you need to give administrative permissions to your application, which will require elevation of the permission by requesting confirmation from the user (UAC request dialog, see also: http://en.wikipedia.org/wiki/User_Account_Control[^]). So, this solution will also be somewhat annoying, not only pretty inefficient.

Nevertheless, I'll describe it, just in case. You can avoid the need to run your application "As Administrator" by adding and embedding appropriate application manifest in your assembly. Then the elevation will be simply automatically requested by running the application, from the very beginning. This is how: http://msdn.microsoft.com/en-us/library/bb756929.aspx[^].

—SA
 
Share this answer
 

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