Exemplo n.º 1
0
        /// <summary> try load the database, could fail if password is incorrect </summary>
        private bool TryLoad(string password, out CredentialDatabaseEntity entity)
        {
            if (!File.Exists(this.fileName))
            {
                entity = this.CreateEmptyEntity(password);
                return(true);
            }

            entity = null;

            try
            {
                var data = File.ReadAllBytes(this.fileName);
                AESCryptographer crypt = new AESCryptographer();

                //TEMP
                var salt          = UTF8Encoding.UTF8.GetBytes(password.PadRight(16, 'x'));
                var decryptedData = crypt.Decrypt(password, data, salt);
                return(this.TryDeserialize <CredentialDatabaseEntity>(decryptedData, out entity));
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary/>
        public void Save(string password, List <CredentialEntity> credentials)
        {
            CredentialDatabaseEntity database = new CredentialDatabaseEntity();

            database.Password    = password;
            database.Version     = Version;
            database.Credentials = credentials;

            XmlSerializer serializer = new XmlSerializer(typeof(CredentialDatabaseEntity));

            using (var memoryStream = new MemoryStream())
            {
                serializer.Serialize(memoryStream, database);
                AESCryptographer crypt = new AESCryptographer();

                //TEMP
                var salt          = UTF8Encoding.UTF8.GetBytes(password.PadRight(16, 'x'));
                var encryptedData = crypt.Encrypt(password, memoryStream.ToArray(), salt);

                File.WriteAllBytes(this.fileName, encryptedData);
            }
        }