Make .txt file unreadable / uneditable

前端 未结 10 1754
北海茫月
北海茫月 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:44

    As you seem to look for relatively low security, I'd actually recommend going for a checksum. Some pseudo-code:

    string toWrite = score + "|" + md5(score+"myKey") + Environment.NewLine
    

    If the score would be 100, this would become

    100|a6b6b0a8e56e42d8dac51a4812def434

    To make sure the user didn't temper with the file, you can then use:

    string[] split = readString().split("|");
    if (split[1] != md5(split[0]+"myKey")){
         alert("No messing with the scores!");
    }else{
         alert("Your score is "+split[0]);
    }
    

    Now of course as soon as someone gets to know your key they can mess with this whatever they want, but I'd consider that beyond the scope of this question. The same risk applies to any encryption/decryption mechanism.

    One of the problems, as mentioned in the comments down below, is that once someone figures out your key (through brute-forcing), they could share it and everybody will be able to very easily change their files. A way to resolve this would be to add something computer-specific to the key. For instance, the name of the user who logged in, ran through md5.

    string toWrite = score + "|" + md5(score+"myKey"+md5(System.username /**or so**/)) + Environment.NewLine
    

    This will prevent the key from being "simply shared".

提交回复
热议问题