Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 29 – Working with Encrypting Data for Security in WP7

 

Introduction:

In this article we are going to see how to encrypt data and use it in Windows phone 7 application development. Security is mainly a concern for developing applications with mobile since misusing of data like Passwords, connection strings etc. are highly possible which we need to take care by encrypting and decrypting where ever possible. In Windows Phone 7 we have Data Protection API which can be used to encrypt the data, storing the confidential data with in the Application Isolated Storage or with encrypting using the local keys is not highly secure since the keys that are required to decrypt the data will reside on the device itself. Data protection API solves this problem of explicitly generating and storing the key. ProtectData class is used to access the Data Protection APIs that can be used with the inbuilt methods which we are going to see in this article.      [more]

Data Protection API’s uses the Protect Method and UnProtect Method which are used to encrypting and decrypting the data as and when required which using the dynamic key generations as and when called. Let us see the step by step process on how to use these methods to encrypt the data and decrypt it in this article.

Steps:

Open visual studio 2010 IDE in administrator mode and create a new Silverlight for Windows phone 7 application with a valid project name as shown in the screen below.

clip_image002

Now let us design the application to get the inputs from the user to encrypt the data and store the pin in the isolated storage using the Protectdata method. Once we added the controls to the page we can see the screen as shown in the screen below.

clip_image004

Let us start with our code behind to add the core logic to encrypt the data, to do that we need to import some namespaces which are not available initially. Copy and paste the below 4 namespaces to the code behind as shown below.

Code Behind:

1

<pre>using System.IO;

2

using System.IO.IsolatedStorage;

3

using System.Text;

4

using System.Security.Cryptography;

Once we added the using statement, we need to add the below code to encrypt the data which the user inputs the data as shown in the screen below. In this code we are going to encrypt the pin to the byte array using the protect method.

clip_image006

Code Behind:

01

private void button1_Click(object sender, RoutedEventArgs e)

02

{

03

byte[] PinByte = Encoding.UTF8.GetBytes(textBlock1.Text);

04

byte[] ProtectedPinByte = ProtectedData.Protect(PinByte, null);

05

this.Writedata(ProtectedPinByte);

06

textBlock1.Text = "";

07

MessageBox.Show("Encrypted the Pin!!!");

08

}

09

 

10

private void Writedata(byte[] pinData)

11

{

12

IsolatedStorageFile ISfile = IsolatedStorageFile.GetUserStoreForApplication();

13

IsolatedStorageFileStream swIS = new IsolatedStorageFileStream(strFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write, ISfile);

14

 

15

Stream writer = new StreamWriter(swIS).BaseStream;

16

writer.Write(pinData, 0, pinData.Length);

17

writer.Close();

18

swIS.Close();

19

}

Now we are done with the code to encrypt the data, we use the Writedata private method to store the data to the Isolated Storage by using the Isolated Stream Class. Once the pin is encrypted we have provided a message box to indicate the user that the data is encrypted correctly. Now let us decrypt the data on the second button click event. Decrypting the data will be done using the UnProtect method which reads the data from the isolated storage medium on the path which we specified and decrypts the pin and get the data as shown in the screen below.

clip_image008

Code Behind:

01

private void button2_Click(object sender, RoutedEventArgs e)

02

{

03

byte[] ProtectedPinByte = this.ReadPinFromFile();

04

byte[] PinByte = ProtectedData.Unprotect(ProtectedPinByte, null);

05

textBlock1.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);

06

 

07

}

08

 

09

private byte[] ReadPinFromFile()

10

{

11

IsolatedStorageFile ISfile = IsolatedStorageFile.GetUserStoreForApplication();

12

IsolatedStorageFileStream rsIS = new IsolatedStorageFileStream(strFilePath, System.IO.FileMode.Open, FileAccess.Read, ISfile);

13

 

14

Stream reader = new StreamReader(rsIS).BaseStream;

15

byte[] pinArray = new byte[reader.Length];

16

 

17

reader.Read(pinArray, 0, pinArray.Length);

18

reader.Close();

19

rsIS.Close();

20

 

21

return pinArray;

22

}

Now we are done with our code part, to check the encryption and decryption, run the application by pressing F5 directly from the keyboard and we can see the application loads on to the Windows Phone 7 Emulator. Input the sample data and we can see the encryption and decryption happening as shown in the screens below.

Output Screens:

clip_image010

Conclusion:

So in this article we have seen the most interesting topic on how to encrypt and decrypt the data using the available API’s which can be used to encrypt the sensitive data as and when required with the application development.

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