-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdecryptKeyPair.go
More file actions
52 lines (45 loc) · 1006 Bytes
/
decryptKeyPair.go
File metadata and controls
52 lines (45 loc) · 1006 Bytes
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
package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
)
//从磁盘读取私钥并解码
func decryptPEMKey(keyPath string) *rsa.PrivateKey {
//打开输入流
file, err := os.Open(keyPath)
checkError(err)
defer file.Close()
//读取文件描述符
info, err := file.Stat()
checkError(err)
//开辟空间
b := make([]byte, info.Size())
//读取密钥
file.Read(b)
//解码密钥
pemKey, _ := pem.Decode(b)
privateKey, err := x509.ParsePKCS1PrivateKey(pemKey.Bytes)
checkError(err)
return privateKey
}
//从磁盘读取公钥并解码
func decryptPublicPEMKey(keyPath string) *rsa.PublicKey {
//打开输入流
file, err := os.Open(keyPath)
checkError(err)
defer file.Close()
//读取文件描述符
info, err := file.Stat()
checkError(err)
//开辟空间
b := make([]byte, info.Size())
//读取密钥
file.Read(b)
//解码密钥
pemKey, _ := pem.Decode(b)
publicKey, err := x509.ParsePKCS1PublicKey(pemKey.Bytes)
checkError(err)
return publicKey
}