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)

4
  • 2
    Possible duplicate of How do you convert Byte Array to Hexadecimal String, and vice versa? Commented Apr 21, 2016 at 8: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? Commented Apr 21, 2016 at 8:03
  • ASCII to HEX and return function is here Commented Apr 21, 2016 at 8:05
  • THE ENCODING OF THIS STRING IS 16291388 Commented Apr 21, 2016 at 8:35

3 Answers 3

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));
Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't look the the original data was UTF8 or UTF16 (Unicode), because using either of those produces nonsensical output.
Sorry, my hex reading skills are a little dusty ;) @MatthewWatson
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.
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.
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.

5 Comments

Aha. That explains it.
"you cannot just decode it into a string" ... unless you know the encryption algorithm and the salt, etc.
@PatrickHofman Well ya! :) I didnt say "you cannot decode it into a string". The "Just" makes all the difference. :)
WEP isn't that hard to decrypt. WPA2 is.
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();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.