-
Notifications
You must be signed in to change notification settings - Fork 4
/
rsa.f95
64 lines (56 loc) · 1.74 KB
/
rsa.f95
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
! https://en.wikipedia.org/wiki/RSA_%28cryptosystem%29
module rsaModule
use, intrinsic :: iso_fortran_env
use primes
implicit none
private
public :: rsaGenerateKeys, rsaEncrypt
contains
function modularMultiplicativeInverse(a0, n0) result(inv)
integer(int32), intent(in) :: a0, n0
integer(int32) :: inv
integer(int32) :: a, n, t, nt, r, nr, quot, tmp
a = a0
n = n0
t = 0_int8
nt = 1_int8
r = n
nr = mod(a, n)
if (n < 0_int32) then
n = -n
end if
if (a < 0_int32) then
a = n - mod(-a, n)
end if
do while (nr /= 0_int32)
quot = r / nr
tmp = nt; nt = t - quot * nt; t = tmp
tmp = nr; nr = r - quot * nr; r = tmp
end do
if (r > 1_int32) then
inv = -1
else if (t < 0_int32) then
inv = t + n
else
inv = t
end if
end function modularMultiplicativeInverse
function rsaGenerateKeys() result(keys)
integer(int32), dimension(3) :: keys
integer(int32) :: p, q, n, t, e, d
p = randomPrime(1_int32, 255_int32)
q = randomPrime(1_int32, 255_int32)
n = p * q
t = (p - 1) * (q - 1)
e = randomPrime(1_int32, t)
d = modularMultiplicativeInverse(e, t)
keys = [ n, e, d ]
end function rsaGenerateKeys
function rsaEncrypt(message, n, e)
character(len=*), intent(in) :: message
integer(int32), intent(in) :: n, e
character(len=len(message)) :: rsaEncrypt
integer(int8), dimension(:), allocatable :: bytes
rsaEncrypt = message
end function
end module rsaModule