-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (55 loc) · 1.38 KB
/
main.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
// Package el_gamal
// Copyright 2023 Oleg Fomenko. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package el_gamal
import (
"crypto/elliptic"
"crypto/rand"
"math/big"
"github.com/olegfomenko/crypto/go/ec"
)
var Curve elliptic.Curve = ec.SECP256K1()
type PublicKey struct {
X, Y *big.Int
}
type PrivateKey struct {
*PublicKey
D *big.Int
}
type Cypher struct {
Ax, Ay *big.Int
Bx, By *big.Int
}
func GeneratePrivateKey() (*PrivateKey, error) {
d, err := rand.Int(rand.Reader, Curve.Params().N)
if err != nil {
return nil, err
}
x, y := Curve.ScalarBaseMult(d.Bytes())
return &PrivateKey{
PublicKey: &PublicKey{
X: x,
Y: y,
},
D: d,
}, nil
}
func Encrypt(mx, my *big.Int, pub *PublicKey) (*Cypher, error) {
k, err := rand.Int(rand.Reader, Curve.Params().N)
if err != nil {
return nil, err
}
ax, ay := Curve.ScalarBaseMult(k.Bytes())
cx, cy := Curve.ScalarMult(pub.X, pub.Y, k.Bytes())
bx, by := Curve.Add(mx, my, cx, cy)
return &Cypher{ax, ay, bx, by}, nil
}
func Decrypt(cypher *Cypher, prv *PrivateKey) (mx *big.Int, my *big.Int) {
x, y := Curve.ScalarMult(cypher.Ax, cypher.Ay, prv.D.Bytes())
mx, my = Curve.Add(cypher.Bx, cypher.By, x, minus(y))
return
}
func minus(val *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Mul(val, big.NewInt(-1)), Curve.Params().P)
}