Encrypt/Decrypt game level data

Hi!

I have a level editor in the works and I don’t want players to change anything outside the editor and right now I am basically saving it as a txt with a custom file format. So when they open it they can change the game time, level author e.t.c. When it is loaded I create a new object and assign the parameters to it. When I save it is basically file.WriteLine(object stuff);. So how would I encrypt the whole file when it is saved and then decrypt it when it is going to be loaded?

Thanks in advance

Use a BinaryFormatter to serialize your data to a file that can’t be modified without deserializing it again first (which would only happen in your editor’s saved/load functions).

Here is an example taken from my own leveleditor’s map loading and saving.
Game is a class that holds all the data you want to save. Every class that is to be serialized needs to be [System.Serializable].
The Load function takes a string that is just the name of the savegame file that is to be loaded. It assigns the loadedGame variable (you can also make it return it) so other scripts can access it and pull any data they need for loading the level (player hitpoints, for example).
Be aware that not every type is serializable. Things that are specific to Unity (GameObject, Transform, Vector3 and such) need to be flagged with [System.NonSerialized] before their variable declaration. This counts for EVERY class that will be used when saving and loading.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad {

	public static Game loadedGame = new Game();
	public static List<Game> savedGames = new List<Game>();

	//it's static so we can call it from anywhere
	public static void Save(Game saveGame) {
		
		BinaryFormatter bf = new BinaryFormatter();
		FileStream file = File.Create ("C:/Savegames/" + saveGame.savegameName + ".sav"); //you can call it anything you want
		bf.Serialize(file, saveGame);
		file.Close();
		Debug.Log("Saved Game: " + saveGame.savegameName);

	}	
	
	public static void Load(string gameToLoad) {
		if(File.Exists("C:/Savegames/" + gameToLoad + ".sav")) {
			BinaryFormatter bf = new BinaryFormatter();
			FileStream file = File.Open("C:/Savegames/" + gameToLoad + ".gd", FileMode.Open);
			loadedGame = (Game)bf.Deserialize(file);
			file.Close();
			Debug.Log("Loaded Game: " + loadedGame.savegameName);
		}
	}


}

Theoretically, you could just take the string that would be put into the txt file of yours, and store it aas a string variable inside the instance of the Game variable that is used as a savegame.