WEP Key Generator

From Buici

Jump to: navigation, search

Resources for this most simple of algorithms are difficult to find on the Internet because so many people want to sell a program to do it. The Perl code below converts an ASCII passphrase into a 104 bit WEP key.

#!/usr/bin/perl
# Accepts a passphrase on the command line and converts it to a WEP key.
# The algorithm extends the passphrase to 64 characters and runs it
# through md5sum.  The result is the WEP key.

use Digest::MD5 qw(md5);

my $phrase = shift;
$phrase = substr ($phrase, 0, 64) if (length ($phrase) > 64);

sub usage {
    print "usage: wepkeygen PHRASE\n";
    print "  Enclose PHRASE in quotes if it includes whitespace.\n";
    print "  PHRASE was '$phrase'\n";
    exit 1;
}

usage () if length ($phrase) == 0 || $#ARGV != -1;

my $md5 = Digest::MD5->new;
my $c = 64;

while ($c >= length ($phrase)) {
    $md5->add ($phrase);
    $c -= length ($phrase);
}
$md5->add (substr ($phrase, 0, $c)) if $c;
my $key = substr ($md5->hexdigest, 0, 26);
print "     passphrase: \"$phrase\"\n";
print "104 bit WEP key: $key\n";

0;
Personal tools