#Encrypting JSON data
1 messages · Page 1 of 1 (latest)
Hey Steve, I implemented your code, here is how I worked it into my structure.
{
private byte[] key;
private byte[] IV;
public KeyData(string key)
{
var bytes = Encoding.UTF8.GetBytes(key);
this.key = new byte[32];
if (bytes.Length < 32)
{
Array.Copy(bytes, this.key, bytes.Length);
}
else
{
Array.Copy(bytes, this.key, 32);
}
using (Aes aes = Aes.Create())
{
aes.Key = this.key;
IV = aes.IV;
}
}
public byte[] GetKeyBytes()
{
return key;
}
public byte[] GetIVBytes()
{
return IV;
}
}```
{
_menuData.levelInfos.Clear();
foreach (var obj in _levelObjects)
{
var levelData = obj.GetComponent<Level>();
if (levelData.LevelName != "")
{
_menuData.levelInfos.Add(
new LevelInfo(levelData.LevelName,
levelData.Verified, levelData.FastestTime));
}
}
var json = JsonUtility.ToJson(_menuData);
var dataBytes = LevelEditor.DefaultEncrypt(new KeyData(LevelEditor.key), Encoding.ASCII.GetBytes(json));
File.WriteAllBytes(_dataPath, dataBytes);
}
/// <summary>
/// Opens the level json list file and creates the level objects
/// </summary>
public void Open()
{
var file = File.ReadAllBytes(_dataPath);
var dataBytes = LevelEditor.DefaultDecrypt(new KeyData(LevelEditor.key), file);
var json = Encoding.ASCII.GetString(dataBytes);
print(json);
_menuData = JsonUtility.FromJson<LevelMenuData>(json);```
issue now is that the data doesnt decrypt properly, only partially
yes, because you need to pass the same KeyData into the decrypt that you used in the encrypt
Yep i did end up figuring that out. Haven't had any errors since. Thank you so much for your patience