add user and rsa

master
修改密码漏洞修复完成 8 months ago
parent 0080cc0683
commit f5f618ac09

@ -5,9 +5,9 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9f8c3ce4-c378-4191-b7a4-3d5acbc0ebbc" name="Changes" comment="rrddf"> <list default="true" id="9f8c3ce4-c378-4191-b7a4-3d5acbc0ebbc" name="Changes" comment="rrddf">
<change afterPath="$PROJECT_DIR$/models/User.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/models/Order.go" beforeDir="false" afterPath="$PROJECT_DIR$/models/Order.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/models/User.go" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/mydb/initialize.go" beforeDir="false" afterPath="$PROJECT_DIR$/mydb/initialize.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/mydb/initialize.go" beforeDir="false" afterPath="$PROJECT_DIR$/mydb/initialize.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -69,6 +69,14 @@
</key> </key>
</component> </component>
<component name="RunManager"> <component name="RunManager">
<configuration default="true" type="GoApplicationRunConfiguration" factoryName="Go Application">
<module name="go_mqtt" />
<working_directory value="$PROJECT_DIR$" />
<kind value="FILE" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<method v="2" />
</configuration>
<configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true"> <configuration name="go build main.go" type="GoApplicationRunConfiguration" factoryName="Go Application" temporary="true" nameIsGenerated="true">
<module name="go_mqtt" /> <module name="go_mqtt" />
<working_directory value="$PROJECT_DIR$" /> <working_directory value="$PROJECT_DIR$" />
@ -78,6 +86,15 @@
<filePath value="$PROJECT_DIR$/main.go" /> <filePath value="$PROJECT_DIR$/main.go" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration default="true" type="GoTestRunConfiguration" factoryName="Go Test">
<module name="go_mqtt" />
<working_directory value="$PROJECT_DIR$" />
<kind value="DIRECTORY" />
<directory value="$PROJECT_DIR$" />
<filePath value="$PROJECT_DIR$" />
<framework value="gotest" />
<method v="2" />
</configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Go Build.go build main.go" /> <item itemvalue="Go Build.go build main.go" />

