-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (108 loc) · 2.52 KB
/
main.go
File metadata and controls
131 lines (108 loc) · 2.52 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/base64"
"fmt"
"os"
"strconv"
"strings"
ecies "github.com/ecies/go/v2"
)
func encrypt(publicKey string, message string) {
publicKeyBytes, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
fatal(err)
}
messageBytes, err := base64.StdEncoding.DecodeString(message)
if err != nil {
messageNew, err := strconv.Unquote(`"` + message + `"`)
if err == nil {
message = messageNew
}
messageBytes = []byte(message)
}
pubKey, err := ecies.NewPublicKeyFromBytes(publicKeyBytes)
if err != nil {
fatal(err)
}
ciphertext, err := ecies.Encrypt(pubKey, messageBytes)
if err != nil {
fatal(err)
}
b64 := base64.StdEncoding.EncodeToString(ciphertext)
fmt.Println(b64)
}
func decrypt(privateKey string, message string) {
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKey)
if err != nil {
fatal(err)
}
messageBytes, err := base64.StdEncoding.DecodeString(message)
if err != nil {
fatal(err)
}
privkey := ecies.NewPrivateKeyFromBytes(privateKeyBytes)
plaintext, err := ecies.Decrypt(privkey, messageBytes)
if err != nil {
fatal(err)
}
fmt.Println(string(plaintext))
}
func privateKey() {
privateKey, err := ecies.GenerateKey()
if err != nil {
fatal(err)
}
privateKeyBytes := privateKey.Bytes()
b64 := base64.StdEncoding.EncodeToString(privateKeyBytes)
fmt.Println(b64)
}
func publicKey(privateKeyB64 string) {
privateKeyBytes, err := base64.StdEncoding.DecodeString(privateKeyB64)
if err != nil {
fatal(err)
}
privkey := ecies.NewPrivateKeyFromBytes(privateKeyBytes)
publicKeyBytes := privkey.PublicKey.Bytes(false)
b64 := base64.StdEncoding.EncodeToString(publicKeyBytes)
fmt.Println(b64)
}
func usage() {
builder := strings.Builder{}
builder.WriteString(" privatekey\n")
builder.WriteString(" publickey PRIVATE_KEY\n")
builder.WriteString(" decrypt PRIVATE_KEY MESSAGE\n")
builder.WriteString(" decrypt PRIVATE_KEY MESSAGE\n")
fmt.Printf("Command Line Options\nUsage: %s [\n%s]", os.Args[0], builder.String())
os.Exit(0)
}
func fatal(err error) {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
func main() {
if len(os.Args) <= 1 {
usage()
}
switch os.Args[1] {
case "privatekey":
if len(os.Args) != 2 {
usage()
}
privateKey()
case "publickey":
if len(os.Args) != 3 {
usage()
}
publicKey(os.Args[2])
case "encrypt":
if len(os.Args) != 4 {
usage()
}
encrypt(os.Args[2], os.Args[3])
case "decrypt":
if len(os.Args) != 4 {
usage()
}
decrypt(os.Args[2], os.Args[3])
}
}