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

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

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...