2014-12-09 81 views
0

我正在嘗試做一個程序,它在手機的獨立存儲內存中存儲很多(5-15).txt文件。我注意到使用Windows Phone Power Tools等程序來讀取這些文件是多麼容易,因此我決定對它們進行加密。我使用這個鏈接作爲教程:Windows Phone 7:解密來自孤立存儲的許多文件

http://msdn.microsoft.com/en-us/library/windows/apps/hh487164(v=vs.105).aspx

加密正常工作,因爲我明顯節約一次一個文件。但是,我在嘗試解密時遇到問題。 我應該如何編輯我的代碼,以便解密很多.txt文件?下面是我的代碼,我使用的時刻:

 private void IsoRead() 
    { 
     System.IO.IsolatedStorage.IsolatedStorageFile local = 
     System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 

     string[] filenames = local.GetFileNames("./DataFolder/*.txt*"); 
     foreach (var fname in filenames) 
     { 
      //retrieve byte 
       byte[] ProtectedByte = this.DecryptByte(); 
      //decrypt with Unprotect method 
       byte[] FromByte = ProtectedData.Unprotect(ProtectedByte, null); 
      //convert from byte to string 
       fText = Encoding.UTF8.GetString(FromByte, 0, FromByte.Length); 

       this.Items.Add(new itemView() { LineOne = fname, LineTwo = fText }); 
     }   
    } 

而另外一個:

 private byte[] DecryptByte() 
    { 
     // Access the file in the application's isolated storage. 
     IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); 

     IsolatedStorageFileStream readstream = new IsolatedStorageFileStream 
      ("DataFolder\\"/*Here's where I'm having problems with*/, System.IO.FileMode.Open, FileAccess.Read, file); 

     // Read the written data from the file. 
     Stream reader = new StreamReader(readstream).BaseStream; 
     byte[] dataArray = new byte[reader.Length]; 
     reader.Read(dataArray, 0, dataArray.Length); 
     return dataArray; 

    } 

所以基本上程序有一個列表視圖頁面,得到的來自獨立存儲的文件。如果有人感動,它會進入一個新的頁面,顯示寫入的內容。

獎金問題:我可以加密WP7/WP8中的文件夾嗎?

編輯:在IsoRead中添加一行代碼。

+0

你究竟有什麼樣的問題呢?性能相關? – FunksMaName 2014-12-09 12:02:15

+0

@FunksMaName我的問題是:我應該如何編輯我的代碼,以便我可以解密很多.txt文件? 我編輯了我的文章,因此現在更容易注意到其他人的問題。 – user3616427 2014-12-09 12:19:52

+0

好的,我現在得到問題了。我已經將這個例子改編爲答案,以便它適用於多個文件。 http://msdn.microsoft.com/en-us/library/windows/apps/hh487164%28v=vs.105%29.aspx我認爲你在該行有問題,因爲你不是試圖讀取一個有效的文件。 「DataFolder \\」不是指向一個文件,而是一個文件夾,因此您可能需要提供一個有效的文件名。 – FunksMaName 2014-12-09 16:54:09

回答

0

的XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <StackPanel> 
      <Button Content="Write Random File" Click="WriteFile" VerticalAlignment="Top" /> 
      <Button Content="Read files File" Click="ReadFiles" VerticalAlignment="Top" /> 
     </StackPanel> 
    </Grid> 

代碼:

public partial class MainPage : PhoneApplicationPage 
{ 
    // Constructor 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private const string FilePath = "{0}.txt"; 

    private readonly List<ItemView> items = new List<ItemView>(); 

    private void WriteFile(object sender, RoutedEventArgs e) 
    { 
     var fileName = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture); 

     // Convert text to bytes. 
     byte[] data = Encoding.UTF8.GetBytes(fileName); 

     // Encrypt byutes. 
     byte[] protectedBytes = ProtectedData.Protect(data, null); 

     // Store the encrypted bytes in iso storage. 
     this.WriteToFile(protectedBytes, fileName); 
    } 

    private void ReadFiles(object sender, RoutedEventArgs e) 
    { 
     items.Clear(); 

     using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      var files = isoStore.GetFileNames("*.txt"); 

      foreach (var file in files) 
      { 
       // Retrieve the protected bytes from isolated storage. 
       byte[] protectedBytes = this.ReadBytesFromFile(file); 

       // Decrypt the protected bytes by using the Unprotect method. 
       byte[] bytes = ProtectedData.Unprotect(protectedBytes, null); 

       // Convert the data from byte to string and display it in the text box. 
       items.Add(new ItemView { LineOne = file, LineTwo = Encoding.UTF8.GetString(bytes, 0, bytes.Length) }); 
      } 
     } 

     //Show all the data... 
     MessageBox.Show(string.Join(",", items.Select(i => i.LineTwo))); 
    } 

    private void WriteToFile(byte[] bytes, string fileName) 
    { 
     // Create a file in the application's isolated storage. 
     using (var file = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (var writestream = new IsolatedStorageFileStream(string.Format(FilePath, fileName), System.IO.FileMode.Create, System.IO.FileAccess.Write, file)) 
      { 
       // Write data to the file. 
       using (var writer = new StreamWriter(writestream).BaseStream) 
       { 
        writer.Write(bytes, 0, bytes.Length); 
       } 
      } 
     } 
    } 

    private byte[] ReadBytesFromFile(string filePath) 
    { 
     // Access the file in the application's isolated storage. 
     using (var file = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      using (var readstream = new IsolatedStorageFileStream(filePath, System.IO.FileMode.Open, FileAccess.Read, file)) 
      { 
       // Read the data in the file. 
       using (var reader = new StreamReader(readstream).BaseStream) 
       { 
        var ProtectedPinByte = new byte[reader.Length]; 

        reader.Read(ProtectedPinByte, 0, ProtectedPinByte.Length); 

        return ProtectedPinByte; 
       } 
      } 
     } 
    } 
} 

public class ItemView 
{ 
    public string LineOne { get; set; } 
    public string LineTwo { get; set; } 
} 
+0

謝謝FunksMaName,真的有幫助。可悲的是,由於名聲不好,無法贊成這個答案,但我將其標記爲已接受。 – user3616427 2014-12-10 07:36:52