-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_decode.pl
88 lines (61 loc) · 1.77 KB
/
encode_decode.pl
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/local/bin/perl
use strict;
use warnings;
#Choose starting parameter
#if you want to encrypt something chose encode
#if you want to decrypt chose decode
#example: perl encode_decode.pl encode
my $param = shift;
die "Please start the program with parameter 'encode' or 'decode'\n" unless($param);
chomp($param);
if($param eq 'encode' ){
print"Enter the message for encryption:\n";
my $input =<>;
chomp($input);
print"Choose the cipher number:\n";
my $offset=<>;
if($offset !~ /^-?\d+$/){
die "The cipher number must be integer!\n";
}
my $result = "";
print"This is the encrypted message: ";
my @array_input =split('',$input);
foreach(@array_input){
if((ord( $_ )< 65 || ord( $_ )>90 ) && (ord($_)<97 || ord($_)>122 ) ){
$result .=$_;
}
if(ord( $_ )>=65 && ord($_)<=90) {
$result .= chr((ord($_)+$offset-65)%26+65);
}
if(ord($_)>=97 && ord($_)<=122){
$result .= chr((ord($_)+$offset-97)%26+97);
}
}
print "$result\n";
}elsif($param eq 'decode'){
print"Enter the message for decryption:\n";
my $input =<>;
chomp($input);
print"Choose the cipher number:\n";
my $offset=<>;
if($offset !~ /^-?\d+$/){
die "The cipher number must be integer!\n";
}
my $result = "";
print"This is the encrypted message: ";
my @array_input =split('',$input);
foreach(@array_input){
if((ord( $_ )< 65 || ord( $_ )>90 ) && (ord($_)<97 || ord($_)>122 ) ){
$result .=$_;
}
if(ord( $_ )>=65 && ord($_)<=90) {
$result .= chr((ord($_)-$offset-65)%26+65);
}
if(ord($_)>=97 && ord($_)<=122){
$result .= chr((ord($_)-$offset-97)%26+97);
}
}
print "$result\n";
}elsif($param ne 'encode' or $param ne 'decode' or $param eq undef){
print"Please start the program with parameter 'encrypt' or 'decode'\n";
}