All about Mobile Development
Windows Phone Rockstar in India

How to Encrypt Data in Windows Phone using Data Protection API ?

Free SharePoint Migration Tool
Migrate to SharePoint 2013 & Office 365 for free with Metalogix

How to Encrypt Data in Windows Phone using Data Protection API ?

If you are a Windows Phone Developer and deal with some sensitive data like passwords etc., you can utilize the Data Protection API that is part of the Windows Phone 7.1 SDK.

One of the interesting aspects of the Data Protection API is the built-in Encryption and Decryption via the API.

The Data Protection API includes the methods

Unprotect

  • This function will decrypt the data and return the decrypted data

Protect

  • This function will encrypt the data and returns the encrypted data

The ProtectedData is defined in the namespace System.Security.Cryptography.

The below code will demonstrate how to encrypt a simple string accepted via textbox and later decrypt it.

Note that you can also save the encrypted data in the Isolated Storage or other storage location but for the demo we will see only the encryption and decryption part.

byte[] DecryptedData;         
// Encrypt data         
private void button1_Click(object sender, RoutedEventArgs e)         
{             
     byte[] dataInBytes = Encoding.UTF8.GetBytes(textBox1.Text);             
     DecryptedData = ProtectedData.Protect(dataInBytes, null);         
}         
// Decrypt Data         
private void button2_Click(object sender, RoutedEventArgs e)         
{             
     byte[] PinByte = ProtectedData.Unprotect(DecryptedData, null);             
     textBox1.Text = Encoding.UTF8.GetString(PinByte, 0, PinByte.Length);             
     MessageBox.Show("Decrypted data is : " + textBox1.Text);         
}


Share
 
Comments

No comments yet.

Leave a Reply