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. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
  10. <Button Content="Encrypt" Name="btn_Encrypt" Height="125" Margin="65,365,0,278" Width="225" Click="btn_Encrypt_Click"></Button>
  11. <Button Content="Decrypt" Name="btn_Decrypt" Margin="350,365,0,278" Height="125" Width="231" Click="btn_Decrypt_Click"></Button>
  12. </Grid>
  13. </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. namespace Encrypt_Decrypt
  20. {

  21. public sealed partial class MainPage : Page
  22. {
  23. public MainPage()
  24. {
  25. this.InitializeComponent();
  26. }
  27. private async void btn_Encrypt_Click(object sender, RoutedEventArgs e)
  28. {
  29. //For Copying File from Installed Location to Local Folder
  30. StorageFolder appFolder = ApplicationData.Current.LocalFolder;
  31. StorageFile stopfile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///OriginalFile.txt"));
  32. await stopfile.CopyAsync(ApplicationData.Current.LocalFolder, "OriginalFile.txt", NameCollisionOption.ReplaceExisting);
  33. string sourceFileName = "OriginalFile.txt";
  34. string destinationFileName = "EncryptedFile.txt";
  35. try
  36. {
  37. StorageFile file = await appFolder.GetFileAsync(sourceFileName);
  38. IBuffer fileStreamBuffer = await FileIO.ReadBufferAsync(file);
  39. string protectionDescriptor = "LOCAL=user";
  40. if (fileStreamBuffer.Length != 0)
  41. {
  42. Windows.Security.Cryptography.DataProtection.DataProtectionProvider provider = String.IsNullOrEmpty(protectionDescriptor) ? new Windows.Security.Cryptography.DataProtection.DataProtectionProvider() : new Windows.Security.Cryptography.DataProtection.DataProtectionProvider(protectionDescriptor);
  43. IBuffer encryptedBuffer = await provider.ProtectAsync(fileStreamBuffer);
  44. StorageFile encryptedFile = await appFolder.CreateFileAsync(destinationFileName, CreationCollisionOption.ReplaceExisting);
  45. await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. Debug.WriteLine(ex.ToString());
  51. }
  52. }
  53. private async void btn_Decrypt_Click(object sender, RoutedEventArgs e)
  54. {
  55. StorageFolder appFolder = ApplicationData.Current.LocalFolder;
  56. string sourceFileName = "EncryptedFile.txt";
  57. string destinationFileName = "DecryptedFile.txt";
  58. try
  59. {
  60. StorageFile file = await appFolder.GetFileAsync(sourceFileName);
  61. IBuffer fileStreamBuffer = await FileIO.ReadBufferAsync(file);
  62. string protectionDescriptor = "LOCAL=user";
  63. if (fileStreamBuffer.Length != 0)
  64. {
  65. Windows.Security.Cryptography.DataProtection.DataProtectionProvider provider = String.IsNullOrEmpty(protectionDescriptor) ? new Windows.Security.Cryptography.DataProtection.DataProtectionProvider() : new Windows.Security.Cryptography.DataProtection.DataProtectionProvider(protectionDescriptor);
  66. IBuffer encryptedBuffer = await provider.UnprotectAsync(fileStreamBuffer);
  67. StorageFile encryptedFile = await appFolder.CreateFileAsync(destinationFileName, CreationCollisionOption.ReplaceExisting);
  68. await FileIO.WriteBufferAsync(encryptedFile, encryptedBuffer);
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. Debug.WriteLine(ex.ToString());
  74. }
  75. }
  76. }
  77. }

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.

Thanks, Happy Coding!

Read more articles on Windows Runtime Apps: