从python程序调用dll来执行会话解锁

有了DLL,您可以使用类似DependencyWalker的工具查看DLL的导出符号。但是如果您已经知道您的DLL在CANoe中工作,那么它将遵循指定的API by Vector Informatik在CANoe的安全访问DLL中实现:GenerateKeyx&;GenerateKeyXopt 

GenerateKeyx:

int VKeyGenResult ExGenerateKeyEx (
  const unsigned char* ipSeedArray,
  unsigned int iSeedArraySize,
  const unsigned int iSecurityLevel,
  const char* ipVariant,
  unsigned char* iopKeyArray,
  unsigned int iMaxKeyArraySize,
  unsigned int& oActualKeyArraySize );
GenerateKeyXOpt:

VKeyGenResult ExOptGenerateKeyExOpt (
  const unsigned char* ipSeedArray,
  unsigned int iSeedArraySize,
  const unsigned int iSecurityLevel,
  const char* ipVariant,
  const char* ipOptions,
  unsigned char* iopKeyArray,
  unsigned int iMaxKeyArraySize,
  unsigned int& oActualKeyArraySize );

然后,只需从python调用此Dll,即使用ctypes

import ctypes

mylib = ctypes.WinDLL("./GenerateKeyExImpl.dll")
seed = (ctypes.c_byte * 4)(0xff, 0xfe, 0xfd, 0xfc) # these bytes you should get from the ECU i.e.
# Tx 27 01
# Rx 67 01 ff fe fd fc
key = (ctypes.c_byte * 4)() # this will contain the secret key after Dll call
keylength = ctypes.c_int(4) # this will contain the secret key length after Dll call
mylib.ExGenerateKeyEx(
     ctypes.pointer(seed), # Seed from the ECU
     ctypes.c_int(4), # Example: Seed length = 4 bytes
     ctypes.c_int(1), # Example: Security Level 1
     POINTER(c_int)(), # Example: NULL = No variant string
     ctypes.pointer(key), # Key to send back to the ECU
     ctypes.c_int(4), # Example: Key Max length = 4 bytes
     ctypes.pointer(keylength), # Example: Seed length = 4 bytes
)
# TODO: Send "key" back to the ECU i.e.
# Tx 27 02 XX XX XX XX
# Rx 67 02   

 #dll必须和python平台匹配,比如都是32位的或者都是64位的

案例:安全算法加载 dll 出seed key的方法:

import ctypes
from ctypes import *
file_path = r'D:\project\***\data\GenerateKeyEx.dll'
mylib = ctypes.WinDLL(file_path)
seed = (ctypes.c_byte * 4)(0x7B,0x74,0xC6,0x35)
key = (ctypes.c_byte * 4)()
keylength = ctypes.c_int(4)
mylib.GenerateKeyEx(
     ctypes.pointer(seed), # Array for the seed [in]
     ctypes.c_short(4), # Length of the array for the seed [in]
     ctypes.c_int(1), # Security level [in]
     POINTER(c_int)(), # Name of the active variant [in] 
     ctypes.pointer(key), # Array for the key [in, out] 
     ctypes.c_int(4), # Maximum length of the array for the key [in]
     ctypes.pointer(keylength), # Length of the key [out] 
)
print(key[0]&0xFF,key[1]&0xFF,key[2]&0xFF,key[3]&0xFF)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值