0

I would like to know what would be the best way to do the opposite of this post : encode Netbios name python

So to encode, you could use this :

encoded_name = ''.join([chr((ord(c)>>4) + ord('A'))
               + chr((ord(c)&0xF) + ord('A')) for c in original_name])

But to decode this for example :

Netbios_Name= "\x46\x45\x45\x46\x46\x44\x45\x45\x46\x45\x45\x45\x45\x45\x46\x44\x46\x44\x46\x44\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41\x43\x41"
## When correctly reversed, Netbios_Name should output in ASCII : "TESDTDDSSS"

I though about the reverse of this function, but i can't figure why it doesn't work.

Thanks !

Community
  • 1
  • 1
user1473508
  • 195
  • 1
  • 5
  • 15

1 Answers1

1

Use this function (taken from dpkt source code):

def decode_name(nbname):
    """Return the NetBIOS first-level decoded nbname."""
    if len(nbname) != 32:
        return nbname
    l = []
    for i in range(0, 32, 2):
        l.append(chr(((ord(nbname[i]) - 0x41) << 4) |
                     ((ord(nbname[i+1]) - 0x41) & 0xf)))
    return ''.join(l).split('\x00', 1)[0]

So:

>> decode_name(Netbios_Name).strip()
'TESDTDDSSS'
dusan
  • 9,104
  • 3
  • 35
  • 55
  • Thanks, I saw that before, but using it like this would be ripping their code. – user1473508 Nov 30 '12 at 20:55
  • And including that library in your application is an option? (`from dpkt.netbios import decode_name`) – dusan Nov 30 '12 at 20:58
  • I'm trying to build an open source program which doesn't require any external module ! But thanks for you suggestion, appreciated :) – user1473508 Nov 30 '12 at 21:05