Programming and Scripting :: Convert ascii wifi key to hex



On 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.

In bash:

Code Sample
#!/bin/bash
#
# asci2hexkey.sh
# Coverts ascii key to hex on the fly

ASCIKEY=""
echo "Type in ascii wifi key:"
while read -s -n 1 ASCHAR; do
[ "$ASCHAR" = "" ] && break
printf "%X" $(printf "%d" \'"$ASCHAR"\')
ASCIKEY=${ASCIKEY}${ASCHAR}
done

echo " = ascii key \"$ASCIKEY\""


Also did it differently using perl, after googling for the gist:

Code Sample
#!/usr/bin/perl
#
# asci2hexkey.pl
# Takes asci key as command line argument & prints hex
#

use strict;
use warnings;

my $asciichars = shift;
chomp $asciichars;

my $hexchars = '';
foreach my $c (split(//,$asciichars)) {
$hexchars .= sprintf "%x", ord($c);
}
print "$hexchars\n";


User should insert hyphens every four chars into the resulting hex key when using with iwconfig.

Isn'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.

Code Sample
#!/bin/bash
#
# asci2hexkey2.sh
# Accepts an ascii key and outputs the hex equivalent, delimited by colons

if [ $# -ne 1 ]; then
   exit 1
fi

FIRST=1
IFS=""

printf $1 |
while read -s -n 1 ASCHAR; do
   if [ $FIRST -eq 0 ]; then
       printf ":"
   else
       FIRST=0
   fi
   [ "$ASCHAR" = "" ] && break
   printf "%X" $(printf "%d" \'"$ASCHAR"\')
done

echo


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?

EDIT:
- yes, I meant the gui tool in DSL instead of iwconfig
- added the reading of spaces (I think they can be used...)

Thx 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.

Quote
Perhaps some kind of integration of this could be used in iwconfig?


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 ...

So 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?

Quote
include a reverse function


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?

Next Page...
original here.