Ejemplo n.º 1
0
        public async Task <string> RetrieveFileContentsAsync(string fileName, string password, FileLocation location)
        {
            if (!_initialized)
            {
                throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
            }

            using (await f_lock.Acquire())
            {
                var    collectionToSearch = location == FileLocation.Local ? LocalFiles : RoamedFiles;
                var    file = collectionToSearch.Single(x => x.Name == fileName);
                string encryptedContents = await FileIO.ReadTextAsync(file.BackingFile);

                if (String.IsNullOrWhiteSpace(encryptedContents))
                {
                    return(null);
                }

                string iv = await _ivService.GetValue(GetParentFolder(file.BackingFile), location);

                try
                {
                    return(LegacyEncryptionManager.Decrypt(encryptedContents, password, iv));
                }
                catch (Exception ex)
                {
                    //Just explicitly noting that .Decrypt() can and WILL throw.
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves and encrypts the contents into a StorageFile with the given name, and the given password.
        /// </summary>
        /// <param name="contents">The contents to encrypt.</param>
        /// <param name="fileName">The filename with which to save the file.</param>
        /// <param name="password">The password that will be used to generate the encryption key.</param>
        /// <returns>If successful, returns the created StorageFile.</returns>
        public async Task <BindableStorageFile> SaveAndEncryptFileAsync(string contents, string fileName, string password)
        {
            using (await f_lock.Acquire())
            {
                if (!_initialized)
                {
                    throw new ServiceNotInitializedException($"{nameof(FileService)} was not initialized before access was attempted.");
                }
                if (contents == null)
                {
                    throw new ArgumentException("File contents cannot be null.");
                }

                string iv = Guid.NewGuid().ToString("N"); // OLD IV code.

                string encryptedContents = LegacyEncryptionManager.Encrypt(contents, password, iv);
                var    savedFile         = await _localFolder.CreateFileAsync(fileName, CreationCollisionOption.GenerateUniqueName);

                await FileIO.WriteTextAsync(savedFile, encryptedContents);

                string savedFileName = savedFile.Name;

                BindableStorageFile bsf = await BindableStorageFile.Create(savedFile);

                LocalFiles.Add(bsf);

                await _ivService.Add(GetParentFolder(bsf.BackingFile), iv, FileLocation.Local);

                return(bsf);
            }
        }