序列化对象,作为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....");//签名无效
}
}
}