Here are the steps,

Step 1: Create a simple Windows Project. New Project, Visual C#, Windows 8, Windows, then click Blank App (Windows 8.1).
blank

Step 2:

Lets add the following elements in MainPage.xaml :

Complete MainPage.xaml code snippet is:

  1. <Page
  2. x:Class="EncryptDecryptString.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:EncryptDecryptString"
  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="#FF449FF5">
  10. <TextBox x:Name="txtPlainString" HorizontalAlignment="Left" TextWrapping="Wrap" PlaceholderText="Enter plain string to encrypt" VerticalAlignment="Top" Margin="92,60,0,0" Width="540" Height="48" FontSize="25"/>
  11. <TextBox x:Name="txtKey" HorizontalAlignment="Left" TextWrapping="Wrap" PlaceholderText="Enter Encryption/Decryption Key" VerticalAlignment="Top" Margin="648,60,0,0" Width="434" FontSize="25" Height="48"/>
  12. <Button x:Name="btnEncrypt" Content="Encrypt plain string" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="89,123,0,0" Height="60" Width="546" Click="btnEncrypt_Click"/>
  13. <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Encrypted String : " VerticalAlignment="Top" FontSize="25" Margin="92,229,0,0" Foreground="Black"/>
  14. <TextBlock x:Name="txtEncryptedString" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="92,289,0,0" Height="40" FontSize="30" Width="899"/>
  15. <Button x:Name="btnDecrypt" Content="Decrypt encrypted string" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="89,398,0,0" Height="60" Width="546" Click="btnDecrypt_Click"/>
  16. <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Decrypted String : " VerticalAlignment="Top" Margin="92,486,0,0" FontSize="25" Height="34" Width="238" Foreground="Black"/>
  17. <TextBlock x:Name="txtdecryptedString" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Margin="92,547,0,0" FontSize="30" Height="40" Width="899"/>
  18. </Grid>
  19. </Page>
output

Step 3:

In the code behind: MainPage.xaml.cs.

Update your code with this:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices.WindowsRuntime;
  6. using System.Threading.Tasks;
  7. using Windows.Foundation;
  8. using Windows.Foundation.Collections;
  9. using Windows.Security.Cryptography;
  10. using Windows.Security.Cryptography.Core;
  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. // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
  20. namespace EncryptDecryptString
  21. {
  22. /// <summary>
  23. /// An empty page that can be used on its own or navigated to within a Frame.
  24. /// </summary>
  25. public sealed partial class MainPage : Page
  26. {
  27. public MainPage()
  28. {
  29. this.InitializeComponent();
  30. }
  31. private async void btnEncrypt_Click(object sender, RoutedEventArgs e)
  32. {
  33. string plainString = txtPlainString.Text;
  34. string key = txtKey.Text;
  35. string encryptedString = await EncryptStringHelper(plainString, key);
  36. txtEncryptedString.Text = encryptedString;
  37. }
  38. private async void btnDecrypt_Click(object sender, RoutedEventArgs e)
  39. {
  40. string encryptedString = txtEncryptedString.Text;
  41. string key = txtKey.Text;
  42. string decryptedString = await DecryptStringHelper(encryptedString, key);
  43. txtdecryptedString.Text = decryptedString;
  44. }
  45. private async Task<string> EncryptStringHelper(string plainString, string key)
  46. {
  47. try
  48. {
  49. var hashKey = GetMD5Hash(key);
  50. var decryptBuffer = CryptographicBuffer.ConvertStringToBinary(plainString, BinaryStringEncoding.Utf8);
  51. var AES = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
  52. var symmetricKey = AES.CreateSymmetricKey(hashKey);
  53. var encryptedBuffer = CryptographicEngine.Encrypt(symmetricKey, decryptBuffer, null);
  54. var encryptedString = CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
  55. return encryptedString;
  56. }
  57. catch (Exception ex)
  58. {
  59. return "";
  60. }
  61. }
  62. private async Task<string> DecryptStringHelper(string encryptedString, string key)
  63. {
  64. try
  65. {
  66. var hashKey = GetMD5Hash(key);
  67. IBuffer decryptBuffer = CryptographicBuffer.DecodeFromBase64String(encryptedString);
  68. var AES = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
  69. var symmetricKey = AES.CreateSymmetricKey(hashKey);
  70. var decryptedBuffer = CryptographicEngine.Decrypt(symmetricKey, decryptBuffer, null);
  71. string decryptedString = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, decryptedBuffer);
  72. return decryptedString;
  73. }
  74. catch (Exception ex)
  75. {
  76. return "";
  77. }
  78. }
  79. private static IBuffer GetMD5Hash(string key)
  80. {
  81. IBuffer bufferUTF8Msg = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
  82. HashAlgorithmProvider hashAlgorithmProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
  83. IBuffer hashBuffer = hashAlgorithmProvider.HashData(bufferUTF8Msg);
  84. if (hashBuffer.Length != hashAlgorithmProvider.HashLength)
  85. {
  86. throw new Exception("There was an error creating the hash");
  87. }
  88. return hashBuffer;
  89. }
  90. }
  91. }
Step 4:

Now you are all set. Run the application and enter plain string and key. Click on the Encrypt string button. You will see the encrypted string.

output

That’s it.

Thanks, Happy Coding!
Read more articles on Windows Runtime apps: