#!/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"; }