Пример #1
0
        public static SecureString GetPassword(string username)
        {
            lock (_lock)
            {
                if (cache != null)
                {
                    SecureString s;
                    if (cache.TryGetValue(username, out s))
                    {
                        return(s);
                    }
                }
                else
                {
                    cache = new Dictionary <string, SecureString>(StringComparer.OrdinalIgnoreCase);
                }

                if (state != StorageState.None)
                {
                    try
                    {
                        var i = GetStorage().Read <DataItem>(username);

                        using (var crypto = new Cryptography.Crypto())
                        {
                            SecureString s;

                            if (i.Data.Length > 0)
                            {
                                var data = crypto.Decrypt(i.Data);

                                try
                                {
                                    s = FromByteArray(data);
                                }
                                finally
                                {
                                    Array.Clear(data, 0, data.Length);
                                }
                            }
                            else
                            {
                                s = new SecureString();
                                s.MakeReadOnly();
                            }

                            cache.Add(username, s);

                            return(s);
                        }
                    }
                    catch (Exception e)
                    {
                        Util.Logging.Log(e);
                    }
                }
            }

            return(null);
        }
Пример #2
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);
        }
Пример #3
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);
            }
        }
Пример #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);
                    }
                }
            }
        }