-
Notifications
You must be signed in to change notification settings - Fork 33
/
RSA_example.pl
executable file
·64 lines (51 loc) · 1.12 KB
/
RSA_example.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 10 January 2017
# https://github.com/trizen
# A simple example for the RSA algorithm.
use 5.010;
use strict;
use warnings;
use ntheory qw(random_strong_prime);
my $p = random_strong_prime(2048);
my $q = random_strong_prime(2048);
my $n = ($p * $q);
my $phi = ($p - 1) * ($q - 1);
sub gcd($$) {
my ($u, $v) = @_;
while ($v) {
($u, $v) = ($v, $u % $v);
}
return abs($u);
}
my $e = 0;
for (my $k = 16 ; gcd($e, $phi) != 1 ; ++$k) {
$e = 2**$k + 1;
}
sub invmod($$) {
my ($a, $n) = @_;
my ($t, $nt, $r, $nr) = (0, 1, $n, $a % $n);
while ($nr != 0) {
my $quot = int(($r - ($r % $nr)) / $nr);
($nt, $t) = ($t - $quot * $nt, $nt);
($nr, $r) = ($r - $quot * $nr, $nr);
}
return if $r > 1;
$t += $n if $t < 0;
return $t;
}
my $d = invmod($e, $phi);
sub expmod($$$) {
my ($a, $b, $n) = @_;
my $c = 1;
do {
($c *= $a) %= $n if $b & 1;
($a *= $a) %= $n;
} while ($b >>= 1);
return $c;
}
my $m = 1234;
my $c = expmod($m, $e, $n);
my $M = expmod($c, $d, $n);
say $M;