TripleDES cryptography on Silverlight and Windows Phone

The .net framework on Silverlight does not contains any implementation in the System.Security.Cryptography namespace... There is just the Aes for crypting data, there is no DES or TripleDES.

After googling for TripleDES on silverlight, I did not find any cool things.. but I found a simple managed implementation of DES and TripleDES http://www.broccoliproducts.com/softnotebook/desblowfish/desblowfish.php

The code is clean but I refactored it to be more .net framework friendly, such as implementing ICryptoTransform and SymmetricAlgorithm.

Now, you can crypted your data with the DES or TripleDES algorithm on your Windows Phone. Here an example:

 

var toEncrypt = Encoding.UTF8.GetBytes("Hi my friends ! I am safe with TripleDES :)");
 
TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider();
 
// Create the encryptor
ICryptoTransform encryptor = tdesAlgorithm.CreateEncryptor();
var cryptedBytes = encryptor.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
 
// Create the decryptor
ICryptoTransform decryptor = tdesAlgorithm.CreateDecryptor();
var decryptedBytes = decryptor.TransformFinalBlock(cryptedBytes, 0, cryptedBytes.Length);
 
var uncryptedString = Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length);

The solution works perfectly with the .net framework implementation, you can encrypt with .net base class and decrypt with our managed class:

var IV = new byte[8 * 3];
 
var toEncrypt = Encoding.UTF8.GetBytes("Hi my friends ! I am safe with TripleDES :)");
 
TripleDESCryptoServiceProvider tdesAlgorithm = new TripleDESCryptoServiceProvider();
tdesAlgorithm.Padding = PaddingMode.PKCS7;
 
Phone7.Fx.Preview.Cryptography.TripleDESCryptoServiceProvider phonetdesAlgorithm = new Phone7.Fx.Preview.Cryptography.TripleDESCryptoServiceProvider();
 
var key = tdesAlgorithm.Key;
 
// Create the encryptor
ICryptoTransform encryptor = tdesAlgorithm.CreateEncryptor(key, IV);
var cryptedBytes = encryptor.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length);
 
// Create the decryptor
ICryptoTransform decryptor = phonetdesAlgorithm.CreateDecryptor(key, IV);
var decryptedBytes = decryptor.TransformFinalBlock(cryptedBytes, 0, cryptedBytes.Length);
 
var uncryptedString = Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length);

Of course, the complete source code is available here: Phone7.Fx.Preview.zip (17,05 kb)

Happy encryption Laughing

Comments (1) -

Robert W. Anderson
Robert W. Anderson
9/3/2011 4:15:34 PM #

This is a very helpful content.  It is informative therefore you have not surprisingly conducted your study well before composing this unique content.  I must come back and pursue your potential articles or blog posts.

Pingbacks and trackbacks (1)+

Comments are closed

Hi there !

 

Specialized in .net technologies for many years, I am a technology fan in both asp.net and wpf/silverlight, using c# and .net 4.5

Taking advantages of new opportunities offered by the Windows Azure platform and WP/Win 8, I develop applications for Windows Phone, 5 of them are already available on the market place.


 

 

Month List