add encrypt private command

This commit is contained in:
Yuzuki616 2023-07-15 18:13:16 +08:00
parent 15efffba0d
commit 97a420f9f3
3 changed files with 28 additions and 5 deletions

View File

@ -145,7 +145,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
return nil, fmt.Errorf("decode v2ray extra error: %s", err) return nil, fmt.Errorf("decode v2ray extra error: %s", err)
} }
if node.ExtraConfig.RealityConfig.PrivateKey != "" { 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)) temp, err = crypt.AesDecrypt(node.ExtraConfig.RealityConfig.PrivateKey, []byte(temp))
node.ExtraConfig.RealityConfig.PrivateKey = temp node.ExtraConfig.RealityConfig.PrivateKey = temp
} }

View File

@ -4,6 +4,9 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"strings"
"github.com/Yuzuki616/V2bX/common/crypt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
@ -22,6 +25,19 @@ func init() {
} }
func executeX25519() { 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 output string
var err error var err error
defer func() { defer func() {
@ -45,9 +61,16 @@ func executeX25519() {
output = Err("gen X25519 error: ", err) output = Err("gen X25519 error: ", err)
return return
} }
p := base64.RawURLEncoding.EncodeToString(privateKey)
output = fmt.Sprint("Private key: ", output = fmt.Sprint("Private key: ",
base64.RawURLEncoding.EncodeToString(privateKey), p,
"\nPublic key: ", "\nPublic key: ",
base64.RawURLEncoding.EncodeToString(publicKey)) 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
}
} }

View File

@ -10,7 +10,7 @@ func AesEncrypt(data []byte, key []byte) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
en := make([]byte, 0, len(data)) en := make([]byte, len(data))
a.Encrypt(en, data) a.Encrypt(en, data)
return base64.StdEncoding.EncodeToString(en), nil return base64.StdEncoding.EncodeToString(en), nil
} }
@ -24,7 +24,7 @@ func AesDecrypt(data string, key []byte) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
de := make([]byte, 0, len(data)) de := make([]byte, len(data))
a.Decrypt(de, d) a.Decrypt(de, d)
return string(de), nil return string(de), nil
} }