-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa.go
68 lines (53 loc) · 1.14 KB
/
rsa.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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"crypto/rand"
"log"
"os"
)
func GenRsaKey() error {
//生产私钥
privateKey , err := rsa.GenerateKey(rand.Reader , 32)
if err != nil {
return err
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block {
Type: "private key",
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: "public key",
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
}
func main() {
if err := GenRsaKey(); err != nil {
log.Fatal("密钥文件生成失败!")
}
log.Println("密钥文件生成成功!")
}