示例#1
0
        public bool LoadNewKey(string key, string password = "", bool fromDb = false)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(false);
            }

            if (fromDb)
            {
                _key = key;
                return(true);
            }

            if (!string.IsNullOrEmpty(password))
            {
                loadedPassword = password;
                passwordLoaded = true;
                PasswordHash   = SecurityUtil.HashPassword(password);
                _key           = SymetricProvider.EncryptString(password, key);
                IsEncrypted    = true;
                return(true);
            }
            else
            {
                _key        = key;
                IsEncrypted = false;
                return(true);
            }
        }
示例#2
0
        public string GetEncryptedKey(string password = "", bool returnEncrypted = false)
        {
            if (returnEncrypted)
            {
                return(_key);
            }

            if (passwordLoaded && string.IsNullOrEmpty(password))
            {
                password = loadedPassword;
            }

            if (!passwordLoaded && string.IsNullOrEmpty(password))
            {
                return(null);
            }

            if (!string.IsNullOrEmpty(password))
            {
                if (PasswordHash?.Length > 0)
                {
                    if (SecurityUtil.VerifyPassword(password, PasswordHash))
                    {
                        return(SymetricProvider.DecryptString(password, _key));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                if (!IsEncrypted)
                {
                    return(_key);
                }
                else
                {
                    return(null);
                }
            }
        }