-
Notifications
You must be signed in to change notification settings - Fork 22
/
encryption.go
42 lines (32 loc) · 861 Bytes
/
encryption.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
package elgamal
import (
"errors"
"crypto/rand"
"github.com/deatil/go-cryptobin/elgamal"
)
// 公钥加密
func (this EIGamal) Encrypt() EIGamal {
if this.publicKey == nil {
err := errors.New("EIGamal: publicKey error.")
return this.AppendError(err)
}
paredData, err := elgamal.EncryptAsn1(rand.Reader, this.publicKey, this.data)
if err != nil {
return this.AppendError(err)
}
this.paredData = paredData
return this
}
// 私钥解密
func (this EIGamal) Decrypt() EIGamal {
if this.privateKey == nil {
err := errors.New("EIGamal: privateKey error.")
return this.AppendError(err)
}
paredData, err := elgamal.DecryptAsn1(this.privateKey, this.data)
if err != nil {
return this.AppendError(err)
}
this.paredData = paredData
return this
}