0

I seem to be struggling with this code and it would be appreciated if anyone can help.

I have a string of data in web.config file in say the following format: 1, 3, 5, 7, 9, 11, 15, 17, 19.

I need to pass the data into: private static readonly byte[] Entropy but I keep getting the error: The data is invalid

If I use the following:

private static readonly byte[] Entropy = { 1, 3, 5, 7, 9, 11, 15, 17, 19}; it works Ok, so my problem seems to be converting the string into byte[].

I have googled this problem on numerous sites (below are a few)

C# convert string into its byte[] equivalent

http://social.msdn.microsoft.com/Forums/vstudio/en-US/08e4553e-690e-458a-87a4-9762d8d405a6/how-to-convert-the-string-to-byte-in-c-

Converting string to byte array in C#

http://www.chilkatsoft.com/faq/dotnetstrtobytes.html

But nothing seems to work.

As stated above any help would be appreciated.

private static readonly string WKey = ConfigurationManager.AppSettings["Entropy"];

        private static readonly byte[] Entropy = WKey; 

        public static string DecryptDataUsingDpapi(string encryptedData)
        { 
            byte[] dataToDecrypt    = Convert.FromBase64String(encryptedData);
            byte[] originalData     = ProtectedData.Unprotect(dataToDecrypt, Entropy, DataProtectionScope.CurrentUser); 
            return Encoding.Unicode.GetString(originalData);
        }

George

Community
  • 1
  • 1
George Phillipson
  • 830
  • 11
  • 39

1 Answers1

1

You can:

string Entropy = "1, 3, 5, 7, 9, 11, 15, 17, 19";
var parts = Entropy.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
byte[] bytes = Array.ConvertAll(parts, p => byte.Parse(p));

The byte.Parse will "eat" and ignore the spaces. Note that you can't use hex-styles numbers (AB, but without the 0x, so no 0xAB) with that. You would need:

byte[] bytes = Array.ConvertAll(parts, p => byte.Parse(p, NumberStyles.HexNumber));

but then it wouldn't accept not-hex numbers :-)

xanatos
  • 109,618
  • 12
  • 197
  • 280