Created
March 9, 2011 05:21
-
-
Save dynax60/861734 to your computer and use it in GitHub Desktop.
Аффинная система подстановок Цезаря
This file contains hidden or 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 | |
# Аффинная система подстановок Цезаря | |
# --dynax60 | |
use strict; | |
use warnings; | |
use encoding 'utf8'; | |
use Encode qw(decode); | |
my ($cmd, $a, $b, $txt) = @ARGV; | |
&usage unless @ARGV == 4; | |
$a = int($a); | |
$b = int($b); | |
$txt = decode 'utf-8', $txt; | |
# Определяем алфавит | |
my @alph = ('A'..'Z', 'a'..'z', 0..9, | |
(map { chr } ord( 'А' ) .. ord( 'Я' ), ord('а') .. ord('я')), "Ё", "ё" | |
); | |
my $m = scalar(@alph); | |
my (%e, %d) = (); | |
my $action = { | |
'encrypt' => sub { return join '', map { $e{$_} } split //, shift }, | |
'decrypt' => sub { return join '', map { $d{$_} } split //, shift }, | |
}; | |
&usage unless exists $action->{$cmd}; | |
for (0..$m-1) { | |
my $idx = ($a*$_+$b) % $m; | |
$e{ $alph[$_] } = $alph[$idx]; | |
$d{ $alph[$idx] } = $alph[$_]; | |
} | |
print $action->{$cmd}->($txt), "\n"; | |
sub usage { die "Usage: $0 <encrypt|decrypt> A B text\n" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment