bytes.NewReader()gzip.NewReader()
gzip.NewReaderio.ReaderReaderio.Reader

如果你想知道这一段文字是,

“amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0 =”

这是从我的客户Python脚本发送采样输入。它使用gzip压缩,使用AES128进行加密,最后按照base64进行编码。

客户端代码:

import time 
import json 
import requests 
import random 
import gzip 
import base64 

from Crypto.Cipher import AES 

baseurl = 'http://0.0.0.0:80' 
key = 'TfvY7I358yospfWKcoviZizOShpm5hyH' 
iv = 'mb13KcoviZizvYhp' 

MODE = AES.MODE_CFB 
BLOCK_SIZE = 16 
SEGMENT_SIZE = 128 

def http_post(url, data): 
    print('Going to make a request to {} with the following data: {}'.format(url, data)) 
    r = requests.post(url, 
        data=data, 
        headers={'Content-type': 'application/json; charset=utf-8', 
          'Connection': 'keep-alive'},) 
    if r.status_code != 200: 
     print('Server returned unexpected response code {}, and content: {}'.format(r.status_code, r.content)) 
     return False 
    else: 
     data = r.json() 
     return data 


def post_sample_data(key, iv): 
    fake_device_id = "MB88" 
    Load_VA_Total_Mean = random.randint(1000, 100000) 
    print('Data that should come back: {}'.format(Load_VA_Total_Mean)) 
    data = {'i': fake_device_id, 'p': [ 
    {'d': [54.3, 0, 99, 49.35, 3, 99, 51.533, 1, 98, 28964, 7348, 43590, Load_VA_Total_Mean, 10350, 55200, 49.7], 
    't': time.time(), 'dt': 'p'}]} 
    url = baseurl + '/realtimedata' 
    encryption_key_reference = 1 
    payload = '{}\n{}'.format(convert_pack(data, key, iv), encryption_key_reference) 
    return http_post(url, payload) 


def convert_pack(inputdict, key, iv): 
    jsonpayload = json.dumps(inputdict) # encode dict to json string 
    gzippayload = gzip.compress(jsonpayload.encode('utf-8')) # compress with gzip 
    encryptedpayload = base64.b64encode(encrypt(key, iv, message)) 
    encoded = base64.b64encode(encryptedpayload) 
    print('encoded: {}'.format(encoded)) 

    return str(encoded, encoding='utf-8') 


def _pad_string(value): 
    length = len(value) 
    pad_size = BLOCK_SIZE - (length % BLOCK_SIZE) 
    return value.ljust(length + pad_size, '\x00') 


def encrypt(key, iv, plaintext): 
    aes = AES.new(key, MODE, iv, segment_size=SEGMENT_SIZE) 
    plaintext = _pad_string(plaintext) 
    encrypted_text = aes.encrypt(plaintext) 
    return encrypted_text 


post_sample_data(key, iv) 

服务器代码:

package main 

import (
"crypto/aes" 
"crypto/cipher" 
"crypto/rand" 
"encoding/base64" 
"fmt" 
"io" 
"compress/gzip" 
"io/ioutil" 
"bytes" 
) 

func main() { 
    receivedText := "amZzRUR2NHVtcVpiZHNROHJiTTNYeGdUSndGTlVDZC9jaElSK1lXcFlJOD0=" 
    fmt.Println("encrypted + encoded + gzipped: ", originalText) 
    key := []byte("TfvY7I358yospfWKcoviZizOShpm5hyH") 

    text := decrypt(key, originalText) 
    fmt.Println("decrypted: ", string(text)) 
    reader := bytes.NewReader(text) 
    gzReader, err1 := gzip.NewReader(reader) 
    fmt.Println(gzReader) 

    if err1 != nil { 
     fmt.Println("error1") 
    } 

    content, err2 := ioutil.ReadAll(gzReader) 

    if err2 != nil { 
     fmt.Println("error2") 
    } 

    fmt.Println(string(content)) 
} 

func decrypt(key []byte, cryptoText string) []byte { 
    ciphertext, _ := base64.StdEncoding.DecodeString(cryptoText) 
    fmt.Println("decoded: ", string(ciphertext)) 
    block, err := aes.NewCipher(key) 
    if err != nil { 
     panic(err) 
    } 

    iv := []byte("mb13KcoviZizvYhp") 
    stream := cipher.NewCFBDecrypter(block, iv) 
    stream.XORKeyStream(ciphertext, ciphertext) 

    return ciphertext 
} 

输出:

<nil> 
error1 
panic: runtime error: invalid memory address or nil pointer dereference [recovered] 
    panic: runtime error: invalid memory address or nil pointer dereference 
[signal SIGSEGV: segmentation violation code=0x1 addr=0x288 pc=0x10a9167] 

goroutine 1 [running]: 
io/ioutil.readAll.func1(0xc420037dd0) 
    /usr/local/go/src/io/ioutil/ioutil.go:30 +0x119 
panic(0x10c4580, 0x1154d00) 
    /usr/local/go/src/runtime/panic.go:489 +0x2cf 
compress/gzip.(*Reader).Read(0x0, 0xc420092000, 0x200, 0x200, 0x1024ade, 0xc400000008, 0xc4200120c0) 
    /usr/local/go/src/compress/gzip/gunzip.go:247 +0x37 
bytes.(*Buffer).ReadFrom(0xc420037d28, 0x11451a0, 0x0, 0xc420092000, 0x0, 0x200) 
    /usr/local/go/src/bytes/buffer.go:179 +0x160 
io/ioutil.readAll(0x11451a0, 0x0, 0x200, 0x0, 0x0, 0x0, 0x0, 0x0) 
    /usr/local/go/src/io/ioutil/ioutil.go:33 +0x150 
io/ioutil.ReadAll(0x11451a0, 0x0, 0x1, 0x7, 0x0, 0x0, 0x20) 
    /usr/local/go/src/io/ioutil/ioutil.go:42 +0x3e 
main.main() 
    /Users/bkaankuguoglu/Desktop/Go-dev/tool-backend/tester.go:43 
+0x404 
exit status 2