#!/usr/bin/perl

use strict 'refs';
use warnings;
use Data::Dumper;

use POSIX;

#it's all about the special sauce
my @special_sauce;

while(my $line = <STDIN>) {
	unless($line =~ /decrypt_p\(\"/) {
		next;
	}

	#pull out the "key"
	my $array = $line;
	$array =~ s/^.*Array\(([^\)]*).*/$1/;
	@special_sauce = split(',', $array);


	#pull out the "encrypted text"
	$line =~ s/^.*decrypt_p\(\"([^\"\)]*).*/$1/;

	print &decode_lorenzo($line);
	exit;
}

sub decode_lorenzo {
	my $encoded_junk = shift or die "umm... gime something!";

	my $l = length($encoded_junk);
	my $my_length = $l;

	my $b = 1024;
	my $i = 0;
	my $j = 0;
	my $r = 0;
	my $p = 0;
	my $s = 0;
	my $w = 0;

	my $return_string = "";

	for($j = POSIX::ceil($l/$b); $j > 0; $j--) {
		$r = "";

		for($i = [sort(($l,$b))]->[0]; $i > 0 && $my_length > ($p + 1);  $i--, $l--) {

			$w |= $special_sauce[(unpack('U', substr($encoded_junk, $p++, 1)) - 48)] << $s;

			if($s) {
				$r .= pack('C', (165 ^ $w & 255));

				$w >>= 8;
				$s -= 2;
			} else {
				$s = 6;
			}
		}

		$return_string .= $r;
	}

	return $return_string;
}
