2011-12-28 84 views
0

我想加密和解密獨立存儲文件。加密和解密獨立存儲文件

微軟網站把我here

  1. 在使用仿真器的獨立存儲,它可以堅持只有等到模擬器運行。

  2. 沒有辦法獲得隔離存儲的物理位置。

我希望我的上述說法是正確的。

現在,我想知道如何加密隔離存儲文件?

以微軟提供的例子,(應用程序名稱是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中在給定的上下文。

在應用程序加載時,解密文件和應用程序終止時,應加密。

有人可以幫助我嗎?

回答

2

ProtectedData類將加密/解密字節數組以存儲在獨立存儲上。您可以提供自己的額外熵,但默認情況下:

在Silverlight for Windows Phone支持,無論是用戶和機器證書被用於加密或解密數據

欲瞭解更多信息,請參閱How to: Encrypt Data in a Windows Phone Application

+0

謝謝@理查德。我知道可以加密和解密機密數據(如密碼,連接字符串和PIN)的DPAPI。我想知道,如果有什麼要加密解密整個孤立的存儲文件本身。例如一個文本文件,數據變成不可讀的形式。 – Anil 2011-12-29 03:35:47

+1

使用DPAPI加密數據後,您將數據寫入文本文件。加載文本文件時,您可以將其加載到字節數組中,然後在讀取文本之前對其進行解密。 – 2011-12-29 10:27:34