#!/usr/bin/perl #--------------------------------------------------------------- # Playfair Viginere script # Author: Rick Saunders # Date: 12 Sept, 2002 # # This script builds a Viginere tableau of 26 standard alpha- # bets and then encrypts/decrypts a section of text following # the rules for the Viginere cipher. #--------------------------------------------------------------- print "Content-type: text/html\n\n"; $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; # Get the input text (either plain or ciphertext) #--------------------------------------------------------------- &FormInput( *input ); $plaintext = uc( $input{ 'textin' } ); # Get the keyword/phrase to build the Visinere matrix #--------------------------------------------------------------- $keyphrase = $akey = uc( $input{ 'keyphrase' } ); $keyphrase =~ s/ //g; @keyrow = split( //, $keyphrase ); # Get the mode.... 1 = 'encrypt' and 0 = 'decrypt' #--------------------------------------------------------------- $mode = $input{ 'mode' }; # Now we create the Viginere tableau. This is a standard # alphabet grid. #--------------------------------------------------------------- for $a ( 0..25 ) { push( @alpharray, substr( $alphabet, $a, 1 ) ); } # Create the full tableau #--------------------------------------------------------------- $string = ""; for $a ( 0..25 ) { $tableau[$a] = join( "", @alpharray ); $letter = shift( @alpharray ); push( @alpharray, $letter ); } # Now do the encrypt/decrypt. Encryptions works as follows: # Going in sequence using the keyword find the alphabet # in the tableau that is to be used. If the keyword is ABC # then use the alphabets in the A, B, C row in order (wrap # when you run out of keyword). Lookup the letter to be # encrypted in the top row (A) and then go down the tableau # to the alphabet to be used. The letter in that alphabet # is the encrypted letter. De-crypt is the exact opposite. #--------------------------------------------------------------- $cypher = ""; $kcount = 0; for $a ( 0..length( $plaintext ) - 1 ) { $letter = substr( $plaintext, $a, 1 ); if ( $letter eq " " ) { $cypher .= " "; } else { $keypos = $kcount % length( $keyphrase ); for $c ( 0..25 ) { if ( substr( $tableau[$c], 0, 1 ) eq $keyrow[$keypos] ) { $tab = $c; } } # If encrypting... if ( $mode ) { for $b ( 0..25 ) { if ( $alpharray[$b] eq $letter ) { $cypher .= substr( $tableau[$tab], $b, 1 ); } } } else { # if decrypting... for $b ( 0..25 ) { if ( substr( $tableau[$tab], $b, 1 ) eq $letter ) { $cypher .= $alpharray[$b]; } } } $kcount++; } } print qq|
|; print " ABCDEFGHIJKLMNOPQRSTUVWXYZ
"; for $a ( 0..25 ) { $hletter = substr( $tableau[$a], 0, 1 ); if ( $keyphrase =~ /$hletter/ ) { print "$hletter "; } else { print "$hletter "; } print "$tableau[$a]\n"; } print "
| ";
print "Key Phrase "; print " $akey "; $header1 = $mode ? "Plaintext" : "Cyphertext"; print "$header1 "; print " ";
$count = 0;
for $f ( 0..length( $plaintext ) -1 ) {
$thisletter = substr( $plaintext, $f, 1 );
if ( ( $count > 50 ) && ( $thisletter eq " " ) ) {
print ""; # Print the output #--------------------------------------------------------------- $header1 = $mode ? "Cyphertext" : "Plaintext"; print "$header1 "; print " ";
$count = 0;
for $f ( 0..length( $cypher ) -1 ) {
$thisletter = substr( $cypher, $f, 1 );
if ( ( $count > 50 ) && ( $thisletter eq " " ) ) {
print "";
print qq|
|