Andrei Schneider
Andrei Schneider

Reputation: 3628

File encryption in WinRT

I'm current working on a metro application (C#/XAML) that requires file encryption. In Winforms and WPF for that I just need to write

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

How to do the same in WinRT?

Upvotes: 2

Views: 2743

Answers (3)

Security Hound
Security Hound

Reputation: 2551

First I would never use System.IO.File.Encrypt to encrypt a file.
Second I would take a look at the following documentation: Windows Runtime API
Third I would encrypt the file using a similar mehod describe here and here

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

Upvotes: 4

Ben Voigt
Ben Voigt

Reputation: 283614

As I understand it, WinRT is designed for applications which run in a sandbox and have no direct filesystem access.

You'll probably need a non-WinRT (e.g. Win32 / .NET desktop API) service for direct filesystem access, and have the WinRT application communicate with the service.

Upvotes: 1

linuxuser27
linuxuser27

Reputation: 7353

Unfortunately this is going to require a little more work in WinRT. Since most functions are asynchronous you will need some more boiler plate and you will be operating on streams and IBuffers rather than files directly. The crypto classes are in the Windows.Security.Cryptography namespace.

An example with an IBuffer can be found here.

Upvotes: 0

Related Questions