Commit 3239132a authored by haoyanbin's avatar haoyanbin

aes

parent ab2296fa
package pool
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"strings"
)
//aes-cbc 密码 偏移量 base64 utf8
const certKey = "dbc88888dbc88888"
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func ZEROPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding) //用0去填充
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
//加密 aes-cbc-128
func AesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
//解密 aes-cbc-128
func AesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, nil
}
// 数据加密
func DataAesEncrypt(Data string) (string, error) {
data := []byte(Data)
xpass, err := AesEncrypt(data, []byte(certKey))
if err != nil {
return "", err
}
pass64 := base64.StdEncoding.EncodeToString(xpass)
return pass64, nil
}
// 数据解密
func DataAesDecrypt(Data string) (string, error) {
Data = strings.Replace(Data, " ", "", -1)
bytesPass, err := base64.StdEncoding.DecodeString(Data)
xpass, err := AesDecrypt(bytesPass, []byte(certKey))
if err != nil {
return "", err
}
return string(xpass[:]), nil
}
// 数据加密
func TestDataAesEncrypt(Data string) (int, string, error) {
data := []byte(Data)
xpass, err := TestAesEncrypt(data, []byte(certKey))
if err != nil {
return 0, "", err
}
pass64 := base64.StdEncoding.EncodeToString(xpass)
return 1, pass64, nil
}
//加密 aes-cbc-128
func TestAesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
origData = ZEROPadding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}
......@@ -306,6 +306,15 @@ func GetClientInfoByToken(token string) (*UserInfo, error) {
return nil, errors.New("用户数据有误")
}
source, err := DataAesDecrypt(tokenData[0])
if err != nil {
return nil, errors.New("用户数据有误")
}
if len(source) <= 0 {
return nil, errors.New("用户数据有误")
}
userData := new(UserInfo)
userData.Source = tokenData[0]
userData.Promoter = tokenData[1]
......@@ -341,6 +350,8 @@ func SaveMsg(msg *SendMsg) {
mqData.UserId = user.CustomerId
}
mqData.BusinessId, _ = DataAesDecrypt(mqData.BusinessId)
mqData.ProcedureType = msg.ProcedureType
mqData.StartTime = msg.SendTime
......
......@@ -133,16 +133,16 @@ func ws(w http.ResponseWriter, r *http.Request) {
closeMsg.Finish = "3"
closeMsg.BusinessId = userInfo.Source
closeMsg.CustomerId = userInfo.CustomerId
//pool.PublishData(closeMsg)
fmt.Println("用户关闭连接",client.Id)
pool.PublishData(closeMsg)
fmt.Println("用户关闭连接", client.Id)
}
if user.Promoter == "2" {
//closeMsg.Finish = "4"
//closeMsg.GroupId = userInfo.Source
//closeMsg.UserId = userInfo.CustomerId
//pool.PublishData(closeMsg)
fmt.Println("专家关闭连接",client.Id)
closeMsg.Finish = "4"
closeMsg.GroupId = userInfo.Source
closeMsg.UserId = userInfo.CustomerId
pool.PublishData(closeMsg)
fmt.Println("专家关闭连接", client.Id)
}
fmt.Printf("连接己经关闭%s", client.Id)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment