From 97a420f9f30afe23a7b6253a47abf219c670662a Mon Sep 17 00:00:00 2001 From: Yuzuki616 Date: Sat, 15 Jul 2023 18:13:16 +0800 Subject: [PATCH] add encrypt private command --- api/panel/node.go | 2 +- cmd/x25519.go | 27 +++++++++++++++++++++++++-- common/crypt/aes.go | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/api/panel/node.go b/api/panel/node.go index 32c546b..7f1767f 100644 --- a/api/panel/node.go +++ b/api/panel/node.go @@ -145,7 +145,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { return nil, fmt.Errorf("decode v2ray extra error: %s", err) } if node.ExtraConfig.RealityConfig.PrivateKey != "" { - temp := crypt.GenShaHash([]byte(c.APIHost + c.Token)) + temp := crypt.GenShaHash([]byte(c.APIHost + c.Token))[:32] temp, err = crypt.AesDecrypt(node.ExtraConfig.RealityConfig.PrivateKey, []byte(temp)) node.ExtraConfig.RealityConfig.PrivateKey = temp } diff --git a/cmd/x25519.go b/cmd/x25519.go index 791a4b0..361d4a8 100644 --- a/cmd/x25519.go +++ b/cmd/x25519.go @@ -4,6 +4,9 @@ import ( "crypto/rand" "encoding/base64" "fmt" + "strings" + + "github.com/Yuzuki616/V2bX/common/crypt" "github.com/spf13/cobra" "golang.org/x/crypto/curve25519" @@ -22,6 +25,19 @@ func init() { } func executeX25519() { + var yes, key string + fmt.Println("要对私钥进行加密吗?(Y/n)") + fmt.Scan(&yes) + if strings.ToLower(yes) == "y" { + var temp string + fmt.Println("请输入Api接口地址:") + fmt.Scan(&temp) + key = temp + fmt.Println("请输入Api认证Token:") + fmt.Scan(&temp) + key += temp + key = crypt.GenShaHash([]byte(key)) + } var output string var err error defer func() { @@ -45,9 +61,16 @@ func executeX25519() { output = Err("gen X25519 error: ", err) return } - + p := base64.RawURLEncoding.EncodeToString(privateKey) output = fmt.Sprint("Private key: ", - base64.RawURLEncoding.EncodeToString(privateKey), + p, "\nPublic key: ", base64.RawURLEncoding.EncodeToString(publicKey)) + if strings.ToLower(yes) == "y" { + key, err = crypt.AesEncrypt([]byte(p), []byte(key[:32])) + if err != nil { + output = Err("encrypt private key error: ", err) + } + output += "\n加密后的Private key:" + key + } } diff --git a/common/crypt/aes.go b/common/crypt/aes.go index 8aae7f7..4ab5cea 100644 --- a/common/crypt/aes.go +++ b/common/crypt/aes.go @@ -10,7 +10,7 @@ func AesEncrypt(data []byte, key []byte) (string, error) { if err != nil { return "", err } - en := make([]byte, 0, len(data)) + en := make([]byte, len(data)) a.Encrypt(en, data) return base64.StdEncoding.EncodeToString(en), nil } @@ -24,7 +24,7 @@ func AesDecrypt(data string, key []byte) (string, error) { if err != nil { return "", err } - de := make([]byte, 0, len(data)) + de := make([]byte, len(data)) a.Decrypt(de, d) return string(de), nil }