Ejemplo n.º 1
0
        private static bool Upgrade(int header, ushort version)
        {
            List <DataItem> items;

            using (BinaryReader reader = new BinaryReader(new BufferedStream(File.OpenRead(StoragePath), 1024)))
            {
                if (reader.ReadUInt16() != FILE_HEADER_V0)
                {
                    return(false);
                }

                var key   = new byte[] { 99, 12, 55, 17, 45, 97, 83, 64, 38 };
                var count = reader.ReadUInt16();
                var scope = Settings.EncryptionScope.CurrentUser;
                var o     = Settings.Encryption.Value;
                if (o != null)
                {
                    scope = o.Scope;
                }

                items = new List <DataItem>(count + 1);

                using (var crypto = new Cryptography.Crypto())
                {
                    for (int i = 0; i < count; i++)
                    {
                        var name   = reader.ReadString();
                        var length = reader.ReadUInt16();
                        var data   = reader.ReadBytes(length);

                        data = ProtectedData.Unprotect(data, key, DataProtectionScope.CurrentUser);

                        try
                        {
                            items.Add(new DataItem()
                            {
                                ID   = name,
                                Data = crypto.Compress(crypto.Encrypt(scope, data), Cryptography.Crypto.CryptoCompressionFlags.All),
                            });
                        }
                        finally
                        {
                            Array.Clear(data, 0, data.Length);
                        }
                    }
                }
            }

            if (items.Count == 0)
            {
                return(false);
            }

            GetStorage().Write <DataItem>(items);

            return(true);
        }
Ejemplo n.º 2
0
        private static void OnCryptoChanged()
        {
            var store = GetStorage();
            var items = new List <DataItem>();
            var o     = Settings.Encryption.Value;
            var scope = o != null ? o.Scope : Settings.EncryptionScope.CurrentUser;

            using (var crypto = new Cryptography.Crypto())
            {
                try
                {
                    foreach (var item in store.ReadAll <DataItem>())
                    {
                        try
                        {
                            if (crypto.GetScope(item.Data) == scope)
                            {
                                continue;
                            }

                            var data = crypto.Decrypt(item.Data);

                            try
                            {
                                items.Add(new DataItem()
                                {
                                    ID   = item.ID,
                                    Data = crypto.Compress(crypto.Encrypt(scope, data), Cryptography.Crypto.CryptoCompressionFlags.All),
                                });
                            }
                            finally
                            {
                                Array.Clear(data, 0, data.Length);
                            }
                        }
                        catch (Exception e)
                        {
                            Util.Logging.Log(e);
                        }
                    }
                }
                catch { }
            }

            if (items.Count > 0)
            {
                store.Write <DataItem>(items);
            }
        }
Ejemplo n.º 3
0
        public byte[] ToArray(Crypto crypto)
        {
            byte[] buffer;

            if (this.data != null)
            {
                if (this.crypto.IsEqual(crypto))
                {
                    return(this.data);
                }

                buffer = this.crypto.Decrypt(data);
            }
            else if (this.value != null)
            {
                buffer = Credentials.ToByteArray(this.value);
            }
            else
            {
                return(new byte[0]);
            }

            try
            {
                var b = crypto.Compress(crypto.Encrypt(buffer));

                this.data   = b;
                this.crypto = crypto;

                return(b);
            }
            finally
            {
                Array.Clear(buffer, 0, buffer.Length);
            }
        }
Ejemplo n.º 4
0
        public static void SetPassword(string username, SecureString password)
        {
            lock (_lock)
            {
                try
                {
                    if (cache == null)
                    {
                        cache = new Dictionary <string, SecureString>(StringComparer.OrdinalIgnoreCase);
                    }

                    var s = password.Copy();
                    s.MakeReadOnly();

                    cache[username] = s;
                }
                catch (Exception ex)
                {
                    Util.Logging.Log(ex);
                }

                if (state != StorageState.None)
                {
                    try
                    {
                        byte[] data;

                        if (password.Length > 0)
                        {
                            var buffer = ToByteArray(password);

                            try
                            {
                                var scope = Settings.EncryptionScope.CurrentUser;
                                var o     = Settings.Encryption.Value;
                                if (o != null)
                                {
                                    scope = o.Scope;
                                }

                                using (var crypto = new Cryptography.Crypto())
                                {
                                    data = crypto.Compress(crypto.Encrypt(scope, buffer), Cryptography.Crypto.CryptoCompressionFlags.All);
                                }
                            }
                            finally
                            {
                                Array.Clear(buffer, 0, buffer.Length);
                            }
                        }
                        else
                        {
                            data = new byte[0];
                        }

                        var item = new DataItem()
                        {
                            ID   = username,
                            Data = data,
                        };

                        GetStorage().Write <DataItem>(item);
                    }
                    catch (Exception e)
                    {
                        Util.Logging.Log(e);
                    }
                }
            }
        }