Wriju's BLOG

.NET, Cloud and everything

Windows Phone Security : Encrypt and Decrypt

Often times you may want to secure the user data. However, there is one native API to support

using System.Security.Cryptography;
using System.Text;
byte[] secretPassword; // to store it in a variable 
private void btnEncrypt_Click(object sender, RoutedEventArgs e)
    //Convert the text to byte[] 
    byte[] clearPassword = Encoding.UTF8.GetBytes(txtData.Text);

    secretPassword = ProtectedData.Protect(clearPassword, null);

    txtDecryptText.Text = "Encrypted...";
    txtData.Text = "";

private void btnDecrypt_Click(object sender, RoutedEventArgs e)

    byte[] protectedPassowrd = secretPassword;
    //Decrypt the text
    byte[] unprotectPassword = ProtectedData.Unprotect(protectedPassowrd, null);

    txtDecryptText.Text = Encoding.UTF8.GetString(unprotectPassword, 0, unprotectPassword.Length);
    txtDecryptText.Text = "Decrypted...";