#!/usr/bin/perl -w use strict; # Vigenere cipher toolkit. Encrypt and Decrypt. # Cracking not included. # my $plaintext = "This is not secure, but it's kind of fun."; my $key = 'perlmonks'; # By definition, letters only. my $ciphertext; my $derived_plaintext; # For convenience, do the work in lowercase... $plaintext = lc($plaintext); $key = lc($key); # By definition, only encipher letters, and only use letters in the key... $plaintext =~ s/[^a-z]//g; $key =~ s/[^a-z]//g; if(length($key) < 1){ die "Key must contain at least one letter.\n"; } print "plaintext is [$plaintext]\n\n"; $ciphertext = encrypt_string( $plaintext, $key); $derived_plaintext = decrypt_string( $ciphertext, $key ); print "Plaintext used :$plaintext\n"; print "Ciphertext :$ciphertext\n"; print "Recreated plaintext :$derived_plaintext\n"; sub encrypt_string{ # encrypts a full string of plaintext, given the plaintext and the key. # returns the encrypted string. my ($plaintext, $key) = @_; my $ciphertext; $key = $key x (length($plaintext) / length($key) + 1); for( my $i=0; $i