-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (56 loc) · 1.43 KB
/
main.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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
)
func generateKeyPair(bitSize int) (*rsa.PrivateKey, *rsa.PublicKey) {
// membuat private key
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
//menghasilkan public key dari private key
publicKey := &privateKey.PublicKey
return privateKey, publicKey
}
func savePEMKey(fileName string, key *rsa.PrivateKey) {
outFile, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating file:", err)
os.Exit(1)
}
defer outFile.Close()
// Menyimpan kunci privat dalam format PEM
var privateKey = &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(key),
}
pem.Encode(outFile, privateKey)
}
func savePublicPEMKey(fileName string, key *rsa.PublicKey) {
outFile, err := os.Create(fileName)
if err != nil {
fmt.Println("Error creating file:", err)
os.Exit(1)
}
defer outFile.Close()
// Menyimpan kunci publik dalam format PEM
var publicKey = &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: x509.MarshalPKCS1PublicKey(key),
}
pem.Encode(outFile, publicKey)
}
func main() {
bitSize := 2048
privateKey, publicKey := generateKeyPair(bitSize)
// Menyimpan kunci privat dan publik ke file
savePEMKey("private_key.pem", privateKey)
savePublicPEMKey("public_key.pem", publicKey)
fmt.Println("Kunci RSA berhasil dibuat dan disimpan.")
}