/// <summary>
        /// Delete a specify connection
        /// </summary>
        /// <param name="tmpConnectionToSet">connection to delete</param>
        internal void DeleteConnection(ConnectionModel tmpConnectionToSet)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            string encComputerMacAddress = engine.Encrypt(string.Concat(Salt, tmpConnectionToSet.MachineMacAddress, Pepper), Key256Bits);
            string encComputerName       = engine.Encrypt(string.Concat(Salt, tmpConnectionToSet.MachineName, Pepper), Key256Bits);
            string encComputerUserName   = engine.Encrypt(string.Concat(Salt, tmpConnectionToSet.MachineUserName, Pepper), Key256Bits);

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                // Delete any previous row which match
                List <ConnectionTemp> existingConnections = (from conn in context.ConnectionTemps
                                                             where conn.ComputerMacAddress.Equals(encComputerMacAddress) &&
                                                             conn.ComputerName.Equals(encComputerName) &&
                                                             conn.ComputerUserName.Equals(encComputerUserName)
                                                             select conn).ToList();

                if (existingConnections.Count > 0)
                {
                    context.ConnectionTemps.RemoveRange(existingConnections);
                    context.SaveChanges();
                }
            }
        }
Пример #2
0
        internal static string AESDecryption(string cipher, string key, string salt, Pkcs7Padding padding)
        {
            BCEngine bcEngine = new BCEngine(new AesEngine(), Encoding.UTF8);

            bcEngine.SetPadding(padding);
            return(bcEngine.Decrypt(cipher, key));
        }
        /// <summary>
        /// Get the whole list of passwords for a user
        /// </summary>
        /// <param name="userId">User id</param>
        /// <returns>The list of active passwords</returns>
        internal List <PasswordModel> GetList(int userId)
        {
            List <PasswordModel> passwords = new List <PasswordModel>();

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                List <Password> pwds = (from pass in context.Passwords
                                        where pass.IsActive && pass.UserId == userId
                                        select pass).ToList();

                BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);
                engine.SetPadding(new Pkcs7Padding());

                foreach (Password pwd in pwds)
                {
                    passwords.Add(new PasswordModel()
                    {
                        Id           = pwd.Id,
                        DisplayName  = pwd.DisplayName,
                        Login        = GetDecryptedString(engine, pwd.Login),
                        Password     = GetDecryptedString(engine, pwd.Password1),
                        Url          = GetDecryptedString(engine, pwd.Url),
                        Notes        = GetDecryptedString(engine, pwd.Notes),
                        CreationDate = pwd.CreationDate,
                        IsActive     = pwd.IsActive,
                        UserId       = pwd.UserId
                    });
                }
            }

            return(passwords);
        }
        /// <summary>
        /// Update a password
        /// </summary>
        /// <param name="pwdToUpdate">Password to update</param>
        internal void Update(PasswordModel pwdToUpdate)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                Password pass = GetPassword(context, pwdToUpdate.Id);

                pass.DisplayName = pwdToUpdate.DisplayName;
                pass.Login       = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Login, Pepper), Key256Bits);
                pass.Password1   = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Password, Pepper), Key256Bits);
                pass.Url         = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Url, Pepper), Key256Bits);
                pass.Notes       = engine.Encrypt(string.Concat(Salt, pwdToUpdate.Notes, Pepper), Key256Bits);

                context.SaveChanges();
            }
        }
        /// <summary>
        /// Get the last user id for the given connection details
        /// </summary>
        /// <param name="tmpConnection">Connection data</param>
        /// <returns>The user id</returns>
        internal int?GetConnectionTempUserId(ConnectionModel tmpConnection)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            string encComputerMacAddress = engine.Encrypt(string.Concat(Salt, tmpConnection.MachineMacAddress, Pepper), Key256Bits);
            string encComputerName       = engine.Encrypt(string.Concat(Salt, tmpConnection.MachineName, Pepper), Key256Bits);
            string encComputerUserName   = engine.Encrypt(string.Concat(Salt, tmpConnection.MachineUserName, Pepper), Key256Bits);

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                int usrIdLogin = (from conn in context.ConnectionTemps
                                  where conn.ComputerMacAddress.Equals(encComputerMacAddress) &&
                                  conn.ComputerName.Equals(encComputerName) &&
                                  conn.ComputerUserName.Equals(encComputerUserName)
                                  select conn.IdUser).FirstOrDefault();

                return(usrIdLogin == 0 ? new int?() : usrIdLogin);
            }
        }
        /// <summary>
        /// Create a new password
        /// </summary>
        /// <param name="pwdToCreate">Password to create</param>
        internal void Create(PasswordModel pwdToCreate)
        {
            BCEngine engine = new BCEngine(new AesEngine(), Encoding.UTF8);

            engine.SetPadding(new Pkcs7Padding());

            using (PasswordKeeperEntities context = new PasswordKeeperEntities())
            {
                Password newPwd = new Password()
                {
                    Login        = engine.Encrypt(string.Concat(Salt, pwdToCreate.Login, Pepper), Key256Bits),
                    Password1    = engine.Encrypt(string.Concat(Salt, pwdToCreate.Password, Pepper), Key256Bits),
                    DisplayName  = pwdToCreate.DisplayName,
                    Url          = engine.Encrypt(string.Concat(Salt, pwdToCreate.Url, Pepper), Key256Bits),
                    Notes        = engine.Encrypt(string.Concat(Salt, pwdToCreate.Notes, Pepper), Key256Bits),
                    CreationDate = pwdToCreate.CreationDate,
                    IsActive     = pwdToCreate.IsActive,
                    UserId       = pwdToCreate.UserId
                };

                context.Passwords.Add(newPwd);
                context.SaveChanges();
            }
        }
        /// <summary>
        /// Decrypt cipher string and remove salt and pepper
        /// </summary>
        /// <param name="engine">Cipher engine</param>
        /// <param name="cipher">Cipher text</param>
        /// <returns>Decrypted string</returns>
        private string GetDecryptedString(BCEngine engine, string cipher)
        {
            string saltedString = engine.Decrypt(cipher, Key256Bits);

            return(saltedString.Replace(Salt, string.Empty).Replace(Pepper, string.Empty));
        }