#! /usr/local/bin/perl5 # # ElGamal - Controlling Script # William McHale # May 10, 1998 # # This program is a controlling script for the El Gamal cipher. It is # based on the script that was used for the RSA cipher. # # Perl was used for the input and output for the programs because of the # ability it brings to handle text quickly and easily. # ###################################################################### #require Math.pm; use Math::BigInt; if (!($#ARGV == 2 && $ARGV[0] eq g) && ($#ARGV != 3)){ print STDOUT "USAGE: \n"; print STDOUT " ElGamal.pl g pubkey privkey\n"; print STDOUT " ElGamal.pl e/d keyfile intext outtext\n"; } my ($option, $keyfile, $infile, $outfile) = @ARGV; my (@text_array, $counter, @substring); $counter = 0; if ($option eq "g") { open (PUBKEY, ">$keyfile"); open (PRIVKEY, ">$infile"); my (@key) = `java generateElGamalKey`; print PUBKEY "$key[0]"; print PUBKEY "$key[1]"; print PUBKEY "$key[2]"; print PRIVKEY "$key[0]"; print PRIVKEY "$key[3]"; close PUBKEY; close PRIVKEY; } elsif ($option eq "e"){ open (IN, "$infile"); open (OUT, ">$outfile"); # Read in the keyfile. (since the key requires minimum parsing we # will just use more on it, with each part of the key on a different # line @keys = `more $keyfile`; my ($P) = $keys[0]; chomp $P; my ($alpha) = $keys[1]; chomp $alpha; my ($beta) = $keys[2]; chomp $beta; # Read in the textfile, one line at a time. Then chop line up into # individual chars which are then placed on the text_array. This # array can then be chopped for encryption. while (){ my ($line) = $_; my (@chars) = split (//, $line); foreach $char (@chars){ push (@text_array, $char); } } close IN; # Go through the array, and when eight characters are grabbed # encode them into an ascii number. foreach (@text_array){ $substring[$counter] = $_; $counter ++; # the below is normally 8 if ($counter == 2){ my ($temp) = text2asci (@substring); my (@encrypt_out) = `java encryptElGamal $temp $P $alpha $beta`; print OUT "$encrypt_out[0]"; print STDOUT "$temp, $encrypt_out[0]\n"; $counter = 0; } } #pad any remaining unused chars in substring with Nulls if ($counter != 0){ while ($counter < 8){ $substring[$counter] = '\0'; $counter ++; } my ($temp) = text2asci (@substring); my (@encrypt_out) = `java encryptElGamal $temp $P $alpha $beta`; print OUT "$encrypt_out[0]\n"; } close OUT; } elsif ($option eq 'd'){ my (@cipher_text); # open the file to be used for output. open (OUT, ">$outfile"); # Read in the keyfile. (since the key requires minimum parsing we # will just use more on it, with each part of the key on a different # line @keys = `more $keyfile`; my ($A) = $keys[1]; chomp $A; my ($P) = $keys[0]; chomp $P; # As the cipher text is one number per line, we will just use # more to read it in. @cipher_text = `more $infile`; # Now grab each number, decrypt, convert to ascii, and then # read out to the output file. foreach $item (@cipher_text){ chomp $item; my (@items) = split(/\ /, $item); my ($Y1, $Y2) = @items; my (@plainvalue) = `java decryptElGamal $Y1 $Y2 $P $A`; my (@plaintext) = asci2text ($plainvalue[0]); print OUT "$plaintext[0]"; } close OUT; } #@foo = `java test`; #chomp $foo[0]; #$bar = "$foo[0]45623452\n"; # #print "this is a test \n"; #print "$bar"; #print "THATS ALL FOLKS\n"; # #@temp = (a, b, c, c, d); #my ($testValue) = text2asci (@temp); #print "$testValue\n"; # #$xt = asci2text ($testValue); #print "__ $xt __\n"; # This function will take a string of text and turn it into a number based off # of the ASCI values of the string. The first char will be equal to its # ASCI value and the second 1000 times its asci value the third 1000,000, etc. # It will continue this conversion until there is no more chars left to # convert. Note the number returned will be a BigInt. Also note that the # size of the string to be converted must be controlled outside this function. # This function will simply convert as many chars as it can. sub text2asci{ my (@theString) = @_; my ($value) = Math::BigInt->new("0"); my ($counter) = Math::BigInt->new("1"); my ($iterator) = Math::BigInt->new("1000"); foreach $aChar (@theString){ my ($temp) = ord $aChar; $value = $value + ($temp * $counter); $counter *= 1000; } my (@temp) = split (/\+/, $value); return $temp[1]; } # modification by Christopher Toole sub asci2text{ my ($theNumber) = Math::BigInt->new(@_); my ($divisor) = Math::BigInt->new("1000"); my ($theString) = ""; while ($theNumber->bcmp(&bigZero) != 0){ my ($val) = $theNumber % $divisor; $theNumber = $theNumber / $divisor; my (@temp) = split (/\+/, $val); my ($aChar) = chr ($temp[1]); print ("** $val, $aChar **\n"); $theString = "$theString$aChar"; } return $theString; } sub bigZero{ my ($a) = Math::BigInt->new("0"); return $a; }