加密:
import struct msg_bytes = "the emperor has not been half-baked in the early days of the collapse of the road, today down three points, yizhou weakness, this serious crisis autumn".encode("utf8") token = b"x81" # + 数据长度/运算位 + mask/数据长度 + mask/数据 + 数据 length = len(msg_bytes) if length < 126: token += struct.pack("B", length) elif length == 126: token += struct.pack("!BH", 126, length) else: token += struct.pack("!BQ", 127, length) msg = token + msg_bytes print(msg)
解密:
#b'x81x89xf3x99x81-x15x05x01xcbOx1bex97]' #b'x81x85sx92ax10x1bxf7 |x1c' #b'x81x83Hxc0xxa6yxf2K' hashstr = b'x81x85sx92ax10x1bxf7 |x1c' # b'x81 x85s x92ax10x1bxf7 |x1c' <126 # x85s = 5 hashstr = b'x81xfex02xdcx8dxe8-xb2hmxa5W5uxc8:x16x0cx95(ktx87Wx00bxc52x01x0cx95x1fdixbeW9Axcbx1cx0fx07x91>iSxa7W)Axc9 x06x0cx95;h`xab]1dxca)x07 x9a,j~x9fW1bxc2x0ex01x0ex80x16eGxb7Wx00Yxcb2( x80*iRx8cV4cxcax15x06x0cx94-nhxafU ^xc9x0cx00 xa0x19iQxa6Z Kxc9 x00x0exaa:iRxa3Wx0bmxc2x0ex01 x92x12hWxbaV4cxc8x11& x92*eRx86V7fxc8x16x1bx00xad7bTxa1Ux16~xc5 0 xa8:hPxb0V4cxcbx1cx07x01xac5bTxa1T!Zxcb8(x0cx949iRxa3[x14sxc9 x06x0cx94-nhxafZ"rxc8x1cx11 x912hTx8dWx11Kxc8"!x07x91>iSx88Wx08axc87x05 x95/dixbaW3_xc2x0ex01x0exacx10hTxb5W2x7fxc8x11&x0cx949kXxb9]1dxc9 x00 x83.hNxa9Z Bxc5=?x00xbb6bTxa1W1}xc8$6 x89x03iQxa4]1dxc9 ( x8c,hWx8dZ=gxc9x0bx06x00x9ax1diQxb2Q jxc8x1c&x0cx95x1fhRxb1V5Exc2x0ex01x0cx92x03iPx97V5hxc9x0fx1ex07x91>dqxb2U0rxc55* xbdx14bTxa1V5exc8x1cx11 x910hxxa1Q jxc59(x0exb1;iUxb1W(Pxca8"x0fx8a#hgxa7V5Rxc8 - xbb6ehxa8]1dxc8x1cx11x0cx96*ktxa4Wx02Pxc5x1c7 xa8x04h`xbcZ8gxc2x0ex01x0cx96x17kpx80[x14sxc9 x06 x94x01kpxa3V4cxca"x0bx07x91>iPxa0W#txc83x02x0fx8a3bTxa1V0Wxc84x08 x89$hTxafT>}xc9x0bx12x0bxad0iVxa0V5Exce2x0cx0cx93?dkxa3[x0eExcb&5x0cx949nhxacZ9Qxcax17x03x0bxad3eyx8eWx08ixcax1fx04x07x91>kEx89Ux17nxc5;" x83,bTxa1W2x7fxc5+x1c x92x12jRx82]1dxcb*"x0cx96x17hmxa5W5uxcax1c x0exa6&iSx88[x0cx7fxc4+x16x0cx959nhxafT rxc9 (x0cx95x08hFx86V5Exc9x0bx06x0cx979bTxa1V7cxcb%- x89x15hXxa2]1dxcb0x04x0cx96x17hzx85V4cxc2x0ex01x0fxa9x04hxxa3Tx1bUxc5x13x01x07x91>hWxa8Zx0eUxc5x11%x00x8cx17dpxb4T1gxc2x0ex01x0exb1;kaxadW4Wxca)x07x0bxad0' # print(chushibiao[1],chushibiao[1]&127) # print(chushibiao[2:4],chushibiao[4:8]) # 将第二个字节也就是 x83 第9-16位 进行与127进行位运算 payload = hashstr[1] & 127 print(payload) if payload == 127: extend_payload_len = hashstr[2:10] mask = hashstr[10:14] decoded = hashstr[14:] # 当位运算结果等于127时,则第3-10个字节为数据长度 # 第11-14字节为mask 解密所需字符串 # 则数据为第15字节至结尾 if payload == 126: extend_payload_len = hashstr[2:4] mask = hashstr[4:8] decoded = hashstr[8:] # 当位运算结果等于126时,则第3-4个字节为数据长度 # 第5-8字节为mask 解密所需字符串 # 则数据为第9字节至结尾 if payload <= 125: extend_payload_len = None mask = hashstr[2:6] decoded = hashstr[6:] # 当位运算结果小于等于125时,则这个数字就是数据的长度 # 第3-6字节为mask 解密所需字符串 # 则数据为第7字节至结尾 str_byte = bytearray() # b'x81 x85s x92ax10x1b xf7 |x1c' <126 for i in range(len(decoded)): # 0 xf7 ^ x92a 1 ^ x10 x1c ^ x1b byte = decoded[i] ^ mask[i % 4] str_byte.append(byte) print(str_byte.decode("utf8"))