3

We have a keyMaterial HEX string. It looks like this

453F1287225ED9971D389A35F8D1032E7748DD0B88302F7C6C194626D4C8659B000000000E800000000200002000000047C2CA7B9A1F1C343CA228CC314A42F063A240E17624F886AF6CE9A135CAF65310000000D30489E536548F129E43240A26344811400000009EE2F5549B1447548ADADDD60A212C22DC2F9B2DC67D8E567B48B3847A525244A2F575AAFFB3AECD0385612BE7C38CA403BE6B5DFA8BEDEFBFA35C5ECC1818AB

You can see the whole file. How can we translate this HEX code to normal text string with C#? I have some examples on C++, it hasn't helped me. Can you give an advise?

THE ENCODING OF THIS STRING IS 16291388 (but it's not a solution of the problem)

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Bitrix24
  • 201
  • 1
  • 4
  • 11
  • 2
    Possible duplicate of [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) – raidensan Apr 21 '16 at 08:02
  • 2
    Before we can answer this question, we need to know the encoding that the string was in before it was converted to hex. UTF8? ASCII? ANSI? UTF16? Something else? – Matthew Watson Apr 21 '16 at 08:03
  • ASCII to HEX and return function is [here](http://rextester.com/LZYTH23443) – Nejc Galof Apr 21 '16 at 08:05
  • THE ENCODING OF THIS STRING IS 16291388 – Bitrix24 Apr 21 '16 at 08:35

3 Answers3

6

If you take the conversion method from here and extend that with conversion of the resulting byte array using the right encoding, you are there.

public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}

// use UTF8 in this sample:
string output = Encoding.UTF8.GetString(StringToByteArray(hex));
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Doesn't look the the original data was UTF8 or UTF16 (Unicode), because using either of those produces nonsensical output. – Matthew Watson Apr 21 '16 at 08:11
  • Sorry, my hex reading skills are a little dusty ;) @MatthewWatson – Patrick Hofman Apr 21 '16 at 08:13
  • Heh, I just tried it with the hex from the OP. It's not the Cyrillic code page either (855) so without knowing how the text was encoded, it's going to be hard to answer completely. But of course, your `StringToByteArray()` is a large part of the answer. – Matthew Watson Apr 21 '16 at 08:16
  • Also, look at all those `00` bytes in the hex string. That doesn't look like any sane string encoding - it looks more like it's got some kind of binary data in there, and it's not an encoded string at all. – Matthew Watson Apr 21 '16 at 08:20
2

If you look at the OP's original file he is trying to decode the keyMaterial of a wireless profile.

This data is not just encoded in hex, it is also encrypted which is why just converting it to a string results in nonsensical data.

<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>TP-LINK_D84690</name>
    <SSIDConfig>
        <SSID>
            <hex>54502D4C494E4B5F443834363930</hex>
            <name>TP-LINK_D84690</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>true</protected>
                <keyMaterial>01000000D08C9DDF0115D1118C7A00C04FC297EB01000000E72F4D29E2AD8E40BBAD5B02842DA10000000000020000000000106600000001000020000000453F1287225ED9971D389A35F8D1032E7748DD0B88302F7C6C194626D4C8659B000000000E800000000200002000000047C2CA7B9A1F1C343CA228CC314A42F063A240E17624F886AF6CE9A135CAF65310000000D30489E536548F129E43240A26344811400000009EE2F5549B1447548ADADDD60A212C22DC2F9B2DC67D8E567B48B3847A525244A2F575AAFFB3AECD0385612BE7C38CA403BE6B5DFA8BEDEFBFA35C5ECC1818AB</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>

This data is encrypted so you cannot just decode it into a string.

CathalMF
  • 9,705
  • 6
  • 70
  • 106
0
    byte[] bytes = StringToByteArray(your hex string here);
    var strArray = (Encoding.Default.GetString(
             bytes,
             0,
             bytes.Length - 1)).Split(new string[] { "\r\n", "\r", "\n", "\0" },
                                         StringSplitOptions.None);
    string output = string.Join("", strArray);

The StringToByteArray Code is here

    public static byte[] StringToByteArray(string hex)
    {
        return Enumerable.Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }
AbdusSalam
  • 420
  • 6
  • 10