- Home /
Why is my crypted save file empty on android while it work on computer?
Hey, I'm a beginner to Unity and I'm making a simple game where I need to save some data but I don't want to use PlayerPrefs, I was using them when I had like 2 int variables... Right now I want to use a crypted text file stocker in Application.persistentDataPath + "/infos.txt", which is crypted from a temp file at Application.persistentDataPath + "/temp.txt". It work on computer (Under windows) but on my android phone, when I test the game this infos file stay empty. (I had realised som tests and I know that when I don't crypt the infos file, it works , so it surely comes from here)
Here is my save function:
public void Save()
{
// Save
if (canLoadOrSave)
{
string[] content = new string[] {
// In this string[] I store variables to save
};
// separator = "%%"
string saveString = string.Join(separator, content);
File.WriteAllText(Application.persistentDataPath + "/temp.txt", saveString);
// Encrypt
gameObject.GetComponent<Cryptage>().EncryptFile("gfhgthuhjklmaznq", "gfhgthuhjklmaznq", Application.persistentDataPath + " /temp.txt", Application.persistentDataPath + "/infos.txt");
// This line is the line to not crypt infos file, on load there is a line to get this back without decrypt
// File.WriteAllText(Application.persistentDataPath + "/infos.txt", File.ReadAllText(Application.persistentDataPath + "/temp.txt"));
}
}
And here is the crypting (Only encrypt) script (To be honest, I took it from a tutorial on "How to crypt files")
public void EncryptFile(string strKey, string strIv, string pathPlainTextFile, string pathCypheredTextFile) { // Place la clé de déchiffrement dans un tableau d'octets byte[] key = Encoding.UTF8.GetBytes(strKey);
// Place le vecteur d'initialisation dans un tableau d'octets
byte[] iv = Encoding.UTF8.GetBytes(strIv);
FileStream fsCypheredFile = new FileStream(pathCypheredTextFile, FileMode.Create);
RijndaelManaged rijndael = new RijndaelManaged();
rijndael.Mode = CipherMode.CBC;
rijndael.Key = key;
rijndael.IV = iv;
ICryptoTransform aesEncryptor = rijndael.CreateEncryptor();
CryptoStream cs = new CryptoStream(fsCypheredFile, aesEncryptor, CryptoStreamMode.Write);
FileStream fsPlainTextFile = new FileStream(pathPlainTextFile, FileMode.OpenOrCreate);
int data;
while ((data = fsPlainTextFile.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
fsPlainTextFile.Close();
cs.Close();
fsCypheredFile.Close();
// File.Delete(pathPlainTextFile);
}
The third arg is the path for file to encrypt, the fourth arg the target file
If you need more things tell me, sorry for my english and thanks for your answers ;-),
Your answer
![](https://koobas.hobune.stream/wayback/20220612232247im_/https://answers.unity.com/themes/thub/images/avi.jpg)