Encrpting and Decrypting data using RSA in Windows Phone
This article explains how to encrypt and decrypt data using RSA algorithm in Windows Phone.
Article Metadata
Tested with
Compatibility
Article
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 the code behind file, which in my case is the default MainPage.xaml.cs
In this example, I used RSACryptoServiceProvider class which inherits from RSA class and performs asymmetric encryption and decryption using the RSA algorithm.
Declare the data members:
- rsaProvider of type RSACryptoServiceProvider
- byteConverter of type UnicodeEncoding.
Note that rsaProvider helps in encryption and decryption whereas byteConverter helps in conversion of string to byte array and vice versa.
The following function explains how to encrypt the 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
The following function explains how to decrypt the 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 type using the following code:
this.byteConverter.GetString(decryptedData,0,decryptedData.Length)


Contents
Kiran10182 - Supported Windows Phone OS versions!
Hi Mehul,
Thanks for the article.
I see that the title is bound with Windows Phone 8 platform. I checked the link: http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.security.cryptography.rsacryptoserviceprovider%28v=vs.105%29.aspx and found out the following information from there:
Version Information Windows Phone OS Supported in: 8.0, 7.1
Since it says that it also supports 7.1 version. Is that applicable with this article?
Thanks,
Kiran.kiran10182 21:36, 21 November 2012 (EET)
Hamishwillee - Renamed/Review
Hi Kiran & Mehul
Thanks for this document. I have renamed to make generic WP (and fix spelling of decrypting) because as Kiran says, this will run on any version.
From a general article point of view it would be great if this included a downloadable example that people could try it with. I'd also love some explanation of when you might want to use this library (ie some use cases). You might also compare with the approach in How to encrypt your application data in Windows Phone.
From a competition point of view this isn't a particularly compelling because it doesn't show of a WP8-specific feature. Nor is is comprehensive or innovative. You might make it more impressive by being comprehensive - ie having a documentation covering everything about encryption/decryption on the platform (of course that would be a huge job since there are lots of APIs), or by making it innovative - a use case that interestingly uses WP8 features along with encryption
HamishRegards
hamishwillee 07:55, 22 November 2012 (EET)
Mehul raje - Supported in windows phone 7.1
Hi Kiran,
Thanks for correction, actually above encryption and decryption is supported on windows phone 7.1 also.mehul_raje 15:24, 27 November 2012 (EET)
Kiran10182 - Re: Supported in windows phone 7.1
Thanks Mehul for confirming this and thanks Hamish for renaming the article. :)kiran10182 16:05, 27 November 2012 (EET)