In this article we are going to see how we can encrypt a data or a pin that are sensitive to be shown to the user or the developer in a Windows Phone application development. In our earlier article we have seen how to use the theme resources that will be use to customize the application background and the elements. Here we can see encrypting a data is pretty much necessary for a development as saving the data which is sensitive directly to the Isolated storage is not secure. In Windows Phone we have ProtectedData class which has methods to encrypt and decrypt the data with the Protect and Unprotect methods. We need to use the Protect method to encrypt and Unprotect method to decrypt the necessary data. Let us see the step by step process on how to encrypt the data with the Protect method in a Windows Phone application development.
Open Visual Studio 2012 IDE and create a new Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to encrypt the data as shown in the screen below.
Now to start with let us add a Button control, a textbox control to enter the data to encrypt and a textblock control to display the message to the end user about the process as shown in the code below.
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″ />
<TextBlock Height=”297″ HorizontalAlignment=”Left” TextWrapping=”Wrap” Margin=”29,195,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″/>
</Grid>
Now we need to write our code which encrypts the data, to start with we need to import some namespaces which are used to manipulate the data to be encrypted. Add the below namespaces to the top of the code base exactly in the using statements as shown in the screen below.
Code:
[code:c#]using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using System.Security.Cryptography;
Next step is to specify a folder basically that is the name of the folder which will be like a database path name called Isolated storage for Windows Phone. So specify the name in a variable that will be used while the data is encrypted. Once we specify the variable we need to convert the data to a array of bytes to be stored in the Isolated storage, so follow the below code which calls another method that does the encryption as shown in the code below.
Code:
[code:c#]private string FilePath = “pinfile”;
private void button1_Click(object sender, RoutedEventArgs e)
{
byte[] btdata = Encoding.UTF8.GetBytes(txtenter.Text);
byte[] encryptedPinByte = ProtectedData.Protect(btdata, null);
this.Writedata(encryptedPinByte);
textBlock1.Text = “Data Encrypted!!!”;
}
private void Writedata(byte[] btsData)
{
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream writestream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, file);
Stream writer = new StreamWriter(writestream).BaseStream;
writer.Write(btsData, 0, btsData.Length);
writer.Close();
writestream.Close();
}
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.
So we can see the data is encrypted now, in our next how to we will see how to decrypt the data with the help of Unprotect class that is available ProtectedData Class.