Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nedcallescarlett · May 22, 2017 at 03:12 PM · javascriptfilehighscoressystem.ioencryption

How do I make it so players can't edit the file but the game can? File.CreateText

So my high score system is a little weird. I use the File.CreateText thing to do it, here is the script if you need it: (Java and its called ScoreSave)

  import System.IO;
 
  var fileName = "hs.data";
  var ScoreAmount : int;
  
  var HighScore : int;
  
  function Start (){
      HighScore = ScoreLoad.CompareScore;
      ScoreAmount = GlobalScore.CurrentScore;
      if (ScoreAmount >= HighScore){
          var OurFile = File.CreateText(fileName);
          OurFile.WriteLine (ScoreAmount);
          OurFile.Close();
      }
  }

Also here is my script for loading the score if that is needed for explanation (java and its called ScoreLoad):

      import System.IO;
      
      var fileName = "HS.data";
      var ScoreLoad : String;
      var HighScoreDisplay : GameObject;
      
      var line : String;
      
      static var CompareScore : int;
      
      function Start(){
          var sr : StreamReader = new StreamReader(fileName);
      
          line = sr.ReadLine();
          while (line != null){
              ScoreLoad = line;
              line = sr.ReadLine();
      
          }
          sr.Close();
      
          HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
      
        
 
   CompareScore = int.Parse(ScoreLoad);
  }

My question is how do I make it so in the final build, players wont just be able to go to the game files, go into the file called hs.data and change their high score to a high number aka cheat? How do I like encrypt it or lock it or make it permanently read only or something? Anything helps, thank you! Also it needs to be able to save the highscore when it gets a new one so would read only not work? Help please. Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Namey5 · May 24, 2017 at 10:13 AM

I see two ways of doing it. Either set the Read/Write access of the file to something secure, i.e.

 File.SetAttributes (fileName, FileAttributes.Hidden);
 File.SetAttributes (fileName, FileAttributes.ReadOnly);

Or, you could use a binary formatter to convert the text to binary; unreadable and unchangeable for anything but the system that encoded it.

   import System.IO;
   import System.Runtime.Serialization.Formatters.Binary;    //I know, it's a bit long eh? 
 
   var fileName = "hs.data";
   var ScoreAmount : int;
   
   var HighScore : int;
   
   function Start (){
       HighScore = ScoreLoad.CompareScore;
       ScoreAmount = GlobalScore.CurrentScore;
       if (ScoreAmount >= HighScore){
           var bf = new BinaryFormatter ();
           var OurFile = File.Open (fileName, FileMode.OpenOrCreate);
           bf.Serialize (OurFile, ScoreAmount);
           OurFile.Close();
       }
   }
 
 ...
 
 //Then for loading the file;
 
 var bf = new BinaryFormatter ();
 var OurFile = File.Open (fileName, FileMode.Open);
 line = bf.Deserialize (OurFile).ToString();
 OurFile.Close ();

There might be an error in the "bf.Deserialize()" line, simply because it's been a while since I did deserialization in JS, so if you use this method and have difficulties just leave a comment.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · May 25, 2017 at 02:35 AM 1
Share

Both solutions are neither secure nor does an attacker need to know anything about the system that saved the information in order to read and modify the information. $$anonymous$$arking a file as hidden and read only can be reverted even without any advanced knowledge.

The binary serialization format is well documented and quite trivial to read / modify. You can even reconstruct the actual datastructure used (at least the members that got serialized).

Finally there is no way to store such information on the user's side and prevent modifications. It would be possible to encrypt the data or store additional checksums, however your code can be decompiled very easily so any encryption or checksum calculations can be bypassed.

avatar image Namey5 Bunny83 · May 25, 2017 at 04:31 AM 1
Share

Judging by the wording of the question, we probably aren't dealing with hackers here. This solution is more to stop people casually co$$anonymous$$g across the file and potentially modifying data.

avatar image ChompIV · May 29, 2017 at 04:30 AM 0
Share

The save one went good, however where do I put the load code? this is what I have now: import System.IO;

 var fileName = "hs.data";
 var ScoreLoad : String;
 var HighScoreDisplay : GameObject;
 
 var line : String;
 
 static var CompareScore : int;
 
 function Start(){
     var sr : StreamReader = new StreamReader(fileName);
     var bf = new BinaryFormatter ();
     var OurFile = File.Open (fileName, File$$anonymous$$ode.Open);
     {
     line = bf.Deserialize (fileName).ToString();
     OurFile.Close ();
     }
     sr.Close();
     
 
     HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
 
     CompareScore = int.Parse(ScoreLoad);
 
     
 
 }

I think that's right? And im getting the error:

Assets/ScoreLoad.js(16,27): BCE0023: No appropriate version of 'System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize' for the argument list '(String)' was found.

avatar image Namey5 ChompIV · May 29, 2017 at 05:52 AM 0
Share

$$anonymous$$y bad, made a typo in the deserialization statement. Answer has been updated. Now if it returns an error, I'm guessing it's to do with type casting. But from memory that isn't a thing in JS, so it should work. As for the placement, just replace all the old loading stuff with that. I may question as to the extra set of braces surrounding the loading code, however. They shouldn't need to be there.

avatar image ChompIV · May 29, 2017 at 04:30 AM 0
Share

btw for your save code I just copied and pasted it replacing the old one, thanks!

avatar image Nedcallescarlett · May 29, 2017 at 06:09 AM 0
Share

I need the loaded script to display it too? This is what I have now:

    import System.IO;
    import System.Runtime.Serialization.Formatters.Binary;    //I know, it's a bit long eh? 
  
 var fileName = "hs.data";
 var ScoreAmount : int;
    
 var HighScore : int;
    
 function Start (){
     HighScore = ScoreLoad.CompareScore;
     ScoreAmount = GlobalScore.CurrentScore;
     if (ScoreAmount >= HighScore){
         var bf = new BinaryFormatter ();
         var OurFile = File.Open (fileName, File$$anonymous$$ode.OpenOrCreate);
         bf.Serialize (OurFile, ScoreAmount);
         OurFile.Close();
     }
 }
avatar image Namey5 Nedcallescarlett · May 29, 2017 at 07:03 AM 0
Share

What exactly is the issue? Just replace the loading stuff from your old script with that above (which should now be fixed).

avatar image Nedcallescarlett Namey5 · May 29, 2017 at 11:12 PM 0
Share

So for saving the score, I used the script you provided. For loading I did:

 import System.IO;
 
 var fileName = "hs.data";
 var ScoreLoad : String;
 var HighScoreDisplay : GameObject;
 
 var line : String;
 
 static var CompareScore : int;
 
 var bf = new BinaryFormatter ();
 var OurFile = File.Open (fileName, File$$anonymous$$ode.Open);
 line = bf.Deserialize (OurFile).ToString();
 OurFile.Close ();
 
     HighScoreDisplay.GetComponent.<Text>().text = "" + ScoreLoad;
 
     CompareScore = int.Parse(ScoreLoad);

Im getting a problem where I load in and it says high score is 0, then i go and get points and it remains 0 like it isnt loading it? (I posted the entire load script start to finish) (Im not an expert and sort of a noob and thank you so much for helping and i am stuck)

Show more comments
avatar image Nedcallescarlett · May 30, 2017 at 10:29 PM 0
Share

THAN$$anonymous$$ YOU!

avatar image
3

Answer by shadowpuppet · May 24, 2017 at 09:26 PM

not an answer, just an observation as I "struggled" with this very same thing. But in the end I said "who cares" . Games are meant to be fun and played as designed so if some snot nosed punk wants to cheat and pad his score, by all means have at it. Unless you're making a AAA title for massive online play or there is some tangible reward for getting a high sore like bit coins I say let them hack it. Let them have that rush of pseudo superiority

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to encrypt/lock a File.CreateText file? (its my highscore) 1 Answer

How would I transfer these into PlayerPrefs? 0 Answers

High Score with player prefs confusion (JAVA) 0 Answers

sending .png using WWWForm 1 Answer

UnauthorizedAccessException: Access to the path is denied 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges