main.go
3.4 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
package main
import (
"bufio"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
socialwork_sdk "gitlab.workai.com.cn/chenang/socialwork-sdk"
"io/ioutil"
"os"
)
/*func init(){
var bits int
flag.IntVar(&bits, "b", 1024, "密钥长度,默认为1024位")
flag.Parse()
if err := GenRsaKey(bits); err != nil {
log.Fatal("密钥文件生成失败!")
}
log.Println("密钥文件生成成功!")
}
*/
func main() {
var key, iv, privateKeyPath, publicKeyPath, ip, bussinessCode, ptCode, reqBody, errFilePath, resultFilePath string
flag.StringVar(&key, "key", "", "key,用于")
flag.StringVar(&iv, "iv", "", "iv,加密使用的偏移量")
flag.StringVar(&privateKeyPath, "privateKeyPath", "", "privateKeyPath,私钥文件所在位置(.pem文件)")
flag.StringVar(&publicKeyPath, "publicKeyPath", "", "publicKeyPath,平台公钥文件所在位置(.pem文件)")
flag.StringVar(&ip, "ip", "", "ip,访问地址,如:http://47.110.250.177:20000/")
flag.StringVar(&bussinessCode, "bussiness_code", "", "bussiness_code,访问接口的bussiness_code参数")
flag.StringVar(&ptCode, "ptCode", "", "ptCode,访问接口的ptcode参数")
flag.StringVar(&reqBody, "reqBody", "", "reqBody,需要加密的数据(json格式)")
flag.StringVar(&errFilePath, "errFilePath", "", "errFilePath,错误信息写入的文件地址")
flag.StringVar(&resultFilePath, "resultFilePath", "", "resultFilePath,返回资源写入的文件地址")
flag.Parse()
if key == "" || iv == "" || privateKeyPath == "" || publicKeyPath == "" || ip == "" || bussinessCode == "" || ptCode == "" || reqBody == "" || resultFilePath == "" || errFilePath == "" {
os.Exit(1)
}
fd, _ := os.OpenFile(errFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
defer fd.Close()
publicKey, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
}
privateKey, err := ioutil.ReadFile(privateKeyPath)
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
}
client := socialwork_sdk.NewOrderClient(key, iv, string(privateKey), string(publicKey), ip, ptCode)
req, err := client.SendRequest(bussinessCode, reqBody)
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
} else {
file, err := os.Create(resultFilePath)
defer file.Close()
if err != nil {
write := bufio.NewWriter(fd)
write.WriteString("命令行执行错误:" + err.Error() + "\r\n")
write.Flush()
return
}
file.WriteString(req)
return
}
}
func GenRsaKey(bits int) error {
// 生成私钥文件
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "私钥",
Bytes: derStream,
}
file, err := os.Create("private.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
// 生成公钥文件
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return err
}
block = &pem.Block{
Type: "公钥",
Bytes: derPkix,
}
file, err = os.Create("public.pem")
if err != nil {
return err
}
err = pem.Encode(file, block)
if err != nil {
return err
}
return nil
}