|
|
|
@@ -3,35 +3,35 @@ package main |
|
|
|
import ( |
|
|
|
"crypto/aes" |
|
|
|
"crypto/cipher" |
|
|
|
"crypto/rand" |
|
|
|
"encoding/base64" |
|
|
|
"fmt" |
|
|
|
) |
|
|
|
|
|
|
|
const txt = "P2DoRtilYwJ1aM+VW1sGs6p11Rhcd/TrFYtvsw53SUVw2Knh27hF5IZUBxRXbz+k87zy983ec5aOwgS+WxYoejmGubaqiYy2yfCBNyGRlWfe+iWc2TnvPIEAJebSWuNOJ7FoITeMndr4tW391XxDdMom8I/VsqKnsZZAangUsxmA1ZEnP/d4Lx8/xt3qJKrJMa4Q8s9VsOOvzLIGhL1IN5bHaXN+CwgBTAUCrHD8AneiX5MLSv/74qozg+jKBSUebZrjRYuTymQ3TBh3pQXyRBQTZNrCAe1tlcNli9e5MSUMsHFIzGw/kiu93/5VkxCYRLgRDQKl9oam4+Rpxywir7EiT7I4X343l9ogcMLWX16evkLlQHoqBhLl6ZcfW7Nmq8/Ghy8jbuwqkR/0jLJ/avtjzgJOwaxdBUz4nYZ452rLJekxIvsmV6PZgCXRrKHzmpZX+i6hIRIWiIaDeMOEsw==" |
|
|
|
var random16 []byte |
|
|
|
|
|
|
|
//Decode Decode encrypt string to xml context |
|
|
|
func Decode(s string) string { |
|
|
|
//Encode convert a xml sequence into encrypted message |
|
|
|
func Encode(s string) string { |
|
|
|
|
|
|
|
r, _ := base64.StdEncoding.DecodeString(txt) |
|
|
|
d := aesEncryptMsg(random16, []byte(s), APIConfig.Appid, getAesEncryptKey()) |
|
|
|
r := base64.StdEncoding.EncodeToString(d) |
|
|
|
fmt.Println(r) |
|
|
|
return r |
|
|
|
} |
|
|
|
|
|
|
|
key, _ := base64.StdEncoding.DecodeString(APIConfig.EncodingAESKey + "=") |
|
|
|
var k [32]byte |
|
|
|
copy(k[:], key) |
|
|
|
random, raw, err := AESDecryptMsg([]byte(r), APIConfig.Appid, k) |
|
|
|
//Decode Decode encrypt string to xml context |
|
|
|
func Decode(s string) string { |
|
|
|
|
|
|
|
fmt.Println(len(txt)) |
|
|
|
fmt.Println(random) |
|
|
|
fmt.Println(string(raw)) |
|
|
|
r, _ := base64.StdEncoding.DecodeString(s) |
|
|
|
|
|
|
|
bc, err := aes.NewCipher([]byte(key)) |
|
|
|
r16, raw, err := aesDecryptMsg([]byte(r), APIConfig.Appid, getAesEncryptKey()) |
|
|
|
random16 = make([]byte, 16) |
|
|
|
copy(random16, r16) |
|
|
|
if err == nil { |
|
|
|
fmt.Printf("The block size is %d\n", bc.BlockSize()) |
|
|
|
var decoded = make([]byte, 4096) |
|
|
|
bc.Decrypt(decoded, r) |
|
|
|
//AES Decode |
|
|
|
fmt.Println(string(raw)) |
|
|
|
return string(raw) |
|
|
|
} |
|
|
|
fmt.Printf("%s", string(r)) |
|
|
|
return string(r) |
|
|
|
return "" |
|
|
|
} |
|
|
|
|
|
|
|
// 把整数 n 格式化成 4 字节的网络字节序 |
|
|
|
@@ -57,8 +57,15 @@ func decodeNetworkBytesOrder(orderBytes []byte) (n int) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//AESEncryptMsg encryptedMsg = AES_Encrypt[random(16B) + msg_len(4B) + rawXMLMsg + AppId] |
|
|
|
func AESEncryptMsg(random, rawXMLMsg []byte, AppId string, AESKey [32]byte) (encryptedMsg []byte) { |
|
|
|
func random16Byte() []byte { |
|
|
|
token := make([]byte, 16) |
|
|
|
rand.Read(token) |
|
|
|
return token |
|
|
|
} |
|
|
|
|
|
|
|
//AESEncryptMsg given an xml message and 16 bytes random string |
|
|
|
//encryptedMsg = AES_Encrypt[random(16B) + msg_len(4B) + rawXMLMsg + AppId] |
|
|
|
func aesEncryptMsg(random, rawXMLMsg []byte, AppId string, AESKey [32]byte) (encryptedMsg []byte) { |
|
|
|
const BLOCK_SIZE = 32 // PKCS#7 |
|
|
|
|
|
|
|
buf := make([]byte, 20+len(rawXMLMsg)+len(AppId)+BLOCK_SIZE) |
|
|
|
@@ -90,8 +97,9 @@ func AESEncryptMsg(random, rawXMLMsg []byte, AppId string, AESKey [32]byte) (enc |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//AESDecryptMsg given a string decode it into three parts |
|
|
|
// encryptedMsg = AES_Encrypt[random(16B) + msg_len(4B) + rawXMLMsg + AppId] |
|
|
|
func AESDecryptMsg(encryptedMsg []byte, AppId string, AESKey [32]byte) (random, rawXMLMsg []byte, err error) { |
|
|
|
func aesDecryptMsg(encryptedMsg []byte, AppId string, AESKey [32]byte) (random, rawXMLMsg []byte, err error) { |
|
|
|
const BLOCK_SIZE = 32 // PKCS#7 |
|
|
|
|
|
|
|
if len(encryptedMsg) < BLOCK_SIZE { |