QQ登录

只需要一步,快速开始

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

报文拼接与加解密测试

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

1175

主题

4

听众

2877

积分

该用户从未签到

跳转到指定楼层
1#
发表于 2024-3-31 17:21 |只看该作者 |倒序浏览
|招呼Ta 关注Ta
# -*- coding: gb2312 -*-
% a' K1 a+ ~$ B- k; k+ W% S1 \1 t6 ]import socket9 n" M; a8 a4 o) |& A- C& P; R
from myutil import *
2 [1 e% k8 y0 n; Ofrom binascii import unhexlify as unhex- t8 y0 H* |( O$ h" h! R7 w; ^
from ctypes import *
1 T3 ^& {+ g. U, z( odll = cdll.LoadLibrary('mydll.dll'); Z) S  E3 t, m9 T6 {, a! V
print 'begin load mydll..'" A5 D& d" X! T4 V4 W
#key* s& _) c; I6 B! W+ |
key='\xF1\xE2\xD3\xC4\xF1\xE2\xD3\xC4'
( c; y9 `, m! Y! U1 r* b#output MAC5 Y, w# \" F9 A0 O  f  X! h
mac='\x00'*8
9 K( \. t7 c# O7 [) I. o) B: Tdata='\x00'*8! i" y7 K5 a( Q$ H
pkey=c_char_p(). S( v( ^2 Y6 ?) Y; z+ R/ \
pdata=c_char_p()
: {- F/ N" m: P8 Z) v4 J- p  X0 npmac=c_char_p()
& G/ g! g1 t, u! J! o3 S! o" upkey.value=key& H1 o2 N: S" B8 F0 R
pdata.value=data
+ V) o# p, w8 o+ rpmac.value=mac" I* F! A# P* |2 ]$ B' x( n+ l
#pack1$ S, j8 k  |$ v$ {+ q* x
class pack:
/ u; Y) M" s+ G- p   pass" k" X" N/ a' }1 n1 t) M
pk=pack()
0 Q0 K7 _8 y7 Y5 S, Tpk.len='00000032'% j2 R% O3 s% R# O" M
pk.ID='0001'+ n6 k6 s' e- e
pk.slnum='00000004'* P# m8 M6 Y3 R2 ]
pk.poscode='123456781234'
# P; S1 e# D/ q$ B2 q% Ypk.rand='1122334455667788'
/ r2 c) P6 i5 ~6 xpk.psam='313233343536'
! v' D# W7 Y; \% C6 ?1 R  ^pk.kind='0000'
# |) b. Y/ E. Spk.ver='000001'
- @* M6 Y$ _% R1 l! W" o- R7 @pk.time='20140805135601') X% ^" M  @! L
pk.mac='06cc571e6d96e12d'! h, C# H- F# s6 n3 c. t2 k

# `+ Z4 @$ f& }7 Tdata=unhex(pk.len+pk.ID+pk.slnum+pk.poscode+pk.rand+pk.psam+pk.kind+pk.ver+pk.time)
) |  y. J! t! \) D3 l# S#print_hex(data)
9 M9 q) a& Q: q/ x/ v4 X* fpdata.value=data; ~% B/ F, J& {
#cacl MAC
; U. C: J7 a6 M5 r# }dll.CurCalc_DES_MAC64(805306481,pkey,0,pdata,42,pmac); y% i9 r- L4 V( [! R! ?& n. n
stro= pmac.value6 C& }- Q% q* i/ C8 f0 H# Y
strtemp=''! B6 O6 s4 h  {6 @% l0 Q
for c in stro:
+ }) t9 A5 T" `+ l' q( E- x9 ^' Z    strtemp+="{0:02x}".format(ord(c))8 f& Y( ^  H* t) x; c, K
#print strtemp
. A! ^. ?, u& H) y" h# qpk.mac=strtemp
4 m& A+ H8 L( Z" t# a" o#data to send
9 T+ s- E+ I. Q! c8 I& gsd=pk.len+pk.ID+pk.slnum+pk.poscode+pk.rand+pk.psam+pk.kind+pk.ver+pk.time+pk.mac
. w8 Z% p% j  u7 S: Sprint  'send1 len is 0x%02x' %(len(sd)/2)
0 ^5 w6 c, _$ Nprint sd' W1 G. G9 W( m, ~/ o( X
#pack2: Y# ~" u5 R6 R. E# q* G
class pack2:5 E) \: D" Q- s: i7 W8 G
   pass! @% y2 |7 C# z, A/ q7 S' u