@ -1,7 +1,14 @@
package main package main
import ( import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"database/sql" "database/sql"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt" "fmt"
mqtt "github.com/eclipse/paho.mqtt.golang" mqtt "github.com/eclipse/paho.mqtt.golang"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
@ -10,7 +17,9 @@ import (
_ "go_mqtt/models" _ "go_mqtt/models"
"go_mqtt/mydb" "go_mqtt/mydb"
_ "go_mqtt/mydb" _ "go_mqtt/mydb"
"io/ioutil"
"log" "log"
"os"
"strings" "strings"
"time" "time"
) )
@ -177,6 +186,207 @@ func dbTest() {
stmt.Exec(1) stmt.Exec(1)
} }
// GenRsaKey generates an PKCS#1 RSA keypair of the given bit size in PEM format.
func GenRsaKey(bits int) (prvkey, pubkey []byte, err error) {
// Generates private key.
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
derStream := x509.MarshalPKCS1PrivateKey(privateKey)
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: derStream,
}
prvkey = pem.EncodeToMemory(block)
// Generates public key from private key.
publicKey := &privateKey.PublicKey
derPkix, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return
}
block = &pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: derPkix,
}
pubkey = pem.EncodeToMemory(block)
return
}
func parsePrivateKey(privateKeyFile string) (*rsa.PrivateKey, error) {
// 读取私钥文件
keyData, err := ioutil.ReadFile(privateKeyFile)
if err != nil {
return nil, err
}
// 解析PEM块
block, _ := pem.Decode(keyData)
if block == nil {
return nil, fmt.Errorf("private key error not block in file")
}
// 解析RSA私钥
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return privateKey, nil
}
//生成RSA私钥和公钥保存到文件中
func GenerateRSAKey(bits int) {
//GenerateKey函数使用随机数据生成器random生成一对具有指定字位数的RSA密钥
//Reader是一个全局、共享的密码用强随机数生成器
privateKey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return
}
//保存私钥
//通过x509标准将得到的ras私钥序列化为ASN.1 的 DER编码字符串
// X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) // PKCS1 和 9 是不一致的
X509PrivateKey, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
//使用pem格式对x509输出的内容进行编码
//创建文件保存私钥
privateFile, err := os.Create("private.pem")
if err != nil {
return
}
//构建一个pem.Block结构体对象
privateBlock := pem.Block{Type: "PRIVATE KEY", Bytes: X509PrivateKey}
//将数据保存到文件
pem.Encode(privateFile, &privateBlock)
//保存公钥
//获取公钥的数据
publicKey := privateKey.PublicKey
//X509对公钥编码
X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
if err != nil {
return
}
//pem格式编码
//创建用于保存公钥的文件
publicFile, err := os.Create("public.pem")
if err != nil {
return
}
//创建一个pem.Block结构体对象
publicBlock := pem.Block{Type: "Public Key", Bytes: X509PublicKey}
//保存到文件
pem.Encode(publicFile, &publicBlock)
}
// RSA_Decrypts RSA解密支持分段解密
func RSA_Decrypts(cipherText []byte, path string) []byte {
//打开文件
var bytesDecrypt []byte
//file, err := os.Open(path)
//if err != nil {
// fmt.Println(err)
//}
//
////获取文件内容
//info, _ := file.Stat()
//buf := make([]byte, info.Size())
//file.Read(buf)
// 读取私钥文件
keyData, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
//pem解码
block, _ := pem.Decode(keyData)
//X509解码
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
fmt.Println(err.Error())
os.Exit(0)
}
p := privateKey.(*rsa.PrivateKey)
keySize := p.Size()
srcSize := len(cipherText)
log.Println("密钥长度", keySize, "密文长度", srcSize)
var offSet = 0
var buffer = bytes.Buffer{}
for offSet < srcSize {
endIndex := offSet + keySize
if endIndex > srcSize {
endIndex = srcSize
}
bytesOnce, err := rsa.DecryptPKCS1v15(rand.Reader, p, cipherText[offSet:endIndex])
if err != nil {
return nil
}
buffer.Write(bytesOnce)
offSet = endIndex
}
bytesDecrypt = buffer.Bytes()
return bytesDecrypt
}
// RsaEncryptBlock 公钥加密-分段
func RsaEncryptBlock(src []byte, path string) (bytesEncrypt []byte, err error) {
//打开文件
//file, err := os.Open(path)
//if err != nil {
// return
//}
//
////读取文件的内容
//info, _ := file.Stat()
//buf := make([]byte, info.Size())
//file.Read(buf)
// 读取公钥文件
keyData, err := ioutil.ReadFile(path)
if err != nil {
return nil,err
}
//pem解码
block, _ := pem.Decode(keyData)
//x509解码
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
fmt.Println(err)
}
//类型断言
publicKey := publicKeyInterface.(*rsa.PublicKey)
keySize, srcSize := publicKey.Size(), len(src)
log.Println("密钥长度", keySize, "明文长度", srcSize)
offSet, once := 0, keySize-11
buffer := bytes.Buffer{}
for offSet < srcSize {
endIndex := offSet + once
if endIndex > srcSize {
endIndex = srcSize
}
// 加密一部分
bytesOnce, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src[offSet:endIndex])
if err != nil {
return nil, err
}
buffer.Write(bytesOnce)
offSet = endIndex
}
bytesEncrypt = buffer.Bytes()
return
}
func timing(client mqtt.Client) { func timing(client mqtt.Client) {
//定时器10秒钟执行一次 //定时器10秒钟执行一次
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(5 * time.Second)
@ -193,6 +403,27 @@ func timing(client mqtt.Client) {
// queryOneDataByMySql(db) // queryOneDataByMySql(db)
// //mydb.Close() // //mydb.Close()
//} //}
//GenerateRSAKey(2048)
//publicPath := "public.pem"
//privatePath := "private.pem"
//
//var a = []byte("jack")
//encrptTxt, err := RsaEncryptBlock(a, publicPath)
//if err != nil {
// fmt.Println(err.Error())
//}
//fmt.Println("加密后的字符串:")
//encodeString := base64.StdEncoding.EncodeToString(encrptTxt)
//fmt.Println(encodeString)
//fmt.Println("-------------------")
//decodeByte, err := base64.StdEncoding.DecodeString(encodeString)
//if err != nil {
// fmt.Println(err.Error())
//}
//decrptCode := RSA_Decrypts(decodeByte, privatePath)
//fmt.Println("解密后的字符串:")
//fmt.Println(string(decrptCode))
} }
} }
} }
@ -203,6 +434,53 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
message := fmt.Sprintf("%s", msg.Payload()) message := fmt.Sprintf("%s", msg.Payload())
topic := fmt.Sprintf("%s", msg.Topic()) topic := fmt.Sprintf("%s", msg.Topic())
fmt.Println(message) fmt.Println(message)
fmt.Println(topic)
privatePath := "privateKey.pem"
if topic == "app_push" {
fmt.Println("珠海电厂APP收到mqtt消息")
decodeByte, err := base64.StdEncoding.DecodeString(message)
if err != nil {
fmt.Println(err.Error())
}
decrptCode := RSA_Decrypts(decodeByte, privatePath)
fmt.Println("解密后的字符串:")
fmt.Println(string(decrptCode))
fmt.Println("-----user--------")
var user models.User
json.Unmarshal(decrptCode, &user)
fmt.Println(user)
//models.SaveUser(&user)
if models.GetUser(&user) {
models.UpdateUser(&user)
}else{
models.SaveUser(&user)
}
}else if topic == "app_push_dyw" {
fmt.Println("大亚湾电厂APP收到mqtt消息")
decodeByte, err := base64.StdEncoding.DecodeString(message)
if err != nil {
fmt.Println(err.Error())
}
decrptCode := RSA_Decrypts(decodeByte, privatePath)
fmt.Println("解密后的字符串:")
fmt.Println(string(decrptCode))
fmt.Println("-----user--------")
//var user models.User
//json.Unmarshal(decrptCode, &user)
//fmt.Println(user)
//
//if models.GetUser(&user) {
// models.UpdateUser(&user)
//}else{
// models.SaveUser(&user)
//}
}else{
result := strings.Split(message, " ") result := strings.Split(message, " ")
fmt.Println(result[0]) fmt.Println(result[0])
fmt.Println(result[1]) fmt.Println(result[1])
@ -216,7 +494,6 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
temperature.Humidity = result[0] temperature.Humidity = result[0]
temperature.Temperature = result[1] temperature.Temperature = result[1]
temperature.Topic = topic temperature.Topic = topic
if topic == "WifiSHT/7C87CE9CA4E6/SHT20" { if topic == "WifiSHT/7C87CE9CA4E6/SHT20" {
temperature.LocationDesc = "广东省珠海市高新区唐家湾镇东岸村水风三街28号501" temperature.LocationDesc = "广东省珠海市高新区唐家湾镇东岸村水风三街28号501"
} }
@ -227,14 +504,11 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
temperature.LocationDesc = "广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号" temperature.LocationDesc = "广西壮族自治区崇左市天等县天等镇荣华村弄在屯113号"
} }
fmt.Println("测温传感器地点:", temperature.LocationDesc) fmt.Println("测温传感器地点:", temperature.LocationDesc)
models.SaveTemperature(temperature)
}
//db, err := getMySqlDB()
//if err == nil {
// insertDataByMySql(db,temperature)
// //mydb.Close()
//}
models.SaveTemperature(temperature)
//uuid := uuid.New() //uuid := uuid.New()
//models.SaveProduct() //models.SaveProduct()
@ -267,6 +541,8 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) { var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
fmt.Println("Connected") fmt.Println("Connected")
subTemperature(client) subTemperature(client)
subAppPush(client)
subAppPushDyw(client)
} }
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) { var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
@ -287,6 +563,20 @@ func subTemperature(client mqtt.Client) {
fmt.Printf("Subscribed to topic: %s", topic) fmt.Printf("Subscribed to topic: %s", topic)
} }
func subAppPush(client mqtt.Client) {
topic := "app_push"
token := client.Subscribe(topic, 2, nil)
token.Wait()
fmt.Printf("Subscribed to topic: %s", topic)
}
func subAppPushDyw(client mqtt.Client) {
topic := "app_push_dyw"
token := client.Subscribe(topic, 2, nil)
token.Wait()
fmt.Printf("Subscribed to topic: %s", topic)
}
func publish(client mqtt.Client) { func publish(client mqtt.Client) {
num := 999999000000000000 num := 999999000000000000
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
@ -324,6 +614,8 @@ func main() {
//sub(client) //sub(client)
subTemperature(client) subTemperature(client)
subAppPush(client)
subAppPushDyw(client)
publish(client) publish(client)
//client.Disconnect(1000) //client.Disconnect(1000)

@ -0,0 +1,76 @@
package models
import (
"errors"
"fmt"
"go_mqtt/mydb"
"gorm.io/gorm"
)
type User struct {
Username string `gorm:"primaryKey;size:255"`
Password string `gorm:"size:255"`
Date string `gorm:"size:255"`
Imeinum string `gorm:"size:50"`
Sysversion string `gorm:"size:100"`
Phonemodel string `gorm:"size:255"`
Appversion string `gorm:"size:255"`
Name string `gorm:"size:255"`
}
func init() {
fmt.Println("User init()")
mydb.DB.AutoMigrate(&User{})
}
// TableName 会将 User 的表名重写为 `user`
func (User) TableName() string {
return "user"
}
func SaveUser(user *User) {
result := mydb.DB.Create(&user)
if result.Error != nil {
fmt.Println("Failed to create User:", result.Error)
} else {
fmt.Println("User created successfully!")
}
}
func UpdateUser(item *User) {
fmt.Println("用户存在,更新操作")
var user User
mydb.DB.First(&user, item.Username)
user.Password=item.Password
user.Date=item.Date
user.Imeinum=item.Imeinum
user.Sysversion=item.Sysversion
user.Phonemodel=item.Phonemodel
user.Appversion=item.Appversion
user.Name=item.Name
mydb.DB.Save(&user)
}
func GetUser(item *User) bool {
var user User
//mydb.DB.First(&user, item.Username)
result := mydb.DB.First(&user, item.Username).Error
fmt.Println("-----user---->>")
fmt.Println(user)
if errors.Is(result, gorm.ErrRecordNotFound) {
// 记录不存在
fmt.Println("用户不存在")
} else if result == nil {
// 记录存在
fmt.Println("用户存在")
return true
} else {
// 其他错误
fmt.Println("查询出错")
}
return false
}

@ -19,8 +19,8 @@ func initDatabase() {
//dsn := "root:Skyinno251,@tcp(192.168.2.18:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local" //dsn := "root:Skyinno251,@tcp(192.168.2.18:3306)/gorm?charset=utf8mb4&parseTime=True&loc=Local"
//dsn := "root:Skyinno251,@tcp(47.242.184.139:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local" //dsn := "root:Skyinno251,@tcp(47.242.184.139:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local"
//dsn := "root:Skyinno251,@tcp(localhost:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local" dsn := "root:Skyinno251,@tcp(localhost:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local"
dsn := "root:Skyinno251,@tcp(192.168.3.29:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local" //dsn := "root:Skyinno251,@tcp(192.168.3.29:3306)/appserver?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.New(mysql.Config{ db, err := gorm.Open(mysql.New(mysql.Config{
DSN: dsn, // DSN data source name DSN: dsn, // DSN data source name

@ -0,0 +1,3 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCYwvInDUICXbmWcOAR5hm86mz7WlKL3dDf12MSeIY2jm5QpUSSoMhrJOWLbOt5fXQepPaNO0M30A+3C4SFZrM/9WA5ehazO1u1m1LAkYzoSDRH3MMJsrJC3lCGD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