From c26ea0fe415c89185cb92cbaf1123de7e72b9df5 Mon Sep 17 00:00:00 2001 From: ZhuoQinghui <1302344380@qq.com> Date: Fri, 25 Oct 2024 15:04:13 +0800 Subject: [PATCH] RSA --- src/daemon.go | 7 +++++++ src/util.go | 30 +++++++++++++++++------------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/daemon.go b/src/daemon.go index f329d29..e474f88 100644 --- a/src/daemon.go +++ b/src/daemon.go @@ -36,6 +36,8 @@ func Start() { dumpConfig() case "domains": showDomains() + case "pubkey": + showPubkey() case "apply": applyOnce() case "-s": @@ -190,6 +192,11 @@ func showDomains() { log.Println(string(config)) } +func showPubkey() { + key := GetAppConfig().Encrypt.PubKey + log.Println(key) +} + /* 守护进程接收名称 */ diff --git a/src/util.go b/src/util.go index fc9732c..9e62762 100644 --- a/src/util.go +++ b/src/util.go @@ -4,7 +4,7 @@ import ( "crypto/rand" "crypto/rsa" "crypto/x509" - "encoding/pem" + "encoding/base64" ) func GenRsa() (priKey string, pubKey string, err error) { @@ -13,23 +13,27 @@ func GenRsa() (priKey string, pubKey string, err error) { return "", "", err } publicKey := &privateKey.PublicKey - publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey) if err != nil { return "", "", err } - pemBlock := &pem.Block{ - Type: "PUBLIC KEY", - Bytes: publicKeyBytes, + pubKey = base64.StdEncoding.EncodeToString(publicKeyBytes) + //pemBlock := &pem.Block{ + // Type: "", + // Bytes: publicKeyBytes, + //} + //pubKey = string(pem.EncodeToMemory(pemBlock)) + //x509.MarshalPKCS8PrivateKey(privateKey) + privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey) + if err != nil { + return "", "", err } - pubKey = string(pem.EncodeToMemory(pemBlock)) - - privateKeyBytes := x509.MarshalPKCS1PrivateKey(privateKey) - pemBlock = &pem.Block{ - Type: "RSA PRIVATE KEY", - Bytes: privateKeyBytes, - } - priKey = string(pem.EncodeToMemory(pemBlock)) + priKey = base64.StdEncoding.EncodeToString(privateKeyBytes) + //pemBlock = &pem.Block{ + // Type: "", + // Bytes: privateKeyBytes, + //} + //priKey = string(pem.EncodeToMemory(pemBlock)) err = nil return }