Skip to content

Commit

Permalink
password encrypt supported. close #22
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrs4s committed Aug 9, 2020
1 parent 97f7de8 commit 44b26b7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
22 changes: 12 additions & 10 deletions global/config.go
Expand Up @@ -6,16 +6,18 @@ import (
)

type JsonConfig struct {
Uin int64 `json:"uin"`
Password string `json:"password"`
EnableDB bool `json:"enable_db"`
AccessToken string `json:"access_token"`
ReLogin bool `json:"relogin"`
ReLoginDelay int `json:"relogin_delay"`
HttpConfig *GoCQHttpConfig `json:"http_config"`
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
Debug bool `json:"debug"`
Uin int64 `json:"uin"`
Password string `json:"password"`
EncryptPassword bool `json:"encrypt_password"`
PasswordEncrypted string `json:"password_encrypted"`
EnableDB bool `json:"enable_db"`
AccessToken string `json:"access_token"`
ReLogin bool `json:"relogin"`
ReLoginDelay int `json:"relogin_delay"`
HttpConfig *GoCQHttpConfig `json:"http_config"`
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
Debug bool `json:"debug"`
}

type CQHttpApiConfig struct {
Expand Down
48 changes: 47 additions & 1 deletion main.go
Expand Up @@ -3,8 +3,11 @@ package main
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
Expand Down Expand Up @@ -112,7 +115,7 @@ func main() {
time.Sleep(time.Second * 5)
return
}
if conf.Uin == 0 || conf.Password == "" {
if conf.Uin == 0 || (conf.Password == "" && conf.PasswordEncrypted == "") {
log.Warnf("请修改 config.json 以添加账号密码.")
time.Sleep(time.Second * 5)
return
Expand All @@ -132,6 +135,24 @@ func main() {
log.Fatalf("加载设备信息失败: %v", err)
}
}
if conf.EncryptPassword && conf.PasswordEncrypted == "" {
log.Infof("密码加密已启用, 请输入Key对密码进行加密: (Enter 提交)")
strKey, _ := console.ReadString('\n')
key := md5.Sum([]byte(strKey))
if encrypted := EncryptPwd(conf.Password, key[:]); encrypted != "" {
conf.Password = ""
conf.PasswordEncrypted = encrypted
_ = conf.Save("config.json")

This comment has been minimized.

Copy link
@wdvxdr1123

wdvxdr1123 Nov 17, 2020

Collaborator

找到了

} else {
log.Warnf("加密时出现问题.")
}
}
if conf.PasswordEncrypted != "" {
log.Infof("密码加密已启用, 请输入Key对密码进行解密以继续: (Enter 提交)")
strKey, _ := console.ReadString('\n')
key := md5.Sum([]byte(strKey))
conf.Password = DecryptPwd(conf.PasswordEncrypted, key[:])
}
log.Info("Bot将在5秒后登录并开始信息处理, 按 Ctrl+C 取消.")
time.Sleep(time.Second * 5)
log.Info("开始尝试登录并同步消息...")
Expand Down Expand Up @@ -210,3 +231,28 @@ func main() {
<-c
b.Release()
}

func EncryptPwd(pwd string, key []byte) string {
tea := binary.NewTeaCipher(key)
if tea == nil {
return ""
}
return base64.StdEncoding.EncodeToString(tea.Encrypt([]byte(pwd)))
}

func DecryptPwd(ePwd string, key []byte) string {
defer func() {
if pan := recover(); pan != nil {
log.Fatalf("密码解密失败: %v", pan)
}
}()
encrypted, err := base64.StdEncoding.DecodeString(ePwd)
if err != nil {
panic(err)
}
tea := binary.NewTeaCipher(key)
if tea == nil {
panic("密钥错误")
}
return string(tea.Decrypt(encrypted))
}

0 comments on commit 44b26b7

Please sign in to comment.