views:

31

answers:

1

I get a FormatException for this Convert.FromBase64String method. I don't mind about hardcoding the value. Anyone can explain why I get this exception.

        // Instantiate a new RijndaelManaged object to perform string symmetric encryption
        RijndaelManaged rijndaelCipher = new RijndaelManaged();

        // Set key and IV
        rijndaelCipher.Key = Convert.FromBase64String("TASK");
        rijndaelCipher.IV = Convert.FromBase64String("0123");

Thank you.

+3  A: 

Your strings aren't valid Base64.

You need to generate two cryptographically secure 256-bit random numbers, convert them to Base64, and embed them in your source.

For example:

var alg = new RijndaelManaged();
alg.BlockSize = alg.KeySize = 256;
Console.WriteLine("Key: " + Convert.ToBase64String(alg.Key));
Console.WriteLine("IV:  " + Convert.ToBase64String(alg.IV));
SLaks
Can you provide an example?
But aren't these random, I would need the crypto key to decrypt it later? You dont set the Key an IV somewhere!
You can run this code to generate a random key and IV, then embed the results in your source.
SLaks
Thank you for help