×
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 - code formatting)
chintandave_er (Talk | contribs)
(Chintandave er - used Icode template)
Line 30: Line 30:
 
==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 code behind file in my case its 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.
  
 
Declare the datamembers
 
Declare the datamembers
# 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] and
# 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 {{Icode|rsaProvider}} helps for encryption and decryption while {{Icode|byteConverter}} helps for conversion of string to byte array and vice versa.
  
 
Refer following function to know how to encrypt data
 
Refer following function to know how to encrypt data
Line 80: Line 80:
 
</code>
 
</code>
  
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.
+
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 06:37, 9 November 2012

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

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: chintandave_er (09 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)
332 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.

×