samples中hash函数代码: public List<byte[]> hash(String plaintext) { TPM_ALG_ID hashAlgs[] = new TPM_ALG_ID[] { TPM_ALG_ID.SHA1, TPM_ALG_ID.SHA256, TPM_ALG_ID.SHA384 }; // first demonstrate non-sequence hashing (for short sequences) byte[] toHash = plaintext.getBytes(); List<byte[]> arr=new ArrayList<>(); int i=0; //write("Simple hashing of " + Helpers.toHex(toHash)); for (TPM_ALG_ID h : hashAlgs) { HashResponse r = tpm.Hash(toHash, h, TPM_HANDLE.NULL); //write(" " + h.toString() + " -- " + Helpers.toHex(r.outHash)); arr.add(r.outHash); //check the hash is good byte[] sofwareHash = Crypto.hash(h, toHash); if (!Helpers.arraysAreEqual(r.outHash, sofwareHash)) throw new RuntimeException("Hash is wrong!"); } return arr; } |
通信客户端client代码: import java.io.*; import java.net.*; import java.util.List; import java.util.Scanner; import samples.Samples; import tss.Helpers; public class Client { public static void main(String[] a) throws IOException { Scanner sc = new Scanner(System.in); Socket mysocket; InputStream in = null; OutputStream out = null; byte[] fn = new byte[1024]; try { mysocket = new Socket("127.0.0.1",2020); System.out.println("服务器连接成功!"); Samples s = new Samples(); List<byte[]> arr=s.hash("HELLO WORLD."); in = mysocket.getInputStream(); out = mysocket.getOutputStream(); fn = "hello world.".getBytes(); System.out.println("要发送的信息:"+"hello world."); System.out.println("信息转化后的hash:"); for(int j=0;j<3;j++){ System.out.println(Helpers.toHex(arr.get(j))); } out.write(fn); String tam; for(int i=0;i<arr.size();i++) { tam=new String(Helpers.toHex(arr.get(i))); tam=tam.substring(0,tam.length()-11); fn = tam.getBytes(); out.write(fn); fn = new byte[1024]; } System.out.println("传输中!"); int i; byte[] ft = new byte[1024]; for(int j=0;j<2;j++){ if((i = in.read(ft)) != -1) { System.out.println(new String(ft, 0, i)); ft = new byte[1024]; } } for(int j=0;j<2;j++){ if((i = in.read(ft)) != -1) { System.out.println(new String(ft, 0, i)); } } } catch(Exception e) { System.out.println("服务器已断开" + e); } finally { in.close(); out.close(); } } } 通信服务器server代码: import java.io.*; import java.net.*; public class Server { public static void main(String[] a) throws IOException { ServerSocket server = null; Socket mysocket = null; InputStream in = null; OutputStream out = null; System.out.println("等待客户呼叫"); try { server = new ServerSocket(2020); } catch (IOException e1) { System.out.println(e1); } try { mysocket = server.accept(); out = mysocket.getOutputStream(); in = mysocket.getInputStream(); byte[] fn = new byte[1024]; int i = in.read(fn); String message =new String(fn, 0, i); String back =new String("收到明文" + " \\ " + message+"\n"); System.out.println("收到明文" + " \\ " + message); out.write(back.getBytes()); String[] arr=new String[3]; byte[] fl = new byte[1024]; for(int j =0;j<3;j++){ i = in.read(fl); arr[j]=new String(fl, 0, i); String gethash = "收到hash:" + arr[j]+"\n"; System.out.println("收到hash:" + arr[j]); out.write(gethash.getBytes()); fl = new byte[1024]; } } catch (Exception e) { System.out.println("客户已断开" + e); }finally { in.close(); out.close(); } } } 结果: Server: Client: |
TPM程序设计基础
于 2022-05-09 21:53:33 首次发布