Get Response from HSM using Java

Jul 28, 2014 at 10:03 AM
I am using Java to connect to the HSM on Windows 2008
I am sending a 'GC' command for getting clear components of key.
I am expecting receive something like
Clear component: XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX 
Encrypted component: U XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX 
Key check value: XXXXXX
The answer from HSM is:
Output from HSM : 000b3015

The code is below:
public class TestHSMJava {
public static void main(String args[]) {
    System.out.println("<<< Main Method Entry >>>");
    String command = null;
    Socket socket = null;
    DataOutputStream out = null;
    DataInputStream in = null;
    byte[] b= new byte[100];
    try {
        socket = new Socket("10.10.10.10", 7500);
        System.out.println("<<< Socket >>> :" + socket);
        if (socket != null) {
            System.out.println("<<< Connected to HSM  >>>:"
                    + socket.isConnected());
            in = new DataInputStream (new BufferedInputStream(socket.getInputStream()));
            out = new DataOutputStream (new BufferedOutputStream(socket.getOutputStream()));
            command = "000b3030303041303030303255";
            out.writeUTF(command);
            System.out.println("Input to HSM : " +command);
            out.flush();
            String response = in.readUTF();
            System.out.println("Output from HSM : " +response);
            System.out.println("");
        }
    }
}
Editor
Aug 5, 2014 at 12:07 PM
Edited Aug 5, 2014 at 12:09 PM
Hi!

If you are using java.io.DataOuputStream, it adds software header itself and uses String. Try to send command as follows:
command = "0000A00002U";
out.writeUTF(command);
out.flush();
String response = in.readUTF();
You should receive the answer like:
A100UXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXKKKKKK
Where:

UXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX is a key
KKKKKK is a key check value

Regards,
Juris
Marked as answer by Manshtein on 8/21/2014 at 6:34 AM