#!/usr/bin/perl
# Author:  gendou
# Usage:   elementcode.pl
# Version: 1.0.0
# Note:    This cipher is based on the periodic table of elements.

%ENCODE = (
	'a' => 13,  # Aluminum
	'b' => 5,   # Phosphorus
	'c' => 6,   # Carbon
	'd' => 66,  # Dysprosium
	'e' => 63,  # Europium
	'f' => 9,   # Fluorine
	'g' => 31,  # Gallium
	'h' => 1,   # Hydrogen
	'i' => 53,  # Iodine
	'j' => 0,   # [None]
	'k' => 19,  # Potassium
	'l' => 3,   # Lithium
	'm' => 12,  # Magnesium
	'n' => 7,   # Nitrogen
	'o' => 8,   # Oxygen
	'p' => 15,  # Phosphorus
	'q' => 114, # Ununquadium
	'r' => 37,  # Rubidium
	's' => 16,  # Sulfur
	't' => 22,  # Titanium
	'u' => 92,  # Uranium
	'v' => 23,  # Vanadium
	'w' => 74,  # Tungsten
	'x' => 54,  # Xenon
	'y' => 39,  # Yttrium
	'z' => 30   # Zinc
);

%DECODE = reverse %ENCODE;

print "Encode or Decode? ";
chomp($mode = <>);

while($line = <>)
{
	exit if($line eq "quit\n");

	if($mode =~ /^d(ecode)?/) # DO DECODE
	{
		foreach $string (split('-', $line))
		{
			foreach $key (keys %DECODE)
			{
				$string =~ s/(^|\D)$key($|\D)/$1$DECODE{$key}$2/;
			}

			print $string;
		}
	}
	else # DO ENCODE
	{
		foreach $letter (split('', lc $line))
		{
			print (exists $ENCODE{$letter} ? "$last$ENCODE{$letter}" : $letter);
			$last = (exists $ENCODE{$letter} ? '-' : '');
		}
	}
}

