小程序可在 Go 上完成微信手机号登录、测评、预约和下单,不再反代 Java。 Co-authored-by: Cursor <cursoragent@cursor.com>
48 lines
1009 B
Go
48 lines
1009 B
Go
package wechat
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"testing"
|
|
)
|
|
|
|
func TestDecryptPhone(t *testing.T) {
|
|
key := make([]byte, 16)
|
|
iv := make([]byte, 16)
|
|
_, _ = rand.Read(key)
|
|
_, _ = rand.Read(iv)
|
|
plain := []byte(`{"phoneNumber":"13800138000","purePhoneNumber":"13800138000","watermark":{"appid":"wxapp"}}`)
|
|
padded := pkcs7Pad(plain, aes.BlockSize)
|
|
block, err := aes.NewCipher(key)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
enc := make([]byte, len(padded))
|
|
cipher.NewCBCEncrypter(block, iv).CryptBlocks(enc, padded)
|
|
|
|
phone, err := DecryptPhone(
|
|
base64.StdEncoding.EncodeToString(key),
|
|
base64.StdEncoding.EncodeToString(enc),
|
|
base64.StdEncoding.EncodeToString(iv),
|
|
"wxapp",
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if phone != "13800138000" {
|
|
t.Fatalf("got %s", phone)
|
|
}
|
|
}
|
|
|
|
func pkcs7Pad(b []byte, block int) []byte {
|
|
n := block - (len(b) % block)
|
|
out := make([]byte, len(b)+n)
|
|
copy(out, b)
|
|
for i := 0; i < n; i++ {
|
|
out[len(b)+i] = byte(n)
|
|
}
|
|
return out
|
|
}
|