socket 加密传送

序列化对象,作为socket传送内容,为了确保安全,增加公匙:

package serialver.entity;

import java.io.Serializable;
import java.security.PublicKey;

/**
 *  @ version 创建时间:2014-3-21 下午04:46:42
 *
 *  @ author  leicl   
 *
 *  类说明:
 *
 */
public class EncriptEntity implements Serializable{
 /**
  * 根据对象结构自动生成的
  */
 private static final long serialVersionUID = 910786463864347205L;
 byte[] data;
 byte[] sign;
 PublicKey key;
 
 public EncriptEntity(byte[] data, byte[] sign, PublicKey key) {
  super();
  this.data = data;
  this.sign = sign;
  this.key = key;
 }

 public PublicKey getKey() {
  return key;
 }

 public byte[] getData() {
  return data;
 }

 public byte[] getSign() {
  return sign;
 }
 
 
}
加密工具类:

package serialver.util;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;

/**
 *  @ version 创建时间:2014-3-21 下午04:18:21
 *
 *  @ author  leicl   
 *
 *  类说明:
 *
 */
public class DataSign {
 
 public static KeyPair encryptionData() throws NoSuchAlgorithmException, InvalidKeyException{
  KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
  gen.initialize(1024, new SecureRandom());
  KeyPair kayPair = gen.generateKeyPair();
  System.out.println("正在生成密匙......");
  
  return kayPair;
 }

}
客户端程序:

package serialver.client;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;

import serialver.entity.EncriptEntity;
import serialver.util.DataSign;

/**
 *  @ version 创建时间:2014-3-21 下午04:41:28
 *
 *  @ author  leicl   
 *
 *  类说明:
 *
 */
public class EncripClient {
 
 public static void main(String[] args) throws UnknownHostException, IOException, InvalidKeyException, NoSuchAlgorithmException, SignatureException {
  Socket s = new Socket(InetAddress.getLocalHost(), 2323);
  
  ObjectOutput os = new ObjectOutputStream(s.getOutputStream());
  
  FileInputStream fi = new FileInputStream("f:\\apllication.policy");
  byte[] b = new byte[fi.available()];
  fi.read(b);
  KeyPair kayPair = DataSign.encryptionData();
  Signature sign = Signature.getInstance("SHA/DSA");
  
  PrivateKey private1 = kayPair.getPrivate();
  PublicKey public1 = kayPair.getPublic();
  
  sign.initSign(private1);
  sign.update(b);
  
  EncriptEntity en = new EncriptEntity(b, sign.sign(), public1);
  os.writeObject(en);
 }

}
服务器端程序:

package serialver.entity;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Signature;
import java.security.SignatureException;

/**
 *  @ version 创建时间:2014-3-21 下午04:58:23
 *
 *  @ author  leicl   
 *
 *  类说明:
 *
 */
public class EncripServer {
 
 public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
  ServerSocket ser = new ServerSocket(2323);
  
  Socket accept = ser.accept();
  ObjectInput is = new ObjectInputStream(accept.getInputStream());
  
  EncriptEntity en =  (EncriptEntity) is.readObject();
  
  Signature sign = Signature.getInstance("SHA/DSA");
  
  sign.initVerify(en.getKey());
  sign.update(en.getData());
  
  boolean verify = sign.verify(en.getSign());
  if(verify){
   System.out.println("sign is valid....");//说明签名有效
  }else{
   System.out.println("sign is not valid....");//签名无效
  }
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值