簡體   English   中英

WinRT中的文件加密

[英]File encryption in WinRT

我目前正在開發需要文件加密的Metro應用程序(C#/ XAML)。 在Winforms和WPF中,我只需要編寫

System.IO.File.Encrypt("file.txt");

如何在WinRT中做同樣的事情?

首先,我永遠不會使用System.IO.File.Encrypt來加密文件。
其次,我將看一下以下文檔: Windows Runtime API
第三,我將使用此處此處描述的類似方法對文件進行加密

public MainWindow()
{
   InitializeComponent();

   byte[] encryptedPassword;

   // Create a new instance of the RijndaelManaged
   // class.  This generates a new key and initialization 
   // vector (IV).
   using (var algorithm = new RijndaelManaged())
   {
      algorithm.KeySize = 256;
      algorithm.BlockSize = 128;

      // Encrypt the string to an array of bytes.
      encryptedPassword = Cryptology.EncryptStringToBytes("Password", 
                                                    algorithm.Key, algorithm.IV);
   }

   string chars = encryptedPassword.Aggregate(string.Empty, 
                                         (current, b) => current + b.ToString());

Cryptology.EncryptFile(@"C:\Users\Ira\Downloads\test.txt", @"C:\Users\Ira\Downloads\encrypted_test.txt", chars);

Cryptology.DecryptFile(@"C:\Users\Ira\Downloads\encrypted_test.txt", @"C:\Users\Ira\Downloads\unencyrpted_test.txt", chars);
}

據我了解,WinRT專為在沙盒中運行且沒有直接文件系統訪問權限的應用程序而設計。

您可能需要非WinRT(例如Win32 / .NET桌面API)服務才能直接訪問文件系統,並使WinRT應用程序與該服務進行通信。

不幸的是,這將需要在WinRT中做更多的工作。 由於大多數功能都是異步的,因此您將需要更多樣板,並且將IBufferIBuffer進行操作,而不是直接對文件進行操作。 加密類位於Windows.Security.Cryptography命名空間中。

這里可以找到帶有IBuffer的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM