加密和解密隔离存储文件

时间:2011-12-28 12:28:01

标签: windows windows-phone-7 encryption isolatedstorage

我想加密和解密隔离的存储文件。

Microsoft网站带我here

  1. 在仿真器上使用Isolated Storage时,它只能在仿真器运行之前保持不变。

  2. 无法获取隔离存储的物理位置。

  3. 我希望我的上述陈述是正确的。

    现在,我想知道如何加密隔离存储文件?

    以Microsoft提供的示例(应用程序名称为GasMileage)

    这是代码

    namespace CodeBadger.GasMileage.Persistence
    {
        public class IsolatedStorageGateway
        {
            private const string StorageFile = "data.txt";
            private readonly XmlSerializer _serializer;
    
        public IsolatedStorageGateway()
        {
            _serializer = new XmlSerializer(typeof (Notebook));
        }
    
        public Notebook LoadNotebook()
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = GetStorageStreamForReading(store))
                using (var reader = new StreamReader(stream))
                {
                    return reader.EndOfStream 
                        ? new Notebook() 
                        : (Notebook) _serializer.Deserialize(reader);
                }
            }
        }
    
        public NotebookEntry LoadEntry(Guid guid)
        {
            var notebook = LoadNotebook();
            return notebook.Where(x => x.Id == guid).FirstOrDefault();
        }
    
        public void StoreEntry(NotebookEntry entry)
        {
            var notebook = LoadNotebook();
            AssignId(entry);
            RemoveExistingEntryFromNotebook(notebook, entry);
            Console.WriteLine(entry);
            notebook.Add(entry);
            WriteNotebookToStorage(notebook);
        }
    
        public void DeleteEntry(NotebookEntry entry)
        {
            var notebook = LoadNotebook();
            RemoveExistingEntryFromNotebook(notebook, entry);
            WriteNotebookToStorage(notebook);
        }
    
        private void WriteNotebookToStorage(Notebook notebook)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            using (var stream = GetStorageStreamForWriting(store))
            {
                _serializer.Serialize(stream, notebook);
            }
        }
    
        private static void AssignId(NotebookEntry entry)
        {
            if (entry.Id == Guid.Empty) entry.Id = Guid.NewGuid();
        }
    
        private static void RemoveExistingEntryFromNotebook(Notebook notebook, NotebookEntry entry)
        {
            var toRemove = notebook.Where(x => x.Id == entry.Id).FirstOrDefault();
            if (toRemove == null) return;
            notebook.Remove(toRemove);
        }
    
        private static IsolatedStorageFileStream GetStorageStreamForWriting(IsolatedStorageFile store)
        {
            return new IsolatedStorageFileStream(StorageFile, FileMode.Create, FileAccess.Write, store);
        }
    
        private static IsolatedStorageFileStream GetStorageStreamForReading(IsolatedStorageFile store)
        {
            return new IsolatedStorageFileStream(StorageFile, FileMode.OpenOrCreate, FileAccess.Read, store);
        }
    }
    

    现在我想知道,如何加密上下文中给出的 data.txt

    在应用程序加载时,解密文件并在应用程序终止时,它应该加密。

    有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

ProtectedData类将加密/解密字节数组以存储在隔离存储上。您可以提供自己的附加熵,但默认情况下:

  

在Silverlight for Windows Phone中,用户和计算机凭据都用于加密或解密数据

有关详细信息,请参阅How to: Encrypt Data in a Windows Phone Application