- fix dingding crypt bug

This commit is contained in:
gazebo
2019-03-09 13:59:04 +08:00
parent e0d459c280
commit 0e62d0a16c
4 changed files with 18 additions and 5 deletions

View File

@@ -1,8 +1,10 @@
package dingdingapi
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"errors"
"fmt"
"sort"
@@ -81,7 +83,12 @@ func (a *API) calculateCallbackSign(data map[string]interface{}) (sign string) {
func (a *API) Encrypt(msg string) (encryptedMsg string, err error) {
aesKey := a.GetCallbackAESKey()
binResult, err := utils.AESCBCEncpryt([]byte(msg), aesKey, aesKey[:16])
buf := bytes.NewBuffer(nil)
buf.WriteString(utils.GetUUID()[:16])
binary.Write(buf, binary.BigEndian, int32(len(msg)))
buf.WriteString(msg)
buf.WriteString(a.corpID)
binResult, err := utils.AESCBCEncpryt(buf.Bytes(), aesKey, aesKey[:16])
encryptedMsg = base64.StdEncoding.EncodeToString(binResult)
return encryptedMsg, err
}
@@ -92,7 +99,11 @@ func (a *API) Decrypt(msg string) (decryptedMsg string, err error) {
aesKey := a.GetCallbackAESKey()
binResult, err2 := utils.AESCBCDecpryt(binMsg, aesKey, aesKey[:16])
if err = err2; err == nil {
decryptedMsg = string(binResult)
var msgLen int32
if err = binary.Read(bytes.NewBuffer(binResult[16:]), binary.BigEndian, &msgLen); err == nil {
baseapi.SugarLogger.Debug(msgLen)
decryptedMsg = string(binResult[16+4 : 16+4+msgLen])
}
}
}
return decryptedMsg, err