QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1902|回复: 0
打印 上一主题 下一主题

报文拼接与加解密测试

[复制链接]
字体大小: 正常 放大

1175

主题

4

听众

2877

积分

该用户从未签到

跳转到指定楼层
1#
发表于 2024-3-31 17:21 |只看该作者 |正序浏览
|招呼Ta 关注Ta
# -*- coding: gb2312 -*-3 w- k2 \0 Z  L! e5 Q: [3 ]9 R
import socket/ g  T3 p) t9 ^  ]- }( K
from myutil import *
& j' N  U! A* @  [& b. v  |3 Dfrom binascii import unhexlify as unhex
9 V! v1 _% G4 W, h& R) {from ctypes import *  z: L- p/ p" J6 X, e  d; f
dll = cdll.LoadLibrary('mydll.dll')" p/ G7 K) z4 G, r
print 'begin load mydll..'
( L" W/ `) f3 k3 _/ @0 X" D, d: f#key7 Q. _9 R1 k* E
key='\xF1\xE2\xD3\xC4\xF1\xE2\xD3\xC4'
0 E' b6 C( U- [6 g5 C  _$ i8 X( T#output MAC$ {3 N$ }. G/ X: U
mac='\x00'*8
+ B( z! x& r% _data='\x00'*82 F0 e4 P" i, n  X! D: X
pkey=c_char_p()8 r3 c3 h  t1 v; U# p6 h: Y
pdata=c_char_p()5 q, U) w7 _" V+ K3 e8 X
pmac=c_char_p()9 T" k+ A; j( k1 f8 A  Z  F0 N3 d
pkey.value=key0 e+ }* J8 Q, g
pdata.value=data6 m$ ?2 }6 o0 f
pmac.value=mac
- d) a: x) J8 `# }# o4 N/ `4 X, j; S#pack1
/ `$ p$ c: I6 e& d) ^$ Yclass pack:
" w- q: J" i) @$ h+ P+ J) h, ~" E   pass
8 ~$ t% z2 j: A5 f. o) N& @pk=pack()
" d6 Z. ]$ a8 q0 ]pk.len='00000032'. j+ Q6 S% `/ I4 A
pk.ID='0001'
3 ^  [" k( ]) vpk.slnum='00000004'2 F4 v/ Q( [% o" ~
pk.poscode='123456781234'
0 ~7 p6 P* c* M& n7 }pk.rand='1122334455667788'- F3 L6 U9 m- A5 {
pk.psam='313233343536'
1 ]& l9 O! y$ b4 v" c% Upk.kind='0000'
3 h! P. Q# ^2 Z: s; Fpk.ver='000001'+ `1 ~8 @' a0 F# ~# e! S1 N
pk.time='20140805135601'
1 K3 H- z* Y, v, M7 L1 rpk.mac='06cc571e6d96e12d'
! ?+ I* k/ b9 h- H7 x1 I; v
( b1 @  [; u+ f1 `; @, {) S# H5 gdata=unhex(pk.len+pk.ID+pk.slnum+pk.poscode+pk.rand+pk.psam+pk.kind+pk.ver+pk.time)
/ `& a# S# e+ V#print_hex(data)
7 j. C) z2 O8 W# q- opdata.value=data! H6 Z% a: G6 c" c$ P
#cacl MAC
4 @& g& G5 ~$ x: Q. b  K4 B% rdll.CurCalc_DES_MAC64(805306481,pkey,0,pdata,42,pmac)' ^7 s* |4 m9 X6 D2 [
stro= pmac.value
4 r5 W- H) z2 T2 T' Dstrtemp=''* A2 |+ q2 N' |# y4 o6 |% p  ]& ^
for c in stro:
+ a4 d& u6 }3 o: ?# j    strtemp+="{0:02x}".format(ord(c))
9 K1 p4 [5 N; K. \9 j#print strtemp
- A2 z% H8 {9 J# Opk.mac=strtemp7 O+ `+ |8 e* g
#data to send' x. q  `& s. c* b0 C, ?
sd=pk.len+pk.ID+pk.slnum+pk.poscode+pk.rand+pk.psam+pk.kind+pk.ver+pk.time+pk.mac
3 Q& l  u, f! i! {# g; F& mprint  'send1 len is 0x%02x' %(len(sd)/2)9 d- Y) ~' l3 a4 ^4 J& x7 `
print sd
1 L6 X8 ~( i$ ?% h3 i3 S* W# o& ~#pack2
6 H' g* z# ^2 h( c8 nclass pack2:
6 u0 i# b' u7 S, t* O7 l( g   pass+ L- `9 I5 r/ l( S) h
pk2=pack2()+ Y; r3 K5 ?' b2 K, B2 g# i( j
pk2.len='0000006E'
, @) H3 O, K% G* ~: m6 C! mpk2.ID='0012'
2 u  P7 @2 E3 n3 D/ i6 H9 R( _& S3 Zpk2.slnum='00000005'" N& Y: F& [* P, f
pk2.fatCode='00'0 x# L7 i8 H2 Y3 S* \
pk2.cardASN='0000000000000000'
4 O: n- j/ B' ?$ U# |pk2.cardType='00'
$ a+ N) x6 m3 Tpk2.userNO= '0000000000000000'
& i" d! |3 o$ L3 T$ L1 J, l9 s8 b1 {8 g. n7 s
pk2.fileName1='00000000000000000000000000000015'" R% H( T7 o* K- k$ T
pk2.dataLen1='00', t! N1 n. N7 h, r* [5 y
pk2.dataArea1='00000000000000319999990800FB2014080620240806FFFFFFFFFFFFFFFFFFFF'! C$ p8 g2 z# n  B) M( d+ j
pk2.fileName2='00000000000000000000000000000016'
/ x8 J5 b. ?' Z4 `8 i# E; \pk2.dataLen2='00'
9 G4 l" A: Z! d3 P& u6 [pk2.dataArea2='000003E800FFFF16'7 H; R3 A  z1 y! x
pk2.mac='06cc571e6d96e12d'
! p% n1 C- `0 V3 G4 E% V3 Q( X- y, k/ a; I
data2=unhex(pk2.len+pk2.ID+pk2.slnum+pk2.fatCode+pk2.cardASN+pk2.cardType+pk2.userNO+pk2.fileName1+pk2.dataLen1+pk2.dataArea1+pk2.fileName2+pk2.dataLen2+pk2.dataArea2)0 o* Q. `, z! Z- P/ D
' F: R+ O8 x% g9 k
pdata.value=data2
. E0 ]6 P( h' _" t$ X#cacl MAC0 O9 A+ m# x2 v( G4 a( r
dll.CurCalc_DES_MAC64(805306481,pkey,0,pdata,102,pmac)
( R) r/ W- m' r/ pstro= pmac.value4 p7 g  v  G$ a' {% R
strtemp=''- D# V$ V9 r9 k' l8 j/ `9 l6 J
for c in stro:
$ X' L; G/ ^3 c. _# G: `    strtemp+="{0:02x}".format(ord(c))8 h' @3 M' w4 P
#print strtemp  i/ ]$ v- _3 }8 @& W) P
pk2.mac=strtemp
1 W7 T' e+ ^2 }#data to send' C# ]/ r2 ]6 J7 k# h& D7 A1 ^
sd2=pk2.len+pk2.ID+pk2.slnum+pk2.fatCode+pk2.cardASN+pk2.cardType+pk2.userNO+pk2.fileName1+pk2.dataLen1+pk2.dataArea1+pk2.fileName2+pk2.dataLen2+pk2.dataArea2+pk2.mac
- l3 }; G9 C( a9 J' t0 q, ]3 F0 G# k0 i6 @+ z' H# L5 X- d
print  'send2 len is 0x%02x' %(len(sd2)/2)7 y7 m9 q& ]( a. H! T
print sd2
$ s) C. |8 t* \: Q. q( `+ J, n2 a( O3 ~& V
( \' K2 k6 P0 K1 Z$ k# ?
#PORT="192.168.60.37"
# c8 V3 ?! U2 x/ Z/ Y#PORT="localhost"
# F! ?' a- O3 e; ~HOST, PORT = "192.168.51.28", 58005 V. P+ M5 y2 F5 }! L3 @
# Create a socket (SOCK_STREAM means a TCP socket)
6 `) t' S, _( E& s. l$ Ksock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- G3 D. {4 K  ?6 M  K2 b9 Jtry:
, l" R  I0 ~- H) a) r    # Connect to server and send data
1 y* T3 e3 @# d% o    sock.connect((HOST, int(PORT))
( R6 f( }( ]7 t        #data= "123456789"' L. d  {+ R. P* s( _, S: a6 a
        #s = struct.pack('bbb',1,2,3)
1 t+ I) H0 [3 D  }0 b1 C    sock.send(sd.decode('hex'))+ M/ m: F! s# z: L. w( M' `$ X2 r
    print "Sent1 OK:"
# P$ \0 e0 L& m$ S: D4 o- R: \    print sd
$ B4 L2 \7 W+ s0 H$ x) S8 L    # Receive data from the server and shut down' s; R8 h! r& Q& a/ ]
    received = sock.recv(1024)( l. K: H% i+ C6 k- @5 }
    print "Received:"4 j# _. B% P/ I6 W
    print_hex(received)8 S- N0 x7 m* Z% m; h& n' |* \
    print  'received len is 0x%02x' %(len(received))4 \3 T+ G# D. W8 L/ T, l) z& t
    print  'received data analysis...'
* P. [1 u0 R+ S4 p    re1=received[0:4]& V- W2 P: Y- h/ |* F: t8 M
    print_hex(re1)
5 X6 I5 W) o* j6 E7 B2 h3 e    re1=received[4:6]6 E( `- ~; b! ?, s* A8 q+ t9 P6 s* ~
    print_hex(re1)
# M, [, P5 N) V4 K+ @    re1=received[6:10]
3 O+ d' u$ M0 U. s1 o    print_hex(re1)
4 x  E. U6 w) h9 e) {( E. c2 t    re1=received[10:16]" i/ B* y6 e% o7 t# U# h7 Z
    print_hex(re1)& P! \% l2 G- y3 M/ i2 n

! V6 R5 O$ g- g. r  I    #pack2 send
. V4 p+ Y) h% q: K  J: J" R    sock.send(sd2.decode('hex'))( _* |0 i0 w7 p( X/ ^- J
    print "Sent2 OK:"
, X' A, }$ F- @8 v+ y! n; j    print sd2  k$ t& C1 }5 _# i. G4 y
    # Receive data from the server and shut down. @# Z: ~8 a4 Q$ S& r
    received1 = sock.recv(1024)% i, g' w% S  j" j' b! c4 H0 t0 Q# P
    print "Received1:"
7 _6 i$ b) k8 u! Z' C/ W) |    print_hex(received1)
. U/ E' \1 W8 }- m( M& t    print  'received1 len is 0x%02x' %(len(received1))
# J9 q4 X& A4 }) Y6 C% F6 T9 q. e# K3 Z9 R3 K
finally:
2 T1 u* _5 Y/ l6 R, w1 P    sock.close()
2 T/ e5 j; u/ }- n- v& J! `4 z" U1 D$ @9 N* U$ I" P( a" O2 ~
s=raw_input('press any key to continue...'): ?" I+ ~2 }5 ~" u# X# j' u' x' ?& R
: T2 }  N# i; h' }  B, i; j, K
& D8 G( m" H5 G1 B3 O; v
zan
转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
您需要登录后才可以回帖 登录 | 注册地址

qq
收缩
  • 电话咨询

  • 04714969085
fastpost

关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

手机版|Archiver| |繁體中文 手机客户端  

蒙公网安备 15010502000194号

Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

GMT+8, 2025-8-29 15:13 , Processed in 1.898474 second(s), 51 queries .

回顶部