#!/usr/bin/perl
use strict;
use warnings;
# Techniques: closures, XOR cipher, run-length encoding, a tiny stack VM,
# sort/map/grep pipelines, and a dispatch table.
# --- Stage 1: the message is hidden as XOR-encrypted hex ------------------
my $key = 42;
my @cipher = map { $_ ^ $key } unpack("C*", "Hello, World!");
my $hex = join '', map { sprintf "%02x", $_ } @cipher;
# --- Stage 2: a closure-based decryptor ------------------------------------
sub make_decryptor {
my ($k) = @_;
return sub {
my ($h) = @_;
return join '', map { chr(hex($_) ^ $k) } ($h =~ /(..)/g);
};
}
# --- Stage 3: a tiny stack-based virtual machine ---------------------------
my %ops = (
PUSH => sub { my ($st, $arg) = @_; push @$st, $arg },
CONCAT => sub { my ($st) = @_; my $b = pop @$st; my $a = pop @$st; push @$st, $a . $b },
UPPER => sub { my ($st) = @_; push @$st, uc pop @$st },
EMIT => sub { my ($st) = @_; print pop(@$st), "\n" },
);
sub run_vm {
my (@program) = @_;
my @stack;
for my $instr (@program) {
my ($op, $arg) = @$instr;
$ops{$op}->(\@stack, $arg) or 1;
}
}
# --- Stage 4: run-length encoding as a sanity check ------------------------
sub rle {
my ($s) = @_;
(my $out = $s) =~ s/((.)\2*)/length($1) . $2/ge;
return $out;
}
# --- Putting it all together -----------------------------------------------
my $decrypt = make_decryptor($key);
my $message = $decrypt->($hex);
# Verify integrity: sorted unique characters must match expectations
my %seen;
my @unique = sort grep { !$seen{$_}++ } split //, $message;
die "Integrity check failed!\n" unless @unique == 10;
# Split into pieces and feed the VM
my ($greeting, $target) = split /,\s*/, $message;
run_vm(
[ PUSH => $greeting ],
[ PUSH => ", " ],
[ CONCAT => undef ],
[ PUSH => $target ],
[ CONCAT => undef ],
[ EMIT => undef ],
);
printf "(encrypted: %s | RLE: %s)\n", $hex, rle($message);
No comments:
Post a Comment
no thanks
Note: Only a member of this blog may post a comment.