×
Namespaces

Variants
Actions
(Difference between revisions)

Encrpting and Decrypting data using RSA in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
chintandave_er (Talk | contribs)
(Chintandave er - added WP8 contest template.)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
Line 11: Line 11:
 
|platform= Windows Phone
 
|platform= Windows Phone
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->  
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|creationdate= 20121108
 
|creationdate= 20121108
|author=[[User:mehul_raje]]
+
|author= [[User:Mehul raje]]
 
}}
 
}}
  
Line 44: Line 44:
 
Refer following function to know how to encrypt data
 
Refer following function to know how to encrypt data
 
<code csharp>
 
<code csharp>
private Byte[] Encrypt(string dataToEncrypt)
+
private Byte[] Encrypt(string dataToEncrypt)
 
{
 
{
 
             Byte[] data = this.byteConverter.GetBytes(dataToEncrypt);
 
             Byte[] data = this.byteConverter.GetBytes(dataToEncrypt);
Line 82: Line 82:
 
</code>
 
</code>
  
The second parameter of both Encrypt and Decrypt method of {{Icode|RSACryptoServiceProvider}} is kept false to use PKCS#1 v1.5 padding, To perform encryption using OAEP padding keep it true.
+
The second parameter of both Encrypt and Decrypt method of {{Icode|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
 
You can convert byte array to string back using following code
 
<code csharp>
 
<code csharp>
 
this.byteConverter.GetString(decryptedData,0,decryptedData.Length)
 
this.byteConverter.GetString(decryptedData,0,decryptedData.Length)
 
</code>
 
</code>

Revision as of 07:07, 13 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: hamishwillee (13 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)
518 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.

×