In this article I will discuss about how to encrypt and decrypt data in Windows Phone. Windows phone uses Data Protection API (DPAPI) to encrypt and decrypt data. Every application of Windows Phone creates descryption key when it is run first time. We will Protect and Unprotect method of ProtectedData class of System.Security.Cryptograph.
Let's write code:
Step 1: Add below code inside contentpanel of MainPage.xaml.
<TextBlock Text="Text to encrypt" Height="30" HorizontalAlignment="Left" Margin="0,15,0,0" Name="textDefault" VerticalAlignment="Top" FontSize="18" /> <TextBox InputScope="Default" Height="70" HorizontalAlignment="Left" Margin="120,0,0,0" Name="txtText" Text="" VerticalAlignment="Top" Width="320" /> <TextBlock Text="Entropy" Height="30" HorizontalAlignment="Left" Margin="10,75,0,0" Name="textEntropy" VerticalAlignment="Top" FontSize="18" /> <TextBox InputScope="Default" Height="70" HorizontalAlignment="Left" Margin="120,60,0,0" Name="txtEntropy" Text="" VerticalAlignment="Top" Width="320" /> <TextBlock Text="Encrypted Text" Height="30" HorizontalAlignment="Left" Margin="10,135,0,0" Name="textEncrypted" VerticalAlignment="Top" FontSize="18" /> <TextBox InputScope="Default" Height="70" HorizontalAlignment="Left" Margin="120,120,0,0" Name="txtEncrypted" Text="" VerticalAlignment="Top" Width="320" /> <TextBlock Text="Decrypted Text" Height="30" HorizontalAlignment="Left" Margin="10,190,0,0" Name="textDecrypted" VerticalAlignment="Top" FontSize="18" /> <TextBox InputScope="Default" Height="70" HorizontalAlignment="Left" Margin="120,180,0,0" Name="txtDecrypted" Text="" VerticalAlignment="Top" Width="320" />
<Button x:Name="Encrypt" Margin="20,340,0,0" Height="80" Width="430" Click="Encrypt_Click" Content="Encrypt" /> <Button x:Name="Decrypt" Margin="20,480,0,0" Height="80" Width="430" Click="Decrypt_Click" Content="Decrypt" />
Step 2: Add a class level variable FileName.
public partial class MainPage : PhoneApplicationPage { string FileName = "SecuredFile";
Step 3: Place below method in MainPage.xaml.cs which will write the encrypted data to a file and store in IsolatedStorage.
private void WriteTextToFile(byte[] textData) { IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream writestream = new IsolatedStorageFileStream(FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, file); Stream stream = new StreamWriter(writestream).BaseStream; stream.Write(textData, 0, textData.Length); stream.Close(); writestream.Close(); }
Step 4: Place below method in MainPage.xaml.cs which will read the encrypted data from file placed in IsolatedStorage.
private byte[] ReadTextFromFile() { IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication(); IsolatedStorageFileStream readstream = new IsolatedStorageFileStream(FileName, System.IO.FileMode.Open, FileAccess.Read, file); Stream reader = new StreamReader(readstream).BaseStream; byte[] textArray = new byte[reader.Length]; reader.Read(textArray, 0, textArray.Length); reader.Close(); readstream.Close(); return textArray; }
Step 5: Place below method which will be triggered on click of Encrypt button. Protect method of ProtectedData takes two arguments first the text to be encrypted and second takes entropy to add additional complexity of encryption
private void Encrypt_Click(object sender, RoutedEventArgs e) { byte[] TextByte = Encoding.UTF8.GetBytes(txtText.Text); byte[] entropyByte = Encoding.UTF8.GetBytes(txtEntropy.Text); byte[] ProtectedTextByte = ProtectedData.Protect(TextByte, entropyByte); this.WriteTextToFile(ProtectedTextByte); txtEncrypted.Text = Encoding.UTF8.GetString(ProtectedTextByte, 0, ProtectedTextByte.Length); }
Step 6: Place below methond also in MainPage.xaml.cs to decrypt encrypted data. Unprotect method of ProtectedData takes two arguments first the text to decrypted and second takes entropy to decrypt the encrypted data.
private void Decrypt_Click(object sender, RoutedEventArgs e) { byte[] entropyByte = Encoding.UTF8.GetBytes(txtEntropy.Text); byte[] ProtectedTextByte = this.ReadTextFromFile(); byte[] TextByte = ProtectedData.Unprotect(ProtectedTextByte, entropyByte); txtDecrypted.Text = Encoding.UTF8.GetString(TextByte, 0, TextByte.Length); }
Step 7: Now run the application and the screen shown below will appear where text can be encrypted and decrypted.

This ends the article of encrypting and decrypting text in Windows Phone.
|