#!/usr/bin/perl
#
# 3rot13 - A heavyweight encryption algorithm: Triple ROT13. Perl.
#
# This implements a new encryption algorithm, 3ROT13. This is a symmetric
#  cypher that has a number of desirable features over existing algorithms;
#  Firstly it is stateless - this allows recovery of any random substring 
#  from the cyphertext, which is especially useful if portions of the 
#  cyphertext are lost or damaged. 3ROT13 is also keyless, removing the 
#  need to worry about key storage or key lengths. Finally ROT13 is fast. 
#  Should a higher performance be desired this code can be reworked in C.
#
# 08-Nov-2004	ver 1.00
#
# USAGE: 3rot13 -d | -e 
#	3rot13 -e < infile > outfile	# encrypt
#	3rot13 -d < infile > outfile	# decrypt
#
# WARNING: Check encryption legislation in your country before use.
#
# SEE ALSO: An RFC has been written for 3ROT13, which has yet to be numbered.
#
# COPYRIGHT: Copyright (c) 2004 Brendan Gregg.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#  (http://www.gnu.org/copyleft/gpl.html)
#
# 08-Nov-2004	Brendan Gregg	Created this.


#
#  Process Command Line Arguments
#
$direction = $ARGV[0];
&usage() unless $direction =~ /^-[de]$/;

#
#  Encryption
#
if ($direction eq "-e") {
	while ($plaintext = <STDIN>) {
		$cyphertext = $plaintext;
		$cyphertext =~ tr/a-zA-Z/n-za-mN-ZA-M/;	# ROT13 1
		$cyphertext =~ tr/a-zA-Z/n-za-mN-ZA-M/;	# ROT13 2
		$cyphertext =~ tr/a-zA-Z/n-za-mN-ZA-M/;	# ROT13 3
		print $cyphertext;
	}
	exit(0);
}

#
#  Decryption
#
if ($direction eq "-d") {
	while ($cyphertext = <STDIN>) {
		$plaintext = $cyphertext;
		$plaintext =~ tr/a-zA-Z/n-za-mN-ZA-M/;	# ROT13 3
		$plaintext =~ tr/a-zA-Z/n-za-mN-ZA-M/;	# ROT13 2
		$plaintext =~ tr/a-zA-Z/n-za-mN-ZA-M/;	# ROT13 1
		print $plaintext;
	}
	exit(0);
}

#
#  Subroutines
#
sub usage {
	print STDERR "USAGE: 3rot13 -d | -e < infile > outfile\n";
	print STDERR "      3rot13 -e < in > out	# encrypt\n";
	print STDERR "      3rot13 -d < out > in	# decrypt\n";
	exit(1);
}
