TPM程序设计基础

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值