Esempio n. 1
0
        public void save(string filename = null)
        {
            if (saveFolders == null)
            {
                saveFolders = new List <string> ();
                return;
            }

            // Serializing data
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream    ms = new MemoryStream();

            bf.Serialize(ms, data);

            // Encrypt serialized data
            byte[] cipherData = null;
            cipherData = useAESEncryption ? AESEncryption.encrypt(ms.ToArray(), aesPassword, aesSalt) : ms.ToArray();

            if (filename == null)
            {
                filename = defaultFilename;
            }

            // Compute hmac to lock data on specific device
            string hmac = "";

            if (useHMAC)
            {
                hmac = Digest.computeHMACSHA1(cipherData, SystemInfo.deviceUniqueIdentifier);
            }

            foreach (string folder in saveFolders)
            {
                // Write encrypted data
                FileStream fs = File.Create(folder + filename);
                fs.Write(cipherData, 0, cipherData.Length);
                fs.Close();

                if (useHMAC)
                {
                    fs = File.Create(folder + filename + ".hash");
                    fs.Close();

                    StreamWriter sw = new StreamWriter(folder + filename + ".hash", false);
                    sw.WriteLine(hmac);
                    sw.Close();
                }
            }
        }
Esempio n. 2
0
        public bool load(string filename = null)
        {
            if (saveFolders == null)
            {
                reset();
                Debug.Log("No save folder defined");
                return(false);
            }

            if (filename == null)
            {
                filename = defaultFilename;
            }

            bool successLoad = false;

            foreach (string folder in saveFolders)
            {
                // Read encrypted data
                string path = folder + filename;
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                FileStream fs         = File.Open(path, FileMode.OpenOrCreate);
                byte[]     cipherData = new byte[fs.Length];
                fs.Read(cipherData, 0, (int)fs.Length);
                fs.Close();

                if (cipherData.Length == 0)
                {
                    reset();
                    Debug.Log("No Such a file at:" + path);
                }
                else
                {
                    // Verify that loaded data is from valid device
                    string hmac       = Digest.computeHMACSHA1(cipherData, SystemInfo.deviceUniqueIdentifier);
                    string hmacPath   = folder + filename + ".hash";
                    string storedHmac = "";
                    if (!useHMAC || File.Exists(hmacPath))
                    {
                        if (useHMAC)
                        {
                            StreamReader sr = File.OpenText(hmacPath);
                            storedHmac = sr.ReadLine();
                        }

                        if (!useHMAC || hmac == storedHmac)
                        {
                            // Decrypt encrypted data
                            byte[] plainData = null;
                            plainData = useAESEncryption ? AESEncryption.decrypt(cipherData, aesPassword, aesSalt) : cipherData;

                            // Deserialized decrypted data
                            try {
                                BinaryFormatter bf = new BinaryFormatter();
                                MemoryStream    ms = new MemoryStream(plainData);
                                data        = (Hashtable)bf.Deserialize(ms);
                                successLoad = true;
                                break;
                            } catch (SerializationException) {
                                reset();
                                Debug.Log("Can't deserialize data at:" + path);
                            }
                        }
                        else
                        {
                            // Invalid device, erase the data
                            reset();
                            Debug.Log("File has been corrupted:" + path);
                        }
                    }
                    else
                    {
                        reset();
                        Debug.Log("Hmac for this data has been lost;" + path);
                    }
                }
            }
            return(successLoad);
        }