pk2=pack2()
6 F1 o, j! r7 [. n+ p, `pk2.len='0000006E'
  b( S! U  |4 p( C" m( |) t* jpk2.ID='0012'
2 B; I& }% i+ ]' a% dpk2.slnum='00000005'0 p0 q* D: }. ]6 I
pk2.fatCode='00'
; }& ]0 r1 k5 P! C* Vpk2.cardASN='0000000000000000'
1 G, r  ~) @0 Qpk2.cardType='00'/ V  k& S$ T# c) b
pk2.userNO= '0000000000000000'
5 Q; o0 r3 ?# D- M% M: ~
: }( q* H2 u' ?& spk2.fileName1='00000000000000000000000000000015'
; B" I7 F/ J. K% @* l3 apk2.dataLen1='00'
3 u5 L, u% f/ E5 C9 Lpk2.dataArea1='00000000000000319999990800FB2014080620240806FFFFFFFFFFFFFFFFFFFF'
: Z7 Q+ E) Z+ T# N% r/ b  zpk2.fileName2='00000000000000000000000000000016': }2 ~. `7 w' t
pk2.dataLen2='00'
. e2 p( q% }& Q) c7 V1 Kpk2.dataArea2='000003E800FFFF16'1 f& C" X) A" u1 d  V, h! U: `' f
pk2.mac='06cc571e6d96e12d', k4 B/ w7 J. w4 x

+ w" G* }7 r& u3 ?& Tdata2=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)
: Y4 G! ~3 J" Y: \, ?% D, X, v) R
1 Y' g4 ^& N! |# g  wpdata.value=data2
3 n8 i& C; G# Q. `& U# p+ \#cacl MAC
( I% ]7 G6 U/ t% b6 h9 B. N0 i5 J# Ddll.CurCalc_DES_MAC64(805306481,pkey,0,pdata,102,pmac)$ q9 `% n7 P" v$ m8 r
stro= pmac.value8 T, f6 x, W$ p
strtemp=''
4 H% ~! U) b: hfor c in stro:3 _- O* K! \) d4 _. W7 d' x
    strtemp+="{0:02x}".format(ord(c))# @; h/ v1 t6 b: i; M
#print strtemp
. e' h" a9 v5 r' L! J5 o: f3 ]pk2.mac=strtemp
  D( Y1 [9 Q; k) c& ~4 U#data to send( V" P4 @0 F" f
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.mac1 b6 {' J) T  R, f& F' r
. u0 ^' C$ t4 V- c. ]! W
print  'send2 len is 0x%02x' %(len(sd2)/2)  {" F+ ?% _" E  D6 k  ]
print sd29 t0 L3 U! \# s
1 k1 T0 W2 y; |' j% H

  q; g7 w8 s6 s' F5 e# y: m6 Q#PORT="192.168.60.37"
8 ^5 ?. X5 ?2 ?9 B0 i#PORT="localhost"' l) m: j* _$ ?6 c7 F  a' f
HOST, PORT = "192.168.51.28", 5800
- J, h1 n/ |6 ?: _7 ~. r$ z# Create a socket (SOCK_STREAM means a TCP socket)
. b& F* C6 V  Zsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
* q0 _* e8 W  q7 B+ d6 n6 l' E, Ktry:7 P5 j' g4 |; y2 k! F, z( L9 V
    # Connect to server and send data- {- v+ c' z( D9 D' R
    sock.connect((HOST, int(PORT)). C1 a9 e5 l6 h6 i
        #data= "123456789"
/ @$ h1 j; M' e5 V        #s = struct.pack('bbb',1,2,3)
; Q6 r( ~& v! m9 ^5 I1 ~    sock.send(sd.decode('hex'))
! t+ d" a  ~4 X! ^/ H' K    print "Sent1 OK:"
$ ]9 R" m& Z  o  F3 G    print sd
( o8 v1 S9 W- ~4 j    # Receive data from the server and shut down
1 A  E& ^  Q4 h& w2 t; o( ~    received = sock.recv(1024)4 _. c/ x1 s" o, S" z" M
    print "Received:"
7 ~3 V! X4 I8 K$ f7 a+ @    print_hex(received)
3 J# E# ?/ q) \* P. X$ ~7 V5 P    print  'received len is 0x%02x' %(len(received))
/ Y( h/ }/ E& I    print  'received data analysis...'8 \" W* u2 y+ r& I1 U& e0 Z
    re1=received[0:4], I- y$ `2 y% u
    print_hex(re1)
3 e4 c. U4 `4 q3 v1 z' G/ h, l/ N    re1=received[4:6]
0 W8 z9 s8 e  N: i( X    print_hex(re1)
2 Z0 i6 F$ o) N0 d! Z    re1=received[6:10], W5 V& U$ Z, C
    print_hex(re1)* I/ Z' C$ i' C  }
    re1=received[10:16]
8 \5 }' x' ~- |- J4 x; v8 ?% r    print_hex(re1)
& ]( m8 L# R" ~8 }( j) [' r
$ F; d' @. D# n1 r$ z* J    #pack2 send
) V" S$ G/ ~' D6 v  ~    sock.send(sd2.decode('hex')), z! O/ h9 N; ?
    print "Sent2 OK:"5 L- |0 |1 g4 d6 ]# g+ p$ V
    print sd24 j1 ~9 i4 R( \% Y& r8 c' I
    # Receive data from the server and shut down
6 A" ?5 g$ @% R% Q. f    received1 = sock.recv(1024)8 l$ K$ F* t% l; P: e
    print "Received1:"5 o* N7 K! B! U2 S4 w7 T2 {
    print_hex(received1)4 k. w* @5 I7 x. I; h
    print  'received1 len is 0x%02x' %(len(received1))8 Q, ]$ h% d  K0 m! V& G
9 f: f, L  D" s9 H/ N2 G# Q2 J
finally:
! X& S; f/ R% J" L' j    sock.close()
/ K+ ]6 O: d5 O$ p
* k+ ?+ K2 H' }$ ^/ l& Zs=raw_input('press any key to continue...')8 Z6 `* _" z9 l6 C/ n
: f* _! k4 S. s4 a% _* m2 [& c9 f

( N- _2 z9 Z  o+ D7 Z9 p
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-30 13:56 , Processed in 0.404549 second(s), 50 queries .

回顶部