1

I have a developer key

 byte[] key = new Byte[]
            {
                0x01, 0x42, 0xA0, 0xE1, 0x5E, 0xEE, 0xA7, 0x01, 0x71, 0x9A, 0xCB, 0xAB, 0x58, 0xEB, 0xED, 0x44...

that I want to store in my web.config, can I do something like as follows

<add key="spotify-devkey" value="0x01, 0x42, 0xA0, 0xE1, 0x5E, 0xEE, 0xA7, 0x01, 0x71, 0x9A, 0xCB, 0xAB, 0x58, 0xEB, 0xED, 0x44,...

and if I can do it this easily what code would I need to get the byte array from the stored string?

Tom
  • 12,591
  • 13
  • 72
  • 112
  • possible duplicate of [Convert hex string to byte array](http://stackoverflow.com/questions/321370/convert-hex-string-to-byte-array), or [How do you convert Byte Array to Hexadecimal String, and vice versa, in C#?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa-in-c) – Grant Thomas Mar 20 '12 at 12:55

2 Answers2

11

I would use Base64

System.Convert.ToBase64String(key,0,key.Length);

and

System.Convert.FromBase64String(keyStringFromConfig);

to help keep the config file 'clean'

Justin Wignall
  • 3,490
  • 20
  • 23
4
string value = ... // value from config file.
byte[] key = value.Split(new[] {','}).Select(s => Convert.ToByte(s, 16))
                                     .ToArray();
Bala R
  • 107,317
  • 23
  • 199
  • 210