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

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);         
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you’ll learn about the Win32 Error “0x000019E5 – ERROR_COULD_NOT_RESIZE_LOG” that you get when debugging system erors in...
In this post, you’ll learn about the error “CO_E_DBERROR 0x8004E02B” that is returned when working with COM based APIs or...
In this post, you’ll learn about the Win32 Error “0x000019D0 – ERROR_LOG_BLOCK_VERSION” that you get when debugging system erors in...