-
Notifications
You must be signed in to change notification settings - Fork 34
/
RSA_encryption.pl
executable file
·203 lines (154 loc) · 4.44 KB
/
RSA_encryption.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 09 January 2017
# https://github.com/trizen
# A general purpose implementation of the RSA encryption algorithm.
use utf8;
use 5.010;
use strict;
use autodie;
use warnings;
use Math::AnyNum qw(:overload gcd irand powmod invmod);
use Math::Prime::Util qw(random_strong_prime urandomm);
use Config qw(%Config);
use Getopt::Long qw(GetOptions);
use constant shortsize => $Config{shortsize};
my $bits = 2048;
my $decrypt = 0;
my $generate = 0;
my $sign = 0;
my $public = 'public.rsa';
my $private = 'private.rsa';
my $in_fh = \*STDIN;
my $out_fh = \*STDOUT;
sub usage {
print <<"EOT";
usage: $0 [options] [<input] [>output]
options:
-g --generate! : generate the private and public keys
-b --bits=i : size of the prime numbers in bits (default: $bits)
-d --decrypt! : decrypt mode (default: ${\($decrypt ? 'true' : 'false')})
-s --sign! : sign/unsign mode (default: ${\($sign ? 'true' : 'false')})
--public=s : public key file (default: $public)
--private=s : private key file (default: $private)
-i --input=s : input file (default: /dev/stdin)
-o --output=s : output file (default: /dev/stdout)
-h --help : prints this message
example:
perl $0 --generate
perl $0 < input.txt > enc.rsa
perl $0 -d < enc.rsa > decoded.txt
EOT
exit;
}
GetOptions(
'bits=i' => \$bits,
'decrypt!' => \$decrypt,
'generate!' => \$generate,
'public=s' => \$public,
'private=s' => \$private,
'input=s' => \$in_fh,
'sign!' => \$sign,
'output=s' => \$out_fh,
'help' => \&usage,
)
or die("Error in command line arguments\n");
if (!ref($in_fh)) {
open my $fh, '<', $in_fh;
$in_fh = $fh;
}
if (!ref($out_fh)) {
open my $fh, '>', $out_fh;
$out_fh = $fh;
}
if ($generate) {
say "** Generating <<$public>> and <<$private>> files...";
# Make sure we have enough bits
if ($bits < 128) {
$bits = 128;
}
# Make sure `bits` is a power of two
if ($bits & ($bits - 1)) {
$bits = 2 << (log($bits) / log(2));
}
my $p = Math::AnyNum->new(random_strong_prime($bits));
my $q = Math::AnyNum->new(random_strong_prime($bits));
my $n = $p * $q;
my $ϕ = ($p - 1) * ($q - 1);
# Choosing `e` (part of the public key)
#<<<
my $e;
do {
say "** Choosing e...";
$e = irand(65537, $n);
} until (
$e < $ϕ
and gcd($e, $ϕ ) == 1
and gcd($e - 1, $p - 1) == 2
and gcd($e - 1, $q - 1) == 2
);
#>>>
# Computing `d` (part of the private key)
my $d = invmod($e, $ϕ);
open my $public_fh, '>', $public;
print $public_fh "$bits $e $n";
close $public_fh;
open my $private_fh, '>', $private;
print $private_fh "$bits $d $n";
close $private_fh;
say "** Done!";
exit;
}
sub decrypt {
my ($bits, $d, $n) = map { Math::AnyNum->new($_) } do {
open my $fh, '<', $private;
split(' ', scalar <$fh>);
};
$bits >>= 2;
$bits += shortsize + shortsize;
while (1) {
my $len = read($in_fh, my ($message), $bits) || last;
my ($s1, $s2, $msg) = unpack('SSb*', $message);
my $c = Math::AnyNum->new(substr($msg, 0, $s1), 2);
my $M = powmod($c, $d, $n);
print $out_fh pack('b*', substr($M->as_bin, 1, $s2));
last if $len != $bits;
}
}
sub encrypt {
my ($bits, $e, $n) = map { Math::AnyNum->new($_) } do {
open my $fh, '<', $public;
split(' ', scalar <$fh>);
};
my $L = $bits << 1;
$bits >>= 2;
$bits -= 1;
while (1) {
my $len = read($in_fh, my ($message), $bits) || last;
my $B = '1' . unpack('b*', $message);
if ($bits != $len) {
$B .= join('', map { urandomm("2") } 1 .. ($L - ($len << 3) - 8));
}
my $m = Math::AnyNum->new($B, 2);
my $c = powmod($m, $e, $n);
my $bin = $c->as_bin;
print $out_fh pack("SSb$L", length($bin), $len << 3, $bin);
last if $len != $bits;
}
}
if ($sign) {
($private, $public) = ($public, $private);
}
if ($decrypt) {
if (not -e $private) {
die "File <<$private>> does not exists! (run --generate)\n";
}
decrypt();
}
else {
if (not -e $public) {
die "File <<$public>> does not exists! (run --generate)\n";
}
encrypt();
}