#!/usr/bin/env perl -l use strict; use warnings; if (@ARGV != 4) { print "Usage: $0 text encrypt/decrypt(1|0) ", 'right_shift/left_shift(1|0) positions_to_shift'; print "(Note: when decrypting, '*_shift' and 'positions_to_shift' ", 'is what was used for the original encryption!)'; exit; } my @chars = map { "\Q@{[chr]}\E" } 32 .. 126; my ($input, $encrypt, $right, $pos) = @ARGV; $pos %= @chars; my $plain_chars = join '' => @chars; my $cipher_chars = $right ? join('' => @chars[$pos .. $#chars, 0 .. $pos - 1]) : join('' => @chars[$#chars - $pos + 1 .. $#chars, 0 .. $#chars - $pos]); print 'Transform: ', ($encrypt ? 'encrypt' : 'decrypt'), ' -- Shift: ', ($right ? 'right' : 'left'), ' -- Positions: ', $pos; if ($encrypt) { my $ciphertext; eval "(\$ciphertext = \$input) =~ y/$plain_chars/$cipher_chars/"; print "Plaintext: $input"; print "Ciphertext: $ciphertext"; } else { my $plaintext; eval "(\$plaintext = \$input) =~ y/$cipher_chars/$plain_chars/"; print "Ciphertext: $input"; print "Plaintext: $plaintext"; } #### $ pm_caesar_cipher.pl def 1 1 3 Transform: encrypt -- Shift: right -- Positions: 3 Plaintext: def Ciphertext: ghi #### $ pm_caesar_cipher.pl ghi 0 1 3 Transform: decrypt -- Shift: right -- Positions: 3 Ciphertext: ghi Plaintext: def #### $ pm_caesar_cipher.pl def 1 0 3 Transform: encrypt -- Shift: left -- Positions: 3 Plaintext: def Ciphertext: abc #### $ pm_caesar_cipher.pl abc 0 0 3 Transform: decrypt -- Shift: left -- Positions: 3 Ciphertext: abc Plaintext: def #### $ pm_caesar_cipher.pl 'The quick brown fox jumped over the lazy dog.' 1 1 145 Transform: encrypt -- Shift: right -- Positions: 50 Plaintext: The quick brown fox jumped over the lazy dog. Ciphertext: ';8RDH<6>R5EBJAR9BKR=H@C87RBI8ERG;8R?4MLR7B:` #### $ pm_caesar_cipher.pl "';8RDH<6>R5EBJAR9BKR=H@C87RBI8ERG;8R?4MLR7B:\`" 0 1 145 Transform: decrypt -- Shift: right -- Positions: 50 Ciphertext: ';8RDH<6>R5EBJAR9BKR=H@C87RBI8ERG;8R?4MLR7B:` Plaintext: The quick brown fox jumped over the lazy dog. #### $ pm_caesar_cipher.pl Usage: ./pm_caesar_cipher.pl text encrypt/decrypt(1|0) right_shift/left_shift(1|0) positions_to_shift (Note: when decrypting, '*_shift' and 'positions_to_shift' is what was used for the original encryption!)