2

I try to use python dbus module to connect to WEP security WiFi

I fill the network configuration dictionary like as follows:

nw_config['wep_key0'] = binascii.unhexlify(mypassword)

mypassword is hex-string

when mypassword is set to '12345678' there will be no error, but when it comes in english letters such as a, b, c, d, e, f.

for instance:

nw_config['wep_key0'] = binascii.unhexlify('abcdef')

It will show the following error

UnicodeError: String parameters to be sent over D-Bus must be valid UTF-8 with no noncharacter code points

Just don't understand what difference between these two cases since they should all be valid hex-string?

Update : The code related to dbus

args = dbus.Dictionary(nw_config)
bus = dbus.SystemBus()
wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_PATH)
wpas = dbus.Interface(wpas_obj, WPAS_DBUS_SERVICE)
if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
path = wpas.GetInterface(if_obj, WPAS_DBUS_IFACE)
network = iface.AddNetwork(args)    # this line has problem
Anakin Tung
  • 419
  • 5
  • 17

2 Answers2

0

All the letters, but also the hex digits 8 and 9 have their highest bit set. When this occurs in the upper nibble of a byte, this byte can only be part of a multi-byte UTF-8 sequence. Check out the Wikipedia article on UTF-8 to get further explanations.

Now, not every byte sequence is valid UTF-8, and your data probably isn't. Your code doesn't help either, because it doesn't include the data from the file that you're trying to decode. You should be able to reproduce it with something like this:

data = '0123456789abcdef'
bytes = unhexlify(data)
string = bytes.decode('UTF-8')

You should get the error on the third line.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • Indeed I can reproduce by the code you provide. But that will be strange... Since all the hex key should be valid as a key for WEP? – Anakin Tung Sep 29 '15 at 01:37
  • If the keys are valid, why are you using `unhexlify()` on them? Maybe that is the problem! – Ulrich Eckhardt Sep 29 '15 at 05:18
  • 1
    acutally I have read the following link: http://lists.shmoo.com/pipermail/hostap/2009-February/019326.html this link describes that the WEP key must transform to binary data, so I transform the hex-string to binary by unhexlify. I also set the WEP key to 12345678, and test with the hex-key.unhexlify(), and I can connect to WiFi successfully. – Anakin Tung Sep 29 '15 at 05:45
  • Good research, that article contains the solution to your problem! At least it probably does, I haven't actually tested it. – Ulrich Eckhardt Oct 01 '15 at 14:23
  • yeah, I have tried that. But as I said before this will not worked for the WEP key containing a,b,c,d,e,f. Still don't know why. – Anakin Tung Oct 01 '15 at 14:25
  • Look at what is passed to the call there: `dbus.ByteArray(profile['KEY'])`. The problem there is that `profile['KEY']` is not a byte array, so it doesn't suit as argument to `dbus.ByteArray`. You problem on the other side seems to be that you have a byte array but you don't store it in a `dbus.ByteArray`! – Ulrich Eckhardt Oct 01 '15 at 14:31
  • you are right!!! I just don't think about to pass the byte array to dbus.ByteArray... – Anakin Tung Oct 01 '15 at 15:06
0

Thanks for @Ulrich Eckhardt's help.

Suppose you have a hex string wep key, named pw. Then you have to do the following to success transmit this key in dbus:

dbus.ByteArray(pw.decode('hex'))

Anakin Tung
  • 419
  • 5
  • 17