コード例 #1
0
        public static string GetDiaryText(byte[] cipherText)
        {
            byte[] keyMaterial = Convert.FromBase64String(kmKey);

            return(CryptoUtilities.ByteArrayToString(
                       CryptoUtilities.Decrypt(cipherText, keyMaterial)));
        }
コード例 #2
0
        public virtual async Task ResetPasswordAsync(dynamic model)
        {
            _validationService.Bundles.ValidateNewPassword(model);

            string q = model.q;

            int    id;
            string ciphertext;

            try {
                var split = q.Split(new[] { ':' });

                id         = int.Parse(split[0]);
                ciphertext = split[1];
            }
            catch {
                throw new ServiceException(ServiceReason.InvalidUserRequest);
            }

            var request = await _repository.GetAsync <UserRequest>(id);

            if (request == null || request.RequestType != UserRequestType.ResetPassword)
            {
                throw new ServiceException(ServiceReason.InvalidUserRequest);
            }

            try {
                var plaintext = CryptoUtilities.Decrypt(ciphertext, request.Key, request.IV);

                dynamic obj = JObject.Parse(plaintext);

                var credentials = new User {
                    Username = obj.username,
                    Password = obj.password
                };

                if (!CryptoUtilities.ValidatePassword(credentials.Password, request.Password))
                {
                    throw new ServiceException(ServiceReason.ResetPasswordError);
                }

                var user = await _repository.UserByUsernameAsync(credentials.Username);

                user.Password = CryptoUtilities.CreateHash((string)model.password);

                await _repository.UpdateAsync(user);

                await _repository.UpdateAsync(request);
            }
            catch {
                throw new ServiceException(ServiceReason.ResetPasswordError);
            }
        }
コード例 #3
0
        // Decryption of goal description - coming from SQLite database
        string GetGoalText(byte[] cipherText)
        {
            Account account = accountManager.CheckForAccount();

            if (!account.Properties.TryGetValue("keymaterial", out string keyString))
            {
                return(String.Empty);
            }

            byte[] keyMaterial = Convert.FromBase64String(keyString);

            return(CryptoUtilities.ByteArrayToString(CryptoUtilities.Decrypt(cipherText, keyMaterial)));
        }
コード例 #4
0
        string GetDiaryText(byte[] cipherText)
        {
            string keyString;

            if (!account.Properties.TryGetValue(kmKey, out keyString))
            {
                return(string.Empty);
            }

            byte[] keyMaterial = Convert.FromBase64String(keyString);

            return(CryptoUtilities.ByteArrayToString(CryptoUtilities.Decrypt(cipherText, keyMaterial)));
        }
コード例 #5
0
        public void TestDefaultBehavior()
        {
            // arrange
            var obj = new {
                plaintext = "secret"
            };

            string plaintext;

            // act
            using (var algorithm = TripleDES.Create()) {
                var ciphertext = CryptoUtilities.Encrypt(obj.plaintext, algorithm.Key, algorithm.IV);

                plaintext = CryptoUtilities.Decrypt(ciphertext, algorithm.Key, algorithm.IV);
            }

            // assert
            Assert.Equal(obj.plaintext, plaintext);
        }
コード例 #6
0
        public void DecryptFromFile(string filePath, SecureString password)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException("File path is empty.");
            }

            try
            {
                // Reading vault from file

                //string xml = string.Empty;

                //using (FileStream sourceStream = new FileStream(filePath,
                //    FileMode.Open, FileAccess.Read, FileShare.Read,
                //    bufferSize: 4096, useAsync: true))
                //{
                //    StringBuilder sb = new StringBuilder();

                //    byte[] buffer = new byte[0x1000];
                //    int numRead;
                //    while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
                //    {
                //        string text = Encoding.UTF8.GetString(buffer, 0, numRead);
                //        sb.Append(text);
                //    }

                //    xml = sb.ToString();
                //}

                // Deserializing vault

                var xml = File.ReadAllText(filePath);

                XmlSerializer serializer = new XmlSerializer(typeof(Vault));
                using (TextReader reader = new StringReader(xml))
                {
                    var vault = (Vault)serializer.Deserialize(reader);
                    EncryptionInfo = vault.EncryptionInfo;
                    Data           = vault.Data;
                    Name           = vault.Name;
                    Description    = vault.Description;
                }

                using (var cu = new CryptoUtilities(EncryptionInfo.SelectedAlgorithm))
                {
                    if (!CryptoUtilities.ValidatePassword(password, EncryptionInfo.ValidationKey, EncryptionInfo.Salt))
                    {
                        throw new ArgumentException("Incorrect password.");
                    }

                    // Unprotect the key and decrypt data

                    var plainData = cu.Decrypt(Data, cu.UnprotectEncryptionKey(password,
                                                                               EncryptionInfo.EncryptionKey, EncryptionInfo.Salt, EncryptionInfo.IV),
                                               EncryptionInfo.IV);

                    // Deserialize folder list

                    serializer = new XmlSerializer(typeof(MvxObservableCollection <Folder>));
                    using (TextReader reader = new StringReader(Encoding.UTF8.GetString(plainData)))
                    {
                        FolderList = (MvxObservableCollection <Folder>)serializer.Deserialize(reader);
                    }

                    // Set vault's Location
                    Location = filePath;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }