RSA加密Socket传输文件、签名(三)

接下来是客户端类CoderClient的编写。

1.首先配置如下信息,包括服务器地址、端口、密钥文件位置.

conf.properties

 

  1. #configure private key on server  
  2. rsa_private=d:/rsa1_private.key  
  3. #public key from client  
  4. rsa_public=d:/rsa_public.key  
  5. #server  
  6. server=127.0.0.1  
  7. #server port  
  8. port=888  

 

 

 

2.编写发送文件的方法,其中byte[] buf=new byte[117]的原因是RSA加密算法支持的最大字节数为117,而加密后变成128位,所以服务器端解密的时候可以使用128bytes的buf读取文件。

 

  1. public boolean send(String filename){  
  2.         try {  
  3.             File f = new File(filename);  
  4.             fis = new FileInputStream(filename);  
  5.             byte[] buf = new byte[117];  
  6.             int available;  
  7.             socket = new Socket(server,port);  
  8.             os = socket.getOutputStream();  
  9.             DataOutputStream dos = new DataOutputStream(os);  
  10.             dos.writeUTF(f.getName()); //写入文件名   
  11.             dos.writeLong(f.length()); //写入文件大小   
  12.             while((available = fis.read(buf)) != -1){  
  13.                 byte[] availableBytes = null;  
  14.                 if(available == buf.length){  
  15.                     availableBytes = buf;  
  16.                 }else{  
  17.                     availableBytes = new byte[available];  
  18.                     for (int i = 0; i < available; i++) {  
  19.                         availableBytes[i] = (byte) buf[i];  
  20.                     }  
  21.                 }  
  22.                 byte[] cipherText = coder.encrypt(availableBytes);  
  23.                 byte[] signature = sign.getSignature(cipherText);  
  24.                 os.write(signature);  
  25.                 os.write(cipherText);  
  26.                 //count += cipherText.length;   
  27.                 //System.out.println("send "+count+" bytes");   
  28.             }  
  29.             socket.shutdownOutput();  
  30.             fis.close();  
  31.             return true;  
  32.         } catch (FileNotFoundException e) {  
  33.             log.error("文件"+filename+" 未找到");  
  34.         } catch (UnknownHostException e) {  
  35.             log.error("未知主机:"+server);  
  36.         } catch (IOException e) {  
  37.             log.error("文件读/写错误,可能是因为远程主机无响应");  
  38.         }  
  39.         return false;  
  40.     }  

 

 

 

 

3.为了测试多线程处理效果,可以将客户端继承于Thread,在重写的run方法中调用 send(String file)方法

 

  1. @Override  
  2.     public void run() {  
  3.         send(this.file);  
  4.     }  

 

 

 

4.main中的测试可以这样来模拟多个客户端同时向服务器传输文件

 

  1. File dir = new File("E://files//demo//");  
  2.         File[] files = dir.listFiles();  
  3.           
  4.         for(int i=0; i<files.length; i++){  
  5.             Thread th = new Thread(new CoderClient(files[i].getAbsolutePath()));  
  6.             th.start();  
  7.         }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值