There may be many scenarios, when you need to save data in protected form for your application in isolated storage. Of course you have an option

  • To encrypt the data with some encryption algorithm
  • Save in the isolated storage

Problem with above approach is either you will have to store the key in isolated storage or read it from some external sources. In major scenarios key is residing on the phone itself and it brings the flaw.

Windows Phone Data Protection API helps us to protect data at the application level. On Windows Phone 7 every application has their own key. This key gets created when you first run the application.

Using DP API, all it takes one line of code to encrypt and decrypt the data.

To Encrypt

image

First line of code is converting string to byte array. To encrypt data only you need to pass byte array.

To Decrypt

image

Password is name of the file in which encrypted data is stored. All it takes only one line of code to decrypt.

If you closely look into ProtectedData class,

image

This is static class with two static methods. If required you can pass entropy as parameter also.

Design page

Now let us design a page like below, Design is very simple with one text box taking input to protect the data. There are two buttons, one to protect data and another to decrypt and retrieve protected data.

clip_image001

XAML of design is as below,


<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="PageTitle" Text="protected data" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}"/>
</StackPanel>
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="180" />
</Grid.RowDefinitions>
<TextBox x:Name="txtDataToProtect" HorizontalAlignment="Left" Width="450" Height="80" />
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" Grid.Row="1">
<Button x:Name="btnReteriveData" Content="Reterive" Height="100"  Click="btnReteriveData_Click" Width="200" />
<Button x:Name="btnProtectData" Content="Protect" Height="100"  Click="btnProtectData_Click" Width="260" />
</StackPanel>
</Grid>
</Grid>

Protecting Data


private void btnProtectData_Click(object sender, RoutedEventArgs e)
{
byte[] passwordData = Encoding.UTF8.GetBytes(txtDataToProtect.Text);
byte[] EncryptedPasswordData = ProtectedData.Protect(passwordData, null);
SaveToFile(EncryptedPasswordData, "password");
}

In above code,

  1. We are converting text to byte array
  2. Passing byte array to protect
  3. We are applying no entropy.
  4. Saving protected data to a file called password. We will have to fetch encrypted data from file password while retrieving.
  5. To save protected data calling a function called SaveToFile

SaveToFile function


private void SaveToFile(byte[] EncryptedPasswordData,string FileName)
{
IsolatedStorageFile getApplicationFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileAsStream = new IsolatedStorageFileStream(FileName, System.IO.FileMode.Create, FileAccess.Write, getApplicationFile);
Stream writer = new StreamWriter(fileAsStream).BaseStream;
writer.Write(EncryptedPasswordData, 0, EncryptedPasswordData.Length);
writer.Close();
fileAsStream.Close();
}

In above code,

  1. We are passing byte array to save
  2. We are passing filename to say where to save the data in application
  3. Reading application file and opening it as file stream
  4. Writing byte array to file stream.

Decrypting data


private void btnReteriveData_Click(object sender, RoutedEventArgs e)
{
byte[] data = ReadFromFIle("password");
byte[] passwordByte = ProtectedData.Unprotect(data, null);
string password = Encoding.UTF8.GetString(passwordByte, 0, passwordByte.Length);
txtDataToProtect.Text = password;

}

In above code,

  1. We are first reading encrypted data from the isolated storage file. If you notice file name is same.
  2. Calling unprotect method and passing encrypted byte array to decrypt.
  3. Converting decrypted byte array to string and displaying in text box

ReadFromFile function


private byte[] ReadFromFIle(string FileName)
{

IsolatedStorageFile getApplicationFile = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fileAsStream = new IsolatedStorageFileStream(FileName, System.IO.FileMode.Open, FileAccess.Read, getApplicationFile);
Stream reader = new StreamReader(fileAsStream).BaseStream;
byte[] password = new byte[reader.Length];
reader.Read(password, 0, password.Length);
reader.Close();
fileAsStream.Close();
return password;
}

In above code,

  1. Reading application file and opening it as file stream
  2. Reading byte array to file stream.

If you would have noticed we have not provided KEY either to ENCRYPT or DECRYPT the data.

In this way you can protect data in Windows Phone 7. I hope this post was useful. Thanks for reading Smile

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Advertisements

10 thoughts on “Protecting password or any data in Windows Phone 7 using Data Protection API

  1. If the application key is created first time the app runs on the particular phone, this means that the following scenario will not work:
    – Install the app, encrypt the data;
    – Back up the data (to the cloud);
    – Uninstall the app;
    – Install the app again;
    – Restore the data from the cloud.
    At this stage, the data cannot be decrypted, because the second time the app was installed and run, it gets a different key.
    Right?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s