Convert ascii wifi key to hex
Forum: Programming and Scripting
Topic: Convert ascii wifi key to hex
started by: WDef
Posted by WDef on Sep. 18 2007,12:33On at least one wifi network I've tried that required an ascii WEP key, the s: prefix option for dsl's iwconfig - signifying an ascii key - just would not work for me. After I used the hex equivalent instead, it connected immediately.
So I thought a converter from asci keys to hex keys might be handy.
Anyway it made a compact little homework exercise for myself, so thought I might share.
Also did it differently using perl, after googling for the gist:
User should insert hyphens every four chars into the resulting hex key when using with iwconfig.
Posted by ^thehatsrule^ on Sep. 18 2007,13:55Isn't it a colon after every 2 characters? But I don't use wifi...
Anyways, I thought it might be more handy to have the ascii phrase to be passed as an argument.
I believe roberts did use to post a perl link that would do this as well (not sure if included in DSL). Perhaps some kind of integration of this could be used in iwconfig?
- yes, I meant the gui tool in DSL instead of iwconfig
- added the reading of spaces (I think they can be used...)
Posted by WDef on Sep. 18 2007,14:20Thx for interface variant.. The interesting point for me was using bash printf to convert to ordinal then to hex.
No colon in key string for me.
I posted the perl since I did that first - but the core came out of the web - no credit to me other than googling and making it cli. Haven't seen Robert's link.
I guess you mean in dsl's wifi setup GUI. I never use that, but if there's a bug in iwconfig on certain ascii keys with s: perhaps either there's a fix or a 'convert to hex' button might be good.
Has anyone else had trouble with the s: option in iwconfig? Perhaps it was just the one network ...
Posted by curaga on Sep. 18 2007,14:40So it's just the ascii key number converted to hex? Thanks, I was wondering about that..
Could you also include a reverse function in that, if it's going to DSL?
Posted by WDef on Sep. 18 2007,14:47
In dsl or no, if you want one I'll have a go.
Just wondering - why would you want to convert a hex key to ascii - to make it easier to remember perhaps?
Posted by curaga on Sep. 18 2007,14:51Yes. I can also think for situations where you would want to check if it's still correct, without going to the browser, the router config page, logging in etc and then using this in asci2hex way..
Posted by curaga on Sep. 18 2007,14:53Some programs give the key with the dash after every four characters, some with colon after every two. It doesn't really matter, as they are just there to ease reading, and if you happen to give the "wrong" one, the program will just convert it to the form it likes better.
Posted by WDef on Sep. 18 2007,14:58That clarifies that.
Posted by WDef on Sep. 18 2007,15:40Curaga, here's quick 'n' dirty reverse:
Will ignore - or : in input.
These are for 13 char asci-equivalent keys.
Posted by WDef on Sep. 18 2007,19:57A Perl version, since I definitely need the practice:
EDIT: Perl version as is can't cope with $ as an illegal input hex char. Gives superior built-in error messaging and tells you that it is ignoring which illegal hex chars.
The bash script runs in around the same time or faster, despite the pipes (subshells), read and 2 sed invocations - not that speed matters here.
Equivalent functionality in bash to both flag and remove illegal hex chars is like:
which is more sed than I usually care to learn and shows why I like Perl. No doubt this could be made better. To cope with $ as a non-hex input char, bash script argument needs to be put in single quotes.
A more readable alternative bash script might parse the input char by char instead.
Posted by WDef on Sep. 22 2007,23:37Continuing explorations, may be of limited interest to anyone else but at least I'll know where to find it again.
Doing away with while and read:
I'm not really sure why that t branching test works quite as it does. So thought I'd try to do without it and let sed take over:
Posted by WDef on Sep. 23 2007,09:12Or Sed-free, parse char-by-char:
Slower, even without the temp file.
Posted by WDef on Sep. 23 2007,11:07Bettter: no subshell, no read, no temp file.
Still less efficient than sed, but then sed is made to do this stuff.
Posted by WDef on Oct. 06 2007,21:18Yes, I know, (groan), "he still hasn't given up on this thread".
(Pls bear with me while I post my self-education exercises).
Much easier in C:
And (todo: doesn't yet report any ignored non-hex input):