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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/usr/bin/env perl
# Author: Steven Wilson
# Date: 2019-07-01
# Week: 015
# Challenge: #2
#
# Write a script to implement Vigenère cipher. The script should be able
# encode and decode. Checkout wiki page for more information.
# https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
# Usage: $ perl ch-2.pl OPERATION KEYWORD MESSAGE
# $ perl ch-2.pl -e LEMONS ATTACKATDAWN # encrypt a message
# $ perl ch-2.pl -d LEMONS LXFOPCLXPOJF # decrypt a message
use strict;
use warnings;
use feature qw / say /;
use Test::More;
my ( $operation, $keyword, $in_text ) = ( $ARGV[0], $ARGV[1], $ARGV[2] );
my $out_text;
my %operations = (
'-e' => 'encrypted',
'-d' => 'decrypted',
);
ok( encode( "ATTACKATDAWN", "LEMON" ) eq "LXFOPVEFRNHR", "encode working" );
ok( decode( "LXFOPVEFRNHR", "LEMON" ) eq "ATTACKATDAWN", "decode working" );
done_testing();
if ( $operation eq "-e" ) {
$out_text = encode( $in_text, $keyword );
}
elsif ( $operation eq "-d" ) {
$out_text = decode( $in_text, $keyword );
}
else {
die "$operation is not a valid operation";
}
say "Message $operations{$operation} to '$out_text'";
sub encode {
my ( $plaintext, $keyword ) = @_;
my $cyphertext;
for my $i ( 0 .. ( ( length $plaintext ) - 1 ) ) {
my $mi = ord( substr( $plaintext, $i, 1 ) ) - 65;
my $ki = ord( substr( $keyword, ( $i % length $keyword ), 1 ) ) - 65;
$cyphertext .= chr( ( ( $mi + $ki ) % 26 ) + 65 );
}
return $cyphertext;
}
sub decode {
my ( $cyphertext, $keyword ) = @_;
my $plaintext;
for my $i ( 0 .. ( ( length $cyphertext ) - 1 ) ) {
my $ci = ord( substr( $cyphertext, $i, 1 ) ) - 65;
my $ki = ord( substr( $keyword, ( $i % length $keyword ), 1 ) ) - 65;
$plaintext .= chr( ( ( $ci - $ki ) % 26 ) + 65 );
}
return $plaintext;
}
|