forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ZoranPandovski#106 from nawglan/nawglan-chinese-ci…
…pher Nawglan chinese cipher
- Loading branch information
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |