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)
m (Hamishwillee - Bot update - Fix metadata)
Vaishali Rawat (Talk | contribs)
(Vaishali Rawat - - How to Encrypt)
Line 32: Line 32:
 
==How to Encrypt==
 
==How to Encrypt==
 
* First create new Windows Phone project in VS 2012
 
* First create new Windows Phone project in VS 2012
* Add namespace {{Icode|System.Security.Cryptography}} in code behind file in my case its default MainPage.xaml.cs
+
* Add namespace {{Icode|System.Security.Cryptography}} in the code behind file, which in my case is the default MainPage.xaml.cs
  
 
In this example i used [http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.security.cryptography.rsacryptoserviceprovider(v=vs.105).aspx RSACryptoServiceProvider] class which inherits from RSA class and it performs asymmetric encryption and decryption using the implementation of the RSA algorithm.
 
In this example i used [http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.security.cryptography.rsacryptoserviceprovider(v=vs.105).aspx RSACryptoServiceProvider] class which inherits from RSA class and it performs asymmetric encryption and decryption using the implementation of the RSA algorithm.

Revision as of 06:29, 15 November 2012

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

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: Vaishali Rawat (15 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 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 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)
1436 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.

×