Daily Deeds: Windows Phone Data Encryption

Well, its nothing new to be said. I was sticking my head on a encrypted sms sender app and was definitely looking for some encryption system over the internet. The thing is I knew a way around on C# as I used it way before. So, I looked it u[ and suddenly it hit me that may be that solution might not work on the silverlight core on Windows Phone 7.5. Just as it turned out I was right. So, I hooked up again on MSDN, found a blog of Rob Tiffany describing a system and I used to make my app! Smile

As I said, daily deed is a daily feed of my lookups, so don’t expect much without codes. Smile

Here’s how I encrypted the data


AesManaged aes = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{

Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

//Create AES algorithm
aes = new AesManaged();
aes.Key = rfc2898.GetBytes(32);
//IV derived from byte array with 16 pseudo-random key bytes
aes.IV = rfc2898.GetBytes(16);

//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

//Encrypt Data
byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();

//Return Base 64 String
return Convert.ToBase64String(memoryStream.ToArray());
}
finally
{
if (cryptoStream != null)
cryptoStream.Close();

if (memoryStream != null)
memoryStream.Close();

if (aes != null)
aes.Clear();
}
}

Points to be remembered here:

    • AES is used here to encrypt and decrypt a message.
    • Salt is needed with the password to get things done and it needs at least 8 characters
    • Anybody can set the iteration at any value, minimum 1000 is preferred.
    • Use same password for encryption and decryption.
    • Code sample is taken from the respective blog. Smile

And the decryption is as followed:


public string Decrypt(string dataToDecrypt, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;

try
{
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);

aes = new AesManaged();

aes.Key = rfc2898.GetBytes(32);
aes.IV = rfc2898.GetBytes(16);

//Create Memory and Crypto Streams
memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

//Decrypt Data
byte[] data = Convert.FromBase64String(dataToDecrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();

//Return Decrypted String
byte[] decryptBytes = memoryStream.ToArray();

//Dispose
if (cryptoStream != null)
cryptoStream.Dispose();

//Retval
return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
}
finally
{
if (memoryStream != null)
memoryStream.Dispose();

if (aes != null)
aes.Clear();
}
}

And Voila! With a SmsComposerTask, my simple app looks like:
onetwothree

This is meant to be a share of my daily experience feed. So, take it easy! Hope it helps! Ciao!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s