栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

TPM程序设计基础

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TPM程序设计基础

samples中hash函数代码:

    public List 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 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 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

                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:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872004.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号