我正在尝试使用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()

最佳答案

HSM的设计方式是,如果它接收到不在列表中的任何命令,它将不会提供任何适当的输出。这样做是为了提高安全性并防止外部黑客入侵。就像您的情况一样,您对所发生的事情一无所知。幸运的是,问题出在标题长度上。但是在某些情况下,可能是其他任何问题,HSM不会提供正确的错误代码。

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

10-13 09:06