Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

Encrpting and Decrypting data using RSA in Windows Phone

From Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee -)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Windows Phone 8]][[Category:Security]]
+
[[Category:Security on Windows Phone]][[Category:Files/Data on Windows Phone]][[Category:Windows Phone 7.5]][[Category:Windows Phone 8]][[Category:Code Snippet]]
{{Abstract|This article explains how to encrypt and decrypt data using RSA algorithm in Windows Phone 8.0 }}
+
{{Abstract|This article explains how to encrypt and decrypt data using RSA algorithm in [[::Category:Windows Phone|Windows Phone]].}}
  
 
{{Note|This is a community entry in the [[Windows Phone 8 Wiki Competition 2012Q4]].}}
 
{{Note|This is a community entry in the [[Windows Phone 8 Wiki Competition 2012Q4]].}}

Latest revision as of 11:18, 15 July 2013

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

Note.pngNote: This is a community entry in the Windows Phone 8 Wiki Competition 2012Q4.

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: hamishwillee (15 Jul 2013)

[edit] Introduction

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

[edit] 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:

  1. rsaProvider of type RSACryptoServiceProvider
  2. 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;
}
}

[edit] 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)
This page was last modified on 15 July 2013, at 11:18.
1387 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.

×