views:

409

answers:

4

I'm doing some work with a server side service I don't control. The following works fine in standard C#, but fails (due to missing crypto classes) in Silverlight 2 and 3.

    static String DecryptString()
    {
        s = "<cipherTextHere>";

        byte[] toDecryptArray = Convert.FromBase64String(s);
        string key = "<key here>";

        byte[] keyArray = new byte[key.Length / 2];
        for (int i = 0; i < keyArray.Length; i++)
        {
            keyArray[i] = Convert.ToByte(Convert.ToInt32(key.Substring(i * 2, 2), 16));
        }

        using (var algo = new System.Security.Cryptography.AesManaged())
        {

            // The two lines below are the problem
            // as SL does not give me the option to select padding or ciphermode
            algo.Padding = PaddingMode.PKCS7;
            algo.Mode = CipherMode.ECB;

            algo.Key = keyArray;

            ICryptoTransform cTransform = algo.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(
                                 toDecryptArray, 0, toDecryptArray.Length);

            return (UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length));

        }
    }

What are my options?

A: 

Call for the Legion of the Bouncy Castle to help you out of this bind.

Jeffrey Hantin
the problem is they don't appear to have compiled anything for silverlight.
aronchick
Easy enough to custom-compile it for Silverlight; they already have a build for Compact Framework.
Jeffrey Hantin
+1  A: 

You could write your own crypto provider, or you could search for third party components which have already addressed the issue.

I know that xceed have implemented a compression provider for Silverlight so compression and encryption are definately possible.

try

Cryptography Tutorial

Peter
do you have suggestion for rolling my own crypto provider in Silverlight?
aronchick
added hyperlink to original answer
Peter
A: 

Silverlight 2 does has some of the System.Security.Cryptography namespace implemented. See the documentation here. What's missing for you?

Michael S. Scherotter
it was the ability to set the padding and cipher mode that was missing.
aronchick
A: 

Solution from someone smart: "You cannot select padding or ciphermode for AesManaged in Silverlight, so you will have to use the default. Padding by default is PKCS7 just like in your C# code, but cipher mode is CBC."

This works, but now I'm going to have to go convinnce the service provider to switch :(

aronchick