-
Notifications
You must be signed in to change notification settings - Fork 0
/
Security_RSAKeys.go
93 lines (75 loc) · 1.96 KB
/
Security_RSAKeys.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
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
/*
本程序旨在演示如何生成,保存,加载RSA key
*/
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/gob"
"encoding/pem"
"fmt"
"os"
)
func main() {
// 生成RSA key,并保存文件
generateKey()
fmt.Println("============================")
loadKey()
}
func generateKey() {
reader := rand.Reader
bitSize := 512
key, err := rsa.GenerateKey(reader, bitSize)
checkError(err)
fmt.Println("Private key primes ", key.Primes[0].String(), key.Primes[1].String())
fmt.Println("Private key exponent ", key.D.String())
publicKey := key.PublicKey
fmt.Println("Public key modules ", publicKey.N.String())
fmt.Println("public key exponent ", publicKey.E)
saveGobKey("private.key", key)
saveGobKey("public.key", publicKey)
savePEMKey("private.pem", key)
}
func saveGobKey(fileName string, key interface{}) {
// 打开文件
outFile, err := os.Create(fileName)
checkError(err)
// 写入文件
encoder := gob.NewEncoder(outFile)
err = encoder.Encode(key)
checkError(err)
// 关闭文件
outFile.Close()
}
func savePEMKey(fileName string, key *rsa.PrivateKey) {
outFile, err := os.Create(fileName)
checkError(err)
var privateKey = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}
pem.Encode(outFile, privateKey)
outFile.Close()
}
func loadKey() {
var key rsa.PrivateKey
load("private.key", &key)
fmt.Println("Private key primes ", key.Primes[0].String(), key.Primes[1].String())
fmt.Println("Private key exponent ", key.D.String())
var publicKey rsa.PublicKey
load("public.key", &publicKey)
fmt.Println("Private key modules ", publicKey.N.String())
fmt.Println("Private key exponent ", publicKey.E)
}
func load(fileName string, key interface{}) {
inFile, err := os.Open(fileName)
checkError(err)
decoder := gob.NewDecoder(inFile)
err = decoder.Decode(key)
checkError(err)
inFile.Close()
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}