Encrypt And Decrypt Files In Windows Runtime Apps Using Windows Cryptography Technique

Here are the steps: 

Step 1:

Create a simple Windows Runtime Project. Click New Project, Visual C#, Windows 8, Windows, then Blank App (Windows 8.1).

 
Step 2:

Let's add two buttons in MainPage.xaml with click events:

  • Encrypt Button
  • Decrypt Button
  1. <Page  
  2.     x:Class="Encrypt_Decrypt.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:local="using:Encrypt_Decrypt"  
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  8.     mc:Ignorable="d">  
  9.   
  10.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  11.         <Button Content="Encrypt" Name="btn_Encrypt" Height="125" Margin="65,365,0,278" Width="225" Click="btn_Encrypt_Click"></Button>  
  12.         <Button Content="Decrypt" Name="btn_Decrypt" Margin="350,365,0,278" Height="125" Width="231" Click="btn_Decrypt_Click"></Button>  
  13.     </Grid>  
  14. </Page> 
Step 3:

Let's add a text file in Solution Explorer for testing purpose. We will copy that file from Installed Location to Local folder and encrypt it.

 

Step 4: Name it ‘OriginalFile.txt’


Step 5:Add some text in this text file.

 

Step 6:

In the code behind: MainPage.xaml.cs, Update your code with this:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.IO;  
  5. using System.Linq;  
  6. using System.Runtime.InteropServices.WindowsRuntime;  
  7. using Windows.ApplicationModel;  
  8. using Windows.Foundation;  
  9. using Windows.Foundation.Collections;  
  10. using Windows.Storage;  
  11. using Windows.Storage.Streams;  
  12. using Windows.UI.Xaml;  
  13. using Windows.UI.Xaml.Controls;  
  14. using Windows.UI.Xaml.Controls.Primitives;  
  15. using Windows.UI.Xaml.Data;  
  16. using Windows.UI.Xaml.Input;  
  17. using Windows.UI.Xaml.Media;  
  18. using Windows.UI.Xaml.Navigation;  
  19.   
  20.   
  21. namespace Encrypt_Decrypt  
  22. {  
  23.    
  24.     public sealed partial class MainPage : Page  
  25.     {  
  26.         public MainPage()  
  27.         {  
  28.             this.InitializeComponent();  
  29.         }  
  30.   
  31.         private async void btn_Encrypt_Click(object sender, RoutedEventArgs e)  
  32.         {  
  33.             //For Copying File from Installed Location to Local Folder   
  34.             StorageFolder appFolder = ApplicationData.Current.LocalFolder;  
  35.             StorageFile stopfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///OriginalFile.txt"));  
  36.             await stopfile.CopyAsync(ApplicationData.Current.LocalFolder, "OriginalFile.txt", NameCollisionOption.ReplaceExisting);  
  37.   
  38.             string sourceFileName = "OriginalFile.txt";  
  39.             string destinationFileName = "EncryptedFile.txt";  
  40.   
  41.             try  
  42.             {  
  43.                 StorageFile file = await appFolder.GetFileAsync(sourceFileName);  
  44.                 IBuffer fileStreamBuffer = await FileIO.ReadBufferAsync(file);  
  45.                 string protectionDescriptor = "LOCAL=user";  
  46.                 if (fileStreamBuffer.Length != 0)  
  47.                 {  
  48.                     Windows.Security.Cryptography.DataProtection.DataProtectionProvider provider = String.IsNullOrEmpty(protectionDescriptor) ? new Windows.Security.Cryptography.DataProtection.DataProtectionProvider() : new Windows.Security.Cryptography.DataProtection.DataProtectionProvider(protectionDescriptor);  
  49.                     IBuffer encryptedBuffer = await provider.ProtectAsync(fileStreamBuffer);  
  50.   
  51.                     StorageFile encryptedFile = await appFolder.CreateFileAsync(destinationFileName, CreationCollisionOption.ReplaceExisting);  
  52.                     await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);  
  53.                 }  
  54.             }  
  55.             catch (Exception ex)  
  56.             {  
  57.                 Debug.WriteLine(ex.ToString());  
  58.             }  
  59.         }  
  60.   
  61.   
  62.         private async void btn_Decrypt_Click(object sender, RoutedEventArgs e)  
  63.         {  
  64.             StorageFolder appFolder = ApplicationData.Current.LocalFolder;  
  65.             string sourceFileName = "EncryptedFile.txt";  
  66.             string destinationFileName = "DecryptedFile.txt";  
  67.             try  
  68.             {  
  69.                 StorageFile file = await appFolder.GetFileAsync(sourceFileName);  
  70.                 IBuffer fileStreamBuffer = await FileIO.ReadBufferAsync(file);  
  71.                 string protectionDescriptor = "LOCAL=user";  
  72.                 if (fileStreamBuffer.Length != 0)  
  73.                 {  
  74.                     Windows.Security.Cryptography.DataProtection.DataProtectionProvider provider = String.IsNullOrEmpty(protectionDescriptor) ? new Windows.Security.Cryptography.DataProtection.DataProtectionProvider() : new Windows.Security.Cryptography.DataProtection.DataProtectionProvider(protectionDescriptor);  
  75.                     IBuffer encryptedBuffer = await provider.UnprotectAsync(fileStreamBuffer);  
  76.   
  77.                     StorageFile encryptedFile = await appFolder.CreateFileAsync(destinationFileName, CreationCollisionOption.ReplaceExisting);  
  78.                     await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);  
  79.                 }  
  80.             }  
  81.             catch (Exception ex)  
  82.             {  
  83.                 Debug.WriteLine(ex.ToString());  
  84.             }  
  85.         }  
  86.     }  

Step 7:

Run the application. Click on Encrypt and Decrypt button. You can see the files being encrypted and decrypted in the Application Local Folder:

This PC, C, Users > [Your User Name] > AppData, Local, Packages > [App package name] > LocalState,


That’s it.


Similar Articles