【问题标题】:Windows Phone 7: Decrypting many files from isolated storageWindows Phone 7:从隔离存储中解密许多文件
【发布时间】:2014-12-09 11:30:12
【问题描述】:

我正在尝试将许多 (5-15) .txt 文件存储在手机的独立存储内存中的程序。我注意到使用 Windows Phone Power Tools 等程序读取这些文件是多么容易,所以我决定对它们进行加密。我将此链接用作教程:

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 中添加了一行代码。

【问题讨论】:

  • 您到底遇到了什么问题?性能相关?
  • @FunksMaName 我的问题是:我应该如何编辑我的代码才能解密许多 .txt 文件?我已经编辑了我的帖子,所以现在其他人也更容易注意到这个问题。
  • 好的,我现在明白了。我已将此处的示例改编为答案,以便它适用于多个文件。 msdn.microsoft.com/en-us/library/windows/apps/… 我认为您在那条线上遇到了问题,因为您没有尝试读取有效文件。 “DataFolder\\” 不是指向文件,它是一个文件夹,因此您可能需要为其提供有效的文件名。

标签: c# windows-phone-7 encryption isolatedstorage


【解决方案1】:

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; }
}

【讨论】:

  • 谢谢 FunksMaName,这真的很有帮助。遗憾的是,由于声誉低下,无法对此答案投赞成票,但我将其标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多