|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"crypto/rand"
|
|
|
|
|
"crypto/rsa"
|
|
|
|
|
"crypto/x509"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"encoding/pem"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
// 设备公共信息
|
|
|
|
|
deviceInfo := map[string]string{
|
|
|
|
|
"bootTimeInSec": "1595643553",
|
|
|
|
|
"countryCode": "CN",
|
|
|
|
|
"language": "zh-Hans-CN",
|
|
|
|
|
"deviceName": "e910dddb2748c36b47fcde5dd720eec1",
|
|
|
|
|
"systemVersion": "14.0",
|
|
|
|
|
"machine": "iPhone10,3",
|
|
|
|
|
"memory": "3955589120",
|
|
|
|
|
"disk": "63900340224",
|
|
|
|
|
"sysFileTime": "1595214620.383940",
|
|
|
|
|
"model": "D22AP",
|
|
|
|
|
"timeZone": "28800",
|
|
|
|
|
"deviceInitTime": "1632467920.301150749",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加密设备信息
|
|
|
|
|
encryptedDeviceByte, err := encrypt(deviceInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("加密错误 encrypt: %v\n", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("加密后的设备信息: encryptedDeviceByte: %s\n", encryptedDeviceByte)
|
|
|
|
|
|
|
|
|
|
//url := "https://caid.china-caa.org/v1.0/get"
|
|
|
|
|
//data := map[string]interface{}{"dev_id": "10702", "encrypted_device_info": encryptedDeviceByte}
|
|
|
|
|
//g.Client().SetHeader("Content-Type", "application/json")
|
|
|
|
|
//g.Client().SetHeader("Cache-Control", "no-cache")
|
|
|
|
|
//log.Printf("sendMsgHugeAmount - url: %s, data: %s", url, gjson.MustEncodeString(data))
|
|
|
|
|
//marshal, _ := json.Marshal(data)
|
|
|
|
|
//bytes, err := g.Client().Post(context.TODO(), url, string(marshal))
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//src := string(bytes.ReadAll())
|
|
|
|
|
//tmp, err := gcharset.ToUTF8("UTF-8", src)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//fmt.Println("Deposit - json: ", tmp)
|
|
|
|
|
////tmp, err := SendCaidMsg(ctx, url, data)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// fmt.Println("SendCaidMsg err:", err)
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//resJson, err := gjson.DecodeToJson(tmp)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// fmt.Println("SendCaidMsg DecodeToJson err:", err)
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//if resJson != nil && resJson.Get("code").Int() != 0 {
|
|
|
|
|
// fmt.Println("SendCaidMsg err:", resJson.Get("message").String())
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
//caid := "J5LdxnMrcpDiNrn2QLZvCPavphS9nUqu7uwCEjLkEvtP2blwhevgGvQD7AKajkEc+PIC2TuIFxbSGK9Jsm27CrsW446pi+gIIc3OdKB4jqQJoDD77BbJC3I105DnsEwgR8uLvSVy0NgqFq+rf4GRYwp93Jy2eZdDKkS+Y0BgTEQkedK1P29hyYEELLQzvJrq9XlwfYx1QcGGcofZmq56B65IbQQfRvoXAiSl2cm12qFkpD8KbPmvy66xE6yu3SFAMC6iAEdfW4W8hz0Qv9Bht2nhDxsm4c39z1mU41s1oP5lJ5kkUl4yU3NWRO19jIYOm8lMJb19oDc+weBaMkgIbQ=="
|
|
|
|
|
//
|
|
|
|
|
//fmt.Println("Deposit - caid: ", caid)
|
|
|
|
|
|
|
|
|
|
//encryptedDeviceInfo, err := encryptWithPublicKey(jsonStr, vm.publicKey)
|
|
|
|
|
//if err != nil {
|
|
|
|
|
// fmt.Printf("加密错误 encryptWithPublicKey: %v\n", err)
|
|
|
|
|
// return
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
// 将encryptedDeviceInfo填入请求中的encrypted_device_info字段
|
|
|
|
|
|
|
|
|
|
// 解密响应数据示例
|
|
|
|
|
// 假设从响应中获取的data字段值
|
|
|
|
|
|
|
|
|
|
//fmt.Printf("解密后的响应数据: %s\n ,decryptedByte: %s\n", decryptedData, string(decryptedByte))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ImportSPKIPublicKeyPEM() *rsa.PublicKey {
|
|
|
|
|
pubKeyBytes, err := ioutil.ReadFile("manifest/config/dev_10702/public_for_api.pem")
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("encrypt pubKeyBytes: %s\n", string(pubKeyBytes))
|
|
|
|
|
body, _ := pem.Decode(pubKeyBytes)
|
|
|
|
|
fmt.Printf(" body.Bytes : %s\n byte: %s\n", body.Type, body.Bytes)
|
|
|
|
|
publicKey, _ := x509.ParsePKIXPublicKey(body.Bytes)
|
|
|
|
|
if publicKey, ok := publicKey.(*rsa.PublicKey); ok {
|
|
|
|
|
fmt.Printf("publicKey : %d\n ", publicKey.Size())
|
|
|
|
|
return publicKey
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encrypt(deviceInfo map[string]string) (string, error) {
|
|
|
|
|
pubKey := ImportSPKIPublicKeyPEM()
|
|
|
|
|
fmt.Printf("data: len: %d\n pubkey: %v\n", len(deviceInfo), pubKey)
|
|
|
|
|
|
|
|
|
|
// 2. 计算RSA最大加密块大小(PKCS#1 v1.5填充需要11字节)
|
|
|
|
|
keySize := pubKey.Size() // 密钥字节长度(如2048位密钥为256字节)
|
|
|
|
|
maxEncryptBlock := keySize - 11 // 最大单次加密数据长度(256-11=245字节)
|
|
|
|
|
if maxEncryptBlock <= 0 {
|
|
|
|
|
return "", errors.New("无效的RSA公钥长度(必须≥11字节)")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 序列化设备信息为JSON
|
|
|
|
|
data, err := json.Marshal(deviceInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Printf("JSON序列化错误: %v\n", err)
|
|
|
|
|
return "", fmt.Errorf("JSON序列化错误: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
plaintext := data
|
|
|
|
|
// 3. 分块加密
|
|
|
|
|
var encryptedBlocks [][]byte
|
|
|
|
|
for offset := 0; offset < len(plaintext); offset += maxEncryptBlock {
|
|
|
|
|
end := offset + maxEncryptBlock
|
|
|
|
|
if end > len(plaintext) {
|
|
|
|
|
end = len(plaintext)
|
|
|
|
|
}
|
|
|
|
|
block := plaintext[offset:end]
|
|
|
|
|
|
|
|
|
|
// 执行RSA加密(使用公钥指数e)
|
|
|
|
|
encryptedBlock, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, block)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("第%d字节块加密失败: %w", offset, err)
|
|
|
|
|
}
|
|
|
|
|
encryptedBlocks = append(encryptedBlocks, encryptedBlock)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. 拼接所有加密块并进行Base64编码
|
|
|
|
|
encryptedBytes := bytes.Join(encryptedBlocks, nil)
|
|
|
|
|
// 4. 拼接所有加密块并进行Base64编码
|
|
|
|
|
return base64.StdEncoding.EncodeToString(encryptedBytes), nil
|
|
|
|
|
|
|
|
|
|
}
|