mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-10 07:42:26 +08:00
小程序数据解密,增加手机号解密,兼容之前的用户信息解密
This commit is contained in:
@@ -47,27 +47,6 @@ type PhoneInfo struct {
|
|||||||
} `json:"watermark"`
|
} `json:"watermark"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// pkcs7Unpad returns slice of the original data without padding
|
|
||||||
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
|
||||||
if blockSize <= 0 {
|
|
||||||
return nil, ErrInvalidBlockSize
|
|
||||||
}
|
|
||||||
if len(data)%blockSize != 0 || len(data) == 0 {
|
|
||||||
return nil, ErrInvalidPKCS7Data
|
|
||||||
}
|
|
||||||
c := data[len(data)-1]
|
|
||||||
n := int(c)
|
|
||||||
if n == 0 || n > len(data) {
|
|
||||||
return nil, ErrInvalidPKCS7Padding
|
|
||||||
}
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
if data[len(data)-n+i] != c {
|
|
||||||
return nil, ErrInvalidPKCS7Padding
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return data[:len(data)-n], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// get cipherText
|
// get cipherText
|
||||||
func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
|
func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
|
||||||
aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
|
aesKey, err := base64.StdEncoding.DecodeString(sessionKey)
|
||||||
@@ -95,7 +74,28 @@ func getCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
|
|||||||
return cipherText, nil
|
return cipherText, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrypt 解密数据(用户)
|
// pkcs7Unpad returns slice of the original data without padding
|
||||||
|
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
|
||||||
|
if blockSize <= 0 {
|
||||||
|
return nil, ErrInvalidBlockSize
|
||||||
|
}
|
||||||
|
if len(data)%blockSize != 0 || len(data) == 0 {
|
||||||
|
return nil, ErrInvalidPKCS7Data
|
||||||
|
}
|
||||||
|
c := data[len(data)-1]
|
||||||
|
n := int(c)
|
||||||
|
if n == 0 || n > len(data) {
|
||||||
|
return nil, ErrInvalidPKCS7Padding
|
||||||
|
}
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
if data[len(data)-n+i] != c {
|
||||||
|
return nil, ErrInvalidPKCS7Padding
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data[:len(data)-n], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt 解密数据
|
||||||
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
|
func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo, error) {
|
||||||
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,7 +113,7 @@ func (wxa *MiniProgram) Decrypt(sessionKey, encryptedData, iv string) (*UserInfo
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DecryptPhone 解密数据(手机)
|
// DecryptPhone 解密数据(手机)
|
||||||
func (wxa *MiniProgram) DecryptPHone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) {
|
func (wxa *MiniProgram) DecryptPhone(sessionKey, encryptedData, iv string) (*PhoneInfo, error) {
|
||||||
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
cipherText, err := getCipherText(sessionKey, encryptedData, iv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
Reference in New Issue
Block a user