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

Posted by ^thehatsrule^ on Sep. 18 2007,13:55
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...)

Posted by WDef on Sep. 18 2007,14:20
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 ...

Posted by curaga on Sep. 18 2007,14:40
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?

Posted by WDef on Sep. 18 2007,14:47
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?

Posted by curaga on Sep. 18 2007,14:51
Yes. 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:53
Some 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:58
That clarifies that.
Posted by WDef on Sep. 18 2007,15:40
Curaga, here's quick 'n' dirty reverse:

Code Sample

#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#

echo $1 | sed 's/[-:]//g' | while read -n 2 HEXCH; do printf "%s\x$HEXCH" 2>/dev/null; done | sed 's/\\\x$/\n/'


Will ignore - or : in input.

These are for 13 char asci-equivalent keys.

Posted by WDef on Sep. 18 2007,19:57
A Perl version, since I definitely need the practice:

Code Sample
#!/usr/bin/perl


#
# hex2ascikey.pl
# Takes hex wifi key as argument and prints asci
#

use strict;
use warnings;

my $hexkey = shift;
chomp $hexkey;
$hexkey =~ s/^[+]+//;  # or can't cope with a leading +
$hexkey =~ s/[:-]//g;

# Insert space every 2 chars
$hexkey =~ s/([^\s]{2})/$1 /gs;

my @A = split (/ /, $hexkey);

foreach my $c (@A){
my $dec = hex($c);
print chr($dec);
}

print "\n";



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:

Code Sample

#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#

echo -n $1 | sed  's/[0-9A-Fa-f]//g
t message
:message s/^.\+$/Ignoring non-hex chars &\n/'

echo -n $1 | sed 's/[^0-9A-Fa-f]//g' | while read -n 2 HEXCH; do
printf "%s\x$HEXCH" 2>/dev/null; done | sed 's/\\\x$//'

echo


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:37
Continuing 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:

Code Sample
#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#
echo -n $1 | sed  's/[0-9A-Fa-f]//g
t message
:message s/.\*/Ignoring non-hex chars &\n/'

echo -n $1 | sed 's/[^0-9A-Fa-f]//g; s/../\"%s\\x&\"/g' | xargs printf

echo


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:

Code Sample

#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#

echo -n $1 | sed '
H
s/[0-9A-Fa-f]//g
/[^0-9A-Fa-f]/{
s/^/\"Ignoring non-hex chars \"/
}
x
s/[^0-9A-Fa-f]//g
s/../\"%s\\x&\"/g
s/^/"%s\n"/
G
' | xargs printf

echo

Posted by WDef on Sep. 23 2007,09:12
Or Sed-free, parse char-by-char:
Code Sample

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#

ASC=""
IGNORED=""
igntemp=`mktemp`

echo -n $1 | while read -n 1 CH; do

case $CH in
[0-9A-Fa-f])
ASC="${ASC}${CH}"
if [ ${#ASC} -eq 2 ]; then
printf "%s\x$ASC"
ASC=""
fi;;
*) IGNORED="${IGNORED}${CH}"
;;
esac
echo ${IGNORED} >${igntemp}
done
echo
echo "Ignored non-hex chars `cat ${igntemp}`"
rm -f ${igntemp}


Slower, even without the temp file.

Posted by WDef on Sep. 23 2007,11:07
Bettter: no subshell, no read, no temp file.

Still less efficient than sed, but then sed is made to do this stuff.

Code Sample
#!/bin/bash

#
# hex2ascikey.sh
# Takes hex wifi key as argument and prints asci
#


ASC=""
IGNORED=""


A="$1"


until [ ${#A} -eq 0 ]; do
B=${A#?}
CH=${A%%$B}

case $CH in
[0-9A-Fa-f])
ASC="${ASC}${CH}"
if [ ${#ASC} -eq 2 ]; then
printf "%s\x$ASC"
ASC=""
fi;;
*) IGNORED="${IGNORED}${CH}"
;;
esac
A=${B}
done

echo
[ -n  "${IGNORED}" ] && echo "Ignored non-hex chars ${IGNORED}"

Posted by WDef on Oct. 06 2007,21:18
Yes, 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:

Code Sample
//~ asci2hex - converts ascii wifi key to hex

#include <stdio.h>

main()
{
char ch;
printf("Enter the ascii key: ");
while ( ch= ' ' ){
ch = getchar();
if (ch == '\n')
break;
printf("%X", ch);
}
printf("\n");
}


And (todo: doesn't yet report any ignored non-hex input):

Code Sample
//~ asci2hex - converts hex wifi key to ascii

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


char hexToAscii(char first, char second)
//~ http://snippets.dzone.com/posts/show/2073
{
char hex[3], *stop;
hex[0] = first;
hex[1] = second;
hex[2] = 0;
return strtol(hex, &stop, 16);
}


main()
{
char ch1, ch2;

printf("Enter the hex key: ");
while (1){
ch1 = getchar();
if (ch1 == '\n')
break;
if (!isxdigit(ch1))
continue;
ch2 = getchar();
if (ch2 == '\n')
break;
if (!isxdigit(ch2))
continue;
printf("%c", hexToAscii(ch1, ch2));
}
printf("\n");
}

Powered by Ikonboard 3.1.2a
Ikonboard © 2001 Jarvis Entertainment Group, Inc.