Make .txt file unreadable / uneditable

前端 未结 10 1706
北海茫月
北海茫月 2020-12-29 03:29

I have a program which saves a little .txt file with a highscore in it:

 // Create a file to write to. 
string createHighscore = _higscore + Environment.NewL         


        
相关标签:
10条回答
  • 2020-12-29 03:52

    You can't write in Resources, more information exists in this answer

    The reason that you can't change a resource string at runtime, is because the resource is compiled into your executable. If you reverse engineer the compiled *.exe or *.dll file, you can actually see your string in the code. Editing an already compiled executable file is never a good idea (unless you're trying to hack it), but when you try to do it from the executables code, it just plain impossible, as the file is locked during execution.

    • You can add Read Only or Hidden attributes to your files using File.SetAttributes, But still user can remove the attributes from windows and edit the file.

    An example:

    File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
    
    • Another way I could suggest is to save the data in a file with some weird extensions so that the user can't think of it as an editable or important file. somthing like ghf.ytr (Can't think of somthing more weird right now!)
    • I'd also suggest making a text file with .dll extension and saving it in one of windows folders like system32. This way user will have a really hard time trying to find out where does the score information go!
    0 讨论(0)
  • 2020-12-29 03:54

    Probably your best bet is securing the whole file using standard NT security and programmatically change the access control list to protect the whole file from being edited by unwanted users (excepting the one impersonating your own application, of course).

    Cryptography can't help here because the file could be still editable using a regular text editor (for example, notepad) and the end user can corrupt the file just adding an extra character (or dropping one too).

    There's an alternate approach which doesn't involve programming effort...

    Tell your users that once they've manually edited the whole text file they've lost your support. At the end of the day, if you're storing this data is because it's required by your application. Corrupting it or doing the risky task of manually editing it can make your application produce errors.

    Another alternate approach which involves programming effort...

    Whenever you change the file from your application, you can compute a MD5 or SHA hash and store in a separate file, and once you want to read or write it again, you're going to check that the whole file produces the same hash before writing on it again.

    This way, the user can still edit your file manually, but you'll know when this unexpected behavior was done by the user (unless the user also manually computes the hash whenever the file is changed...).

    0 讨论(0)
  • 2020-12-29 03:57

    Simple solution:
    To mitigate the hackish user ability to change the score, you can write it as a binary I guess.
    Another solution:
    Write the data in a SQLite DB?

    0 讨论(0)
  • 2020-12-29 04:02

    Something I have not yet seen mentioned is storing the high score on an online leader board. Obviously this solution requires a lot more development, but since you are talking about a game, you could probably make use of a third party provider like Steam, Origin, Uplay, ... This has the added advantage of leader boards not just being for your machine.

    0 讨论(0)
提交回复
热议问题