github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/helper/aes128com.go (about) 1 package helper 2 3 import ( 4 //"encoding/base64" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "io/ioutil" 9 "net/http/cookiejar" 10 simplejson "github.com/insionng/yougam/libraries/bitly/go-simplejson" 11 ) 12 13 func Aes128COMEncrypt(content string, constKey string) (string, error) { 14 15 guidKey := GUID32BIT() //32位 16 17 kakey := guidKey[:8] //8位 18 kbkey := guidKey[len(guidKey)-3:] //3位 19 key := kakey + kbkey + constKey //16位 , constKey = "23423" //5位 20 21 if len(key) != 16 { 22 return "", errors.New("Aes128 the key length must be 16 bytes.") 23 } 24 25 if crypted, err := AesCBCEncrypt([]byte(content), []byte(key)); err != nil { 26 return "", err 27 } else { 28 data := kakey + string(crypted) + kbkey 29 return data, err 30 31 } 32 } 33 34 func Aes128COMDecrypt(crypted string, constKey string) (string, error) { 35 if len(crypted) <= 11 { 36 return "", errors.New("Aes128 the Ciphertext is too short!") 37 } 38 kakey := crypted[:8] //前8位 39 kbkey := crypted[len(crypted)-3:] //后3位 40 key := kakey + kbkey + constKey //16位 , constKey = "23423" //5位 41 42 if len(key) != 16 { 43 return "", errors.New("Aes128 the key length must be 16 bytes.") 44 } 45 46 crypteds := crypted[8 : len(crypted)-3] //截取密文 47 48 if crypteds == "" { 49 return "", errors.New("密文为空!") 50 } else { 51 52 // 对AES密文进行解密 53 if decrypts, err := AesCBCDecrypt([]byte(crypteds), []byte(key)); err == nil { 54 return string(decrypts), err 55 } else { 56 return "", err 57 } 58 } 59 } 60 61 func SetJsonCOMEncrypt(status int64, msg string, data interface{}) (string, error) { 62 //{"status":1, //状态。(1成功, 0失败)"msg":"success", //描述"data":""} 63 m := map[string]interface{}{} 64 65 if status != 1 { 66 status = 0 67 } 68 69 if msg == "" { 70 if status == 1 { 71 msg = "success" 72 } else { 73 msg = "failure" 74 } 75 } 76 77 m["data"] = data 78 m["msg"] = msg 79 m["status"] = status 80 if content, err := json.Marshal(m); err != nil { 81 return "", err 82 } else { 83 if crypted, err := Aes128COMEncrypt(string(content), AesConstKey); err != nil { 84 return "", err 85 } else { 86 return crypted, err 87 } 88 89 } 90 91 return "", errors.New("SetJsonCOMEncrypt Error") 92 93 } 94 95 func GetJsonCOMDecrypt(status string, actionurl string, data interface{}, ckJar *cookiejar.Jar) (*simplejson.Json, error) { 96 97 content, e := json.Marshal(data) 98 if e != nil { 99 return nil, e 100 } 101 102 crypted, e := Aes128COMEncrypt(string(content), AesConstKey) 103 if e != nil { 104 return nil, e 105 } 106 107 if ckJar == nil { 108 ckJar, _ = cookiejar.New(nil) 109 } 110 111 if reps, e := SendPacket(status, actionurl, crypted, ckJar); e == nil { 112 113 bodyByte, e := ioutil.ReadAll(reps.Body) 114 if e != nil { 115 return nil, e 116 } 117 118 bodyString := string(bodyByte) 119 if reps.StatusCode == 200 { 120 //fmt.Println("服务端响应成功") 121 if bodyString == "" { 122 return nil, errors.New("服务端响应正文为空!") 123 } else { 124 if s, err := Aes128COMDecrypt(bodyString, AesConstKey); err != nil { 125 return nil, errors.New(fmt.Sprint("解密失败错误:", err.Error())) 126 127 } else { 128 return simplejson.NewJson([]byte(s)) 129 } 130 131 } 132 133 } else if reps.StatusCode == 401 { 134 return nil, errors.New("服务端响应状态:尚未认证!") 135 } else { 136 return nil, errors.New(fmt.Sprint("服务端响应错误状态:", reps.StatusCode)) 137 } 138 139 } 140 141 return nil, errors.New("GetJsonCOMDecrypt Error") 142 143 }