-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxos-rot.pl
executable file
·158 lines (126 loc) · 2.52 KB
/
nxos-rot.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/perl
use File::Basename;
use Getopt::Std;
# nxos substitution "cipher"
@nxos_rot = (3, 22, 4, 5, 18, 0, 21, 5, 18, 3, 10, 5, 16, 22, 4, 16, 24, 17, 12, 5, 21, 18, 5, 22, 19, 7);
my $script = basename($0);
getopts('d:e:');
# no arguments ? show usage, exit with error
if (!$opt_d && !$opt_e)
{
print "usage:\t$script -d <encrypted string>\n";
print "\t$script -e <plaintext string>\n";
exit 1;
}
# both options defined ? exit with error
if ($opt_d && $opt_e) { print "Please use either -d OR -e.\n"; exit 1; }
# decrypt case
if ($opt_d)
{
$plain = $opt_d;
nxos_decrypt($plain)
}
# encrypt case
if ($opt_e)
{
$plain = $opt_e;
nxos_encrypt($plain)
}
sub nxos_decrypt()
{
$plain = @_[0];
@input = split("", $plain);
$index = 0;
foreach $char(@input)
{
# non-letter - no change
if ($char !~ /[a-zA-Z]/)
{
$enc_char = $char;
push (@rot_complete, $enc_char);
$index++;
if ($index == 26) { $index = 0; };
next;
}
# uppercase
if ($char =~ /[A-Z]/)
{
$ascii = ord($char);
$ascii_rot = ($ascii - $nxos_rot[$index]);
if ($ascii_rot < 65)
{
$ascii_rot = ($ascii_rot + 26);
}
$enc_char = chr($ascii_rot);
push (@rot_complete, $enc_char);
$index++;
if ($index == 26) { $index = 0; };
next;
}
# lowercase
$ascii = ord($char);
$ascii_rot = ($ascii - $nxos_rot[$index]);
if ($ascii_rot < 97)
{
$ascii_rot = ($ascii_rot + 26);
}
$enc_char = chr($ascii_rot);
push (@rot_complete, $enc_char);
$index++;
if ($index == 26) { $index = 0; };
}
foreach $char (@rot_complete)
{
print $char;
}
print "\n";
}
sub nxos_encrypt()
{
$plain = @_[0];
@input = split("", $plain);
$index = 0;
foreach $char(@input)
{
# non-letter - no change
if ($char !~ /[a-zA-Z]/)
{
$enc_char = $char;
push (@rot_complete, $enc_char);
$index++;
if ($index == 26) { $index = 0; };
next;
}
# uppercase
if ($char =~ /[A-Z]/)
{
$ascii = ord($char);
$ascii_rot = ($ascii + $nxos_rot[$index]);
if ($ascii_rot > 90)
{
$ascii_rot = ($ascii_rot - 26);
}
$enc_char = chr($ascii_rot);
push (@rot_complete, $enc_char);
$index++;
if ($index == 26) { $index = 0; };
next;
}
# lowercase
$ascii = ord($char);
$ascii_rot = ($ascii + $nxos_rot[$index]);
if ($ascii_rot > 122)
{
$ascii_rot = ($ascii_rot - 26);
}
$enc_char = chr($ascii_rot);
push (@rot_complete, $enc_char);
$index++;
if ($index == 26) { $index = 0; };
}
foreach $char (@rot_complete)
{
print $char;
}
print "\n";
}