4 回答

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個贊
您需要 Luna 客戶端到應(yīng)用程序以連接 HSM 以處理加密操作。Luna 客戶端包含客戶端與 HSM 通信所需的庫。

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個贊
您可以使用 safenet SDK 開發(fā)您的加密功能,這些功能可以與 Java 中的 HSM 進(jìn)行交互。例如:Gemalto HSM 為 Java 開發(fā)人員提供 JSP 和 JCProv API 作為 SDK 的一部分。

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個贊
以下命令顯示如何向 Thales HSM 發(fā)送命令。
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
public class ThalesHSMConnect2 {
//@formatter:off
public static final String send(final String command) throws UnknownHostException, IOException {
try(final Socket sc = new Socket(host, port);
final DataInputStream din = new DataInputStream(sc.getInputStream());
final DataOutputStream dos = new DataOutputStream(sc.getOutputStream())) {
sc.setSoTimeout(5000);
dos.writeUTF(command);
dos.flush();
final String response = din.readUTF();
return response;
}
}
public static final byte[] send(final byte[] command) throws Exception {
try(Socket sc = new Socket(host, port);
InputStream in = sc.getInputStream();
OutputStream os = sc.getOutputStream()) {
sc.setSoTimeout(5000);
command[0] = (byte) ((command.length-2)/256); //two byte command length
command[1] = (byte) ((command.length-2)%256); //two byte command length
os.write(command);
os.flush();
final byte b1 = (byte) in.read();
final byte b2 = (byte) in.read();
if(b1 < 0 || b2 < 0) throw new SocketTimeoutException("no response from hsm.");
final byte[] response = new byte[b1*256+b2];
in.read(response);
return response;
}
}
public static void main(String[] args) throws IOException {
final String cvvGenerationResponse = send("0000CWAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB4484070020000310;2105000");
}
}

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個贊
以下代碼顯示了如何準(zhǔn)備并向 safenet HSM 發(fā)送命令。
public static final String send(String command) {
try (Socket socket = new Socket(HSMIP, HSMPORT);
InputStream in = socket.getInputStream();
OutputStream os = socket.getOutputStream()) {
byte[] commandbytes = DatatypeConverter.parseHexBinary(command);
byte[] request = new byte[6 + commandbytes.length];
request[0] = 0x01; //constant as per setting during installation
request[1] = 0x01; //constant as per setting during installation
request[2] = 0x00; //constant as per setting during installation
request[3] = 0x00; //constant as per setting during installation
request[4] = (byte) (commandbytes.length / 256); //length of command
request[5] = (byte) (commandbytes.length % 256); //length of command
System.arraycopy(commandbytes, 0, request, 6, commandbytes.length);
//logger.info("request : " + DatatypeConverter.printHexBinary(request));
os.write(request);
os.flush();
byte[] header = new byte[6];
in.read(header);
logger.info("header : " + DatatypeConverter.printHexBinary(header));
int len = (header[4] & 0xFF) * 256 + (header[5] & 0xFF); //length of response
logger.info("len : " + len);
byte[] response = new byte[len];
in.read(response);
logger.info("response : " + DatatypeConverter.printHexBinary(response));
return DatatypeConverter.printHexBinary(response);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
添加回答
舉報(bào)