×
Namespaces

Variants
Actions
(Difference between revisions)

Encrpting and Decrypting data using RSA in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
mehul_raje (Talk | contribs)
(Mehul raje -)
 
hamishwillee (Talk | contribs)
m (Hamishwillee -)
 
(18 intermediate revisions by 5 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]].}}
  
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
Line 9: 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= <!-- Format YYYYMMDD -->
+
|creationdate= 20121108
|author=[[User:mehul_raje]]
+
|author= [[User:Mehul raje]]
 
}}
 
}}
  
Line 30: 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 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 performs asymmetric encryption and decryption using the RSA algorithm.
  
Declare the datamembers
+
Declare the data members:
# rsaProvider of type [http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.security.cryptography.rsacryptoserviceprovider(v=vs.105).aspx RSACryptoServiceProvider] and
+
# {{Icode|rsaProvider}} of type [http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.security.cryptography.rsacryptoserviceprovider(v=vs.105).aspx RSACryptoServiceProvider]
# byteConverter of type [http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.text.unicodeencoding(v=vs.105).aspx UnicodeEncoding].
+
# {{Icode|byteConverter}} of type [http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.text.unicodeencoding(v=vs.105).aspx UnicodeEncoding].
  
Note rsaProvider helps for encryption and decryption while byteConverter helps for conversion of string to byte array and vice versa.
+
Note that {{Icode|rsaProvider}} helps in encryption and decryption whereas {{Icode|byteConverter}} helps in conversion of string to byte array and vice versa.
  
Refer following function to know how to encrypt data
+
The following function explains how to encrypt the 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);
 
             try
 
             try
Line 57: Line 59:
 
                 return null;
 
                 return null;
 
             }
 
             }
        }
+
}
 
</code>
 
</code>
  
==How To decrypt==
+
==How to decrypt==
Refer following function to know how to encrypt data
+
The following function explains how to decrypt the data:
 
<code csharp>
 
<code csharp>
 
  private Byte[] Decrypt(Byte[] dataTodecrypt)
 
  private Byte[] Decrypt(Byte[] dataTodecrypt)
        {
+
{
 
             try
 
             try
 
             {
 
             {
Line 77: Line 79:
 
                 return null;
 
                 return null;
 
             }
 
             }
        }
+
}
 
</code>
 
</code>
  
You can convert byte array to string back using following 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.
 +
You can convert byte array to string type using the following code:
 
<code csharp>
 
<code csharp>
      this.byteConverter.GetString(decryptedData,0,decryptedData.Length)
+
this.byteConverter.GetString(decryptedData,0,decryptedData.Length)
 
</code>
 
</code>

Latest revision as of 14: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 14:18.
258 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.

×