-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElGamal.go
92 lines (78 loc) · 1.76 KB
/
ElGamal.go
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
package routers
import (
"encoding/json"
"math"
"net/http"
"strconv"
"github.com/JavDomGom/gcat/models"
"github.com/JavDomGom/gcat/resources"
)
func ElGamal(w http.ResponseWriter, r *http.Request) {
// Bob's public number.
p, err := strconv.ParseInt(r.URL.Query().Get("p"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
// Bob's public number.
a, err := strconv.ParseInt(r.URL.Query().Get("a"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
// Bob's private key.
priK, err := strconv.ParseInt(r.URL.Query().Get("pri_k"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
// Bob's public key.
pubK := resources.ModExp(a, priK, p)
// Secret number.
N, err := strconv.ParseInt(r.URL.Query().Get("n"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
// Alice's random session number.
v, err := strconv.ParseInt(r.URL.Query().Get("v"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
// Alice's first part of pair (N1, N2).
N1 := resources.ModExp(a, v, p)
// Alice's second part of pair (N1, N2).
N2 := int64(
math.Mod(
math.Mod(
math.Mod(float64(N), float64(p))*float64(resources.ModExp(pubK, v, p)),
float64(p),
),
float64(p),
),
)
// Alice send to Bob the pair (N1, N2), and Bob calculates:
N3 := resources.ModExp(N1, priK, p)
NDecrypted := int64(
math.Mod(
float64(N2*resources.ModInv(N3, p)),
float64(p),
),
)
var elgamal = models.ElGamal{
Bob_p: p,
Bob_a: a,
Bob_PriK: priK,
Bob_PubK: pubK,
SecretNumber: N,
Alice_v: v,
Alice_N1: N1,
Alice_N2: N2,
Bob_N3: N3,
N_decrypted: NDecrypted,
}
w.Header().Set("context-type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(elgamal)
}