gpt4 book ai didi

python - 向 HSM 发送命令时出现问题

转载 作者:行者123 更新时间:2023-11-30 22:59:45 24 4
gpt4 key购买 nike

我正在尝试使用 python 代码向 HSM (Thales paysheild 9000) 发送命令。但我从代码中得到的响应并不符合预期。

Input: HEADJA12345678912306 #generate random pin of length 6
Ouput: HEADJA12315

有人可以帮我找出问题所在吗?理想情况下,响应应该包含 JB 而不是我作为输出得到的 JA。

下面是代码。

#!/usr/bin/python
import socket, binascii, string
from struct import *
import time;

TCP_IP = 'localhost'
TCP_PORT = 6511

COMMAND = 'HEADJA12345678912306'

def testPrintable(str):
return all(c in string.printable for c in str)

def buildCommand(command):
hCommand = ''
i = 0
while True:
if (command[i:i+1] == '<'):
i = i + 1
while True:
hCommand = hCommand + binascii.a2b_hex(command[i:i+2])
i = i + 2
if (command[i:i+1] == '>'):
i = i + 1
break
else:
hCommand = hCommand + command[i]
i = i + 1
if (i == len(command)):
break
return hCommand

def main():
global TCP_IP
global TCP_PORT
global COMMAND

connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect((TCP_IP, TCP_PORT))

BUFFER_SIZE = 1024

COMMAND = buildCommand(COMMAND)
SIZE=pack('>h',len(COMMAND))
MESSAGE = SIZE + COMMAND
connection.send(MESSAGE)
data = connection.recv(BUFFER_SIZE)

if (testPrintable(MESSAGE[2:])):
print "sent data (ASCII) :", MESSAGE[2:]
print "sent data (HEX) :", MESSAGE.encode('hex')
if (testPrintable(data[2:])):
print "received data (ASCII):", data[2:]

print "received data (HEX) :", data.encode('hex')
connection.close()

if __name__ == "__main__":
main()

最佳答案

问题已解决,我使用的 header 长度不正确。更正 header 长度后,HSM 使用正确的消息进行响应。

关于python - 向 HSM 发送命令时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com