Skip to content

Commit

Permalink
Create chinese_cipher.pl
Browse files Browse the repository at this point in the history
  • Loading branch information
nawglan authored Oct 8, 2017
1 parent 0f79d84 commit cc571c1
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions cryptography/chinese_cipher/perl/chinese_cipher.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/usr/bin/env perl

sub encrypt
{
my $data = shift;

my $encoded_data = '';

my $length = length $data;
my $sqrt = int(sqrt($length));
$sqrt++ unless (($sqrt * $sqrt) >= $length);
my @unencoded = split(//, $data);
my @message;
my $minside = $sqrt;
if (($sqrt * $sqrt) > $length) {
while (($minside * $sqrt) > $length) { $minside--;};
$minside++; # ensure square can hold the string
}

for (my $j = 0; $j < $sqrt; $j++) {
if ((($sqrt - $j)%2) == 0) {
for (my $i = $minside-1; $i >= 0; $i--) {
my $char = shift @unencoded;
$message[$i][$j] = $char;
}
} else {
for (my $i = 0; $i < $minside; $i++) {
my $char = shift @unencoded;
$message[$i][$j] = $char;
}
}
}

for (my $i = 0; $i < $minside; $i++) {
for (my $j = 0; $j < $sqrt; $j++) {
if (defined $message[$i][$j]) {
$encoded_data .= $message[$i][$j];
} else {
$encoded_data .= " ";
}
}
}

return $encoded_data;
}

sub decrypt
{
my $data = shift;

my @chars = split(//, $data);
my $length = length $data;
my $sqrt = int(sqrt($length));
if (($sqrt * $sqrt) < $length) {
$sqrt++;
}
my $minside = $sqrt;
if (($sqrt * $sqrt) > $length) {
while (($minside * $sqrt) > $length) { $minside--;};
$minside++; # ensure square can hold the string
}

my @message;
for (my $i = 0; $i < $minside; $i++) {
for (my $j = 0; $j < $sqrt; $j++) {
my $char = shift @chars;
$message[$i][$j] = $char;
}
}

my $decoded_data = '';
for (my $j = 0; $j < $sqrt; $j++) {
if ((($sqrt - $j)%2) == 0) {
for (my $i = $minside-1; $i >= 0; $i--) {
$decoded_data .= $message[$i][$j];
}
} else {
for (my $i = 0; $i < $minside; $i++) {
$decoded_data .= $message[$i][$j];
}
}
}

#trim decoded_data
$decoded_data =~ s/\s+$//;

return $decoded_data;
}

sub test {
my $text = shift;
my $encrypted = encrypt($text);
my $decrypted = decrypt($encrypted);
return $decrypted eq $text;
}

print "Got expected message\n" if test('This is a test');

0 comments on commit cc571c1

Please sign in to comment.