Created
November 8, 2012 11:02
-
-
Save aterga/4038180 to your computer and use it in GitHub Desktop.
A simple unicode perl chipher decryption tool
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
#!/usr/bin/perl -w | |
use strict; | |
use warnings; | |
use utf8; | |
open FILE, "enc.txt" or die $!; | |
binmode FILE, ":utf8"; | |
my $text = 0; | |
foreach my $line (<FILE>) | |
{ | |
$text .= $line; | |
} | |
close FILE; | |
#print $text; | |
my %stat = (); | |
my $nletters = 0; | |
binmode STDOUT, ":utf8"; | |
foreach my $letter (map { chr } ( ord("А") .. ord("Я") )) | |
{ | |
$stat{$letter}++ while ($text =~ /$letter/g); | |
} | |
print ">>> The size of the input alphabet is ".keys(%stat)." symbols.\n"; | |
#foreach my $key (sort { $b <=> $a } keys %stat) | |
#{ | |
# print "\t$stat{$key} \t\t $key\n"; | |
#} | |
my @ru_letters = (map { chr } ( ord("А") .. ord("Я") )); | |
my %ru_letters_freq = ( | |
"О" => 52295949, | |
"Е" => 40392978, | |
"А" => 38081816, | |
"И" => 35075552, | |
"Н" => 31900994, | |
"Т" => 30084462, | |
"С" => 26058590, | |
"Р" => 22595850, | |
"В" => 21582499, | |
"Л" => 20678280, | |
"К" => 16599539, | |
"М" => 15252377, | |
"Д" => 14173134, | |
"П" => 13349597, | |
"У" => 12452612, | |
"Я" => 9528713, | |
"Ы" => 9036813, | |
"Ь" => 8263123, | |
"Г" => 8031521, | |
"З" => 7811723, | |
"Б" => 7579289, | |
"Ч" => 6904749, | |
"Й" => 5753983, | |
"Х" => 4597146, | |
"Ж" => 4476464, | |
"Ш" => 3420179, | |
"Ю" => 3044673, | |
"Ц" => 2314208, | |
"Щ" => 1719607, | |
"Э" => 1573696, | |
"Ф" => 1268926, | |
"Ъ" => 175908, | |
"Ё" => 63623 | |
); | |
my %ru_letter_rank = ( | |
#" " => 0, | |
"О" => 0, | |
"Е" => 1, | |
"А" => 2, | |
"И" => 3, | |
"Н" => 4, | |
"Т" => 5, | |
"С" => 6, | |
"Р" => 7, | |
"В" => 8, | |
"Л" => 9, | |
"К" => 10, | |
"М" => 11, | |
"Д" => 12, | |
"П" => 13, | |
"У" => 14, | |
"Я" => 15, | |
"Ы" => 16, | |
"Ь" => 17, | |
"Г" => 18, | |
"З" => 19, | |
"Б" => 20, | |
"Ч" => 21, | |
"Й" => 22, | |
"Х" => 23, | |
"Ж" => 24, | |
"Ш" => 25, | |
"Ю" => 26, | |
"Ц" => 27, | |
"Щ" => 28, | |
"Э" => 29, | |
"Ф" => 30, | |
"Ъ" => 31, | |
"Ё" => 32 | |
); | |
my %rev_ru_letter_rank = reverse %ru_letter_rank; | |
my @stat_rank = (); | |
foreach my $value (sort { $stat{$b} <=> $stat{$a} } keys %stat) | |
{ | |
print "$value => $stat{$value} ($ru_letter_rank{$value})\n"; | |
push(@stat_rank, $value); | |
#$sorted_stat{$value} = $stat{$value}; | |
} | |
my @offset = (); | |
for (my $i = 0; $i < $#stat_rank + 1; $i ++) | |
{ | |
print "$i: the truth is... $stat_rank[$i] is $rev_ru_letter_rank{$i} "; | |
$offset[$i] = ord($stat_rank[$i]) - ord($rev_ru_letter_rank{$i}); | |
print "(offset=$offset[$i])\n"; | |
} | |
my %mode = (); | |
foreach my $gap (@offset) | |
{ | |
$mode{$gap} ++; | |
} | |
my $cur_gap = 0; | |
my $cur_text; | |
foreach my $value (sort { $mode{$b} <=> $mode{$a} } keys %mode) | |
{ | |
#if ($mode{$value} != $cur_gap) | |
{ | |
$cur_gap = $mode{$value}; | |
print "$value => $mode{$value}\n"; | |
$cur_text = $text; | |
for (my $i = 0; $i < $#stat_rank + 1; $i ++) | |
{ | |
$cur_text =~ s/$ru_letters[$i]/$ru_letters[($i + $value) % 32]/g; | |
} | |
print "$cur_text\n\n"; | |
} | |
} | |
#use List::Util qw(sum); | |
#for (my $i = 0; $i < $#ru_letters + 1; $i ++) | |
#{ | |
# print "offset($ru_letters[$i], $ru_letters[$i+5])=".(ord($ru_letters[$i]) - ord($ru_letters[$i+5]))."\n"; | |
#} | |
#while ( (my $k,my $v) = each %mode ) | |
#{ | |
# print "$k => $v\n"; | |
#} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment