×
Namespaces

Variants
Actions
Revision as of 06:37, 9 November 2012 by chintandave_er (Talk | contribs)

Encrpting and Decrypting data using RSA in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to encrypt and decrypt data using RSA algorithm in Windows Phone 8.0

WP Metro Icon File.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
Devices(s): Windows Phone 8.0 emulator.
Compatibility
Platform(s): Windows Phone
Windows Phone 8
Windows Phone 7.5
Article
Created: mehul_raje (08 Nov 2012)
Last edited: chintandave_er (09 Nov 2012)

Introduction

This article explains how to use RSA algorithm to encrypt and decrypt data.

How to Encrypt

  • First create new Windows Phone project in VS 2012
  • Add namespace System.Security.Cryptography in code behind file in my case its default MainPage.xaml.cs

In this example i used RSACryptoServiceProvider class which inherits from RSA class and it performs asymmetric encryption and decryption using the implementation of the RSA algorithm.

Declare the datamembers

  1. rsaProvider of type RSACryptoServiceProvider and
  2. byteConverter of type UnicodeEncoding.

Note rsaProvider helps for encryption and decryption while byteConverter helps for conversion of string to byte array and vice versa.

Refer following function to know how to encrypt data

private Byte[] Encrypt(string  dataToEncrypt)
{
Byte[] data = this.byteConverter.GetBytes(dataToEncrypt);
try
{
return this.rsaProvider.Encrypt(data, false);
}
catch (CryptographicException ex)
{
return null;
}
catch (ArgumentNullException ex)
{
return null;
}
}

How To decrypt

Refer following function to know how to encrypt data

 private Byte[] Decrypt(Byte[] dataTodecrypt)
{
try
{
return this.rsaProvider.Decrypt(dataTodecrypt,false);
}
catch (CryptographicException ex)
{
return null;
}
catch (ArgumentNullException ex)
{
return null;
}
}

The second parameter of both Encrypt and Decrypt method of RSACryptoServiceProvider is kept false to use PKCS#1 v1.5 padding, To perform encryption using OAEP padding keep it true. You can convert byte array to string back using following code

this.byteConverter.GetString(decryptedData,0,decryptedData.Length)
605 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×