Floating FB popout byF5debug

101 How to on Windows Phone – How to #49 – Decrypt data in a Windows Phone Application

 

In this article we are going to see how we can decrypt a data that is been encrypted and protected using the Protect data method available with the ProtectedData class. In our previous article we have seen how to encrypt a data that will be saved in the Isolated storage, we will use the data that is encrypted to decrypt in this article. To decrypt the data we need to use the Unprotect method by passing the Folder (Isolated Folder) variable that we assigned at the beginning. Open Visual Studio 2012 IDE and open the project which we created in the previous article and add a new button to decrypt the data as shown in the screen below.

image

XAML Code:

[code:c#]

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="F5debug How to Series" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="Encrypt Data" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Encrypt Data" Height="72" HorizontalAlignment="Left" Margin="10,118,0,0" Name="button1" VerticalAlignment="Top" Width="441" Click="button1_Click" />
            <TextBlock Height="238" HorizontalAlignment="Left" TextWrapping="Wrap" Margin="29,254,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" Width="407" />
            <TextBox x:Name="txtenter" HorizontalAlignment="Left" Height="72" Margin="10,41,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="436"/>
            <Button Content="Decrypt Data" Height="72" HorizontalAlignment="Left" Margin="10,177,0,0" x:Name="button1_Copy" VerticalAlignment="Top" Width="441" Click="button1_Click" />
        </Grid>

[/code]

Now in the decrypt button click event write the below code which will basically call another private method which has the code base to decrypt the data with the help of the Unprotect method that is available with the Windows Phone SDK as shown in the code below.

Code:

[code:c#]

private void button1_Copy_Click(object sender, RoutedEventArgs e)
        {
            byte[] btPin = this.GetData();
            byte[] PinByte = ProtectedData.Unprotect(btPin, null);
            textBlock1.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);

        }

        private byte[] GetData()
        {
            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream readstream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open, FileAccess.Read, file);

            Stream reader = new StreamReader(readstream).BaseStream;
            byte[] pinArray = new byte[reader.Length];

            reader.Read(pinArray, 0, pinArray.Length);
            reader.Close();
            readstream.Close();
            return pinArray;
        }

[/code]

Now we are done with our code, just run the application by pressing F5 directly from the keyboard or we can use the Build and execute the project option from the tool bar to run the application. Once the Build is successful we can see the Windows Phone emulator with the application and the expected outputs as shown in the screens below.

image

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter