Skip to content

Commit

Permalink
Merge pull request ZoranPandovski#106 from nawglan/nawglan-chinese-ci…
Browse files Browse the repository at this point in the history
…pher

Nawglan chinese cipher
  • Loading branch information
ZoranPandovski authored Oct 8, 2017
2 parents 1802179 + cc571c1 commit b672bcc
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cryptography/chinese_cipher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from: https://en.wikipedia.org/wiki/Classical_cipher#Transposition_ciphers

In the Chinese cipher's method of transposing, the letters of the message are written from right to left, down and up columns to scramble the letters. Then, starting in the first row, the letters are taken in order to get the new ciphertext. For example, if the message needed to be enciphered was THE DOG RAN FAR, the Chinese cipher would look like this:

R R G T
A A O H
F N D E
The cipher text then reads: RRGT AAOH FNDE

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 b672bcc

Please sign in to comment.