views:

1186

answers:

1

Hi,

When creating the instance, the KEY and IV are generated for me.

 RijndaelManaged myRijndael = new RijndaelManaged();

How can I store the Key in my database or web.config file?
And in what format?

Because I will have to load the key when trying to decrypt the encrypted string obviously.

thanks for your help, a little lost on this topic.

+5  A: 

When storing binary as text (regardless if to a text file or a database field), the Base64 encoding is the way to go.

RijndaelManaged myRijndael = new RijndaelManaged();

// to Base64
string keyb64 = Convert.ToBase64String(myRijndael.Key);

// reverse
myRijndael.Key = Convert.FromBase64String(keyb64);

The Base64 string is safe to store anywhere you like.

Tomalak
thanks man, I just figured it out myself, but still need some 'confirmation'. cheers!
btw, why base64?
"Why use it?" or "Why is it called like that?"?
Tomalak
As to "Why use it?": It encodes binary bytes to a limited set of safe characters (out of the ASCII range). These characters do not get tampered with during mail transfer, are not affected by code page issues and Base64 is an agreed standard widely supported and easy to implement.
Tomalak
As to "Why is it called like that?": Because it is _based_ on an alphabet of 64 characters.
Tomalak
You can also store your key as a byte array in the database, however its much more easy to support a base64 encoded string.
Will
Ok so its gauranteed to have all the mappings coming from the 256 bit array? I would have thought there would be more than 64 characters required to map the 256 bit array!
No, there are not. That's the trick with Base64 - you can map 256 values to 64 at the expense of string length. Base64 simply needs more bytes than byte[]. See http://en.wikipedia.org/wiki/Base64
Tomalak
BTW: If that answered your question, it would be nice if you accepted the answer. :-)
Tomalak