예제 #1
0
        public async Task <bool> ChangeKey(string storage, string oldKey, string newKey, string confirmKey)
        {
            string url = HttpService.Url;

            try
            {
                var model = new ChangeKeyStorageViewModel()
                {
                    Name       = storage,
                    OldKey     = oldKey,
                    NewKey     = newKey,
                    ConfirmKey = confirmKey
                };
                string      json    = JsonConvert.SerializeObject(model);
                HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
                var         result  = await HttpService.Instance.PostAsync(url + "Storage/ChangeKeyStorage", content);

                if (result.IsSuccessStatusCode)
                {
                    return(true);
                }

                Exceptions.Add(new Exception(result.Content.ReadAsStringAsync().Result));
                return(false);
            }
            catch (Exception ex)
            {
                Exceptions.Add(ex);
                return(false);
            }
        }
예제 #2
0
        public async Task <IActionResult> ChangeKeyStorage([FromBody] ChangeKeyStorageViewModel model)
        {
            if (ModelState.IsValid)
            {
                var storage = storageContext.Storages
                              .Where(s => s.Name == model.Name && s.User == User.Identity.Name)
                              .FirstOrDefault();

                var word = storageContext.Words
                           .Where(w => w.Storage == model.Name && w.User == User.Identity.Name)
                           .FirstOrDefault();

                if (storage == null)
                {
                    return(new BadRequestObjectResult("Хранилище не найдено"));
                }

                if (word == null)
                {
                    return(new BadRequestObjectResult("Контрольное слово отсутствует"));
                }

                if (!model.NewKey.Equals(model.ConfirmKey))
                {
                    return(new BadRequestObjectResult("Пароли не совпадают"));
                }

                if (model.OldKey.Equals(model.NewKey))
                {
                    return(new BadRequestObjectResult("Старый и новый пароли совпадают"));
                }

                if (!encryptor.CheckSizeKey(model.NewKey) || !encryptor.CheckSizeKey(model.OldKey))
                {
                    return(new BadRequestObjectResult("Размер ключа [16, 24, 32]"));
                }

                var controlWord = encryptor.Decrypt(encryptor.ToByte(model.OldKey), storage.IV, word.ControlWord);

                if (controlWord == null)
                {
                    return(new BadRequestObjectResult("Ключ неверный"));
                }

                storage.IV = encryptor.GenerateIV();

                word.ControlWord = encryptor.Encrypt(encryptor.ToByte(model.NewKey), storage.IV, controlWord);

                storageContext.Storages.Update(storage);
                storageContext.Words.Update(word);

                storageContext.Storages.Update(storage);
                await storageContext.SaveChangesAsync();

                return(new OkObjectResult("Пароль хранилища обновлен"));
            }

            return(new BadRequestObjectResult("Модель данных не корректна"));
        }