📄 packet.java.svn-base
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package mytools;import mytools.DSA;import java.io.*;import java.security.*;import java.util.Arrays;/** * * @author DJ尐舞 */public class Packet implements Serializable { /** * 普通消息 */ transient public static final String Message = "msg"; /** * 签名认证的消息 */ transient public static final String SignedMessage = "signedmsg"; /** * 登陆信息 */ transient public static final String LoginMessage = "login"; /** * 回复登陆信息 */ transient public static final String ResponeLogin = "responelogin"; /** * 注册信息的消息 */ transient public static final String RegisterMessage = "reg"; /** * 公钥包 */ transient public static final String PublicKey = "pubkey"; /** * 私钥包 */ transient public static final String PrivateKey = "prikey"; /** * 包类型 */ private String type; /** * 用户名 */ private byte[] userName; /** * 包内容 */ private byte[] content; /** * 签名内容 */ private byte[] sign; /** * 消息验证摘要 */ private byte[] FCS; /** * 摘要算法对象 */ private transient MessageDigest encodeMethod; /** * 返回包内容的字符串 * @return */ public String getStringContent() { return new String(content); } /** * 包构造函数 * @param userName 用户名 * @param content 包的内容 * @param type 包的类型 */ public Packet(String type, String userName, byte[] content) { this.type = type; this.userName = userName.getBytes(); this.content = content; try { encodeMethod = MessageDigest.getInstance("sha-1"); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); System.out.println("找不到算法"); } encodeMethod.update(this.userName); encodeMethod.update(this.getContent());//对内容摘要 //如果是签名消息包则对签名进行摘要 if (type.equals(Packet.SignedMessage)||type.equals(Packet.LoginMessage)) { System.out.print(this.getStringContent()); try { this.sign = DSA.sign(this.getContent(),userName); //进行数字签名 } catch (Exception ex) { ex.printStackTrace(); System.out.println("签名失败"); } encodeMethod.update(sign); } FCS = encodeMethod.digest();//添加签名进行摘要,并返回摘要结果 } /** * 包构造函数 * @param content 包的内容 * @param type 包的类型 */ public Packet(String type, byte[] content) { this(type, "", content); } /** * 获取包类型 * @return 返回包类型 */ public String getType() { return new String(this.type); } /** * 获取用户名 * @return */ public String getUserName() { return new String(this.userName); } /** * 获取包内容 * @return 包内容的字节数组 */ public byte[] getContent() { return content; } /** * 验证消息完整性 * @return 包是否完整 */ public boolean isIntegrated() { byte[] newFCS; try { encodeMethod = MessageDigest.getInstance("sha-1"); } catch (NoSuchAlgorithmException ex) { ex.printStackTrace(); } encodeMethod.update(this.userName); encodeMethod.update(this.getContent());//对内容摘要 //如果是签名消息包则对签名进行摘要 if (this.type.equals(Packet.SignedMessage)||this.type.equals(Packet.LoginMessage)) { encodeMethod.update(sign);//添加签名进行摘要,并返回摘要结果 } newFCS = encodeMethod.digest(); if (Arrays.equals(newFCS, FCS)) { return true; } else { return false; } } /** * 身份认证 * @param encodedPubKey 经过X509编码的公钥字节数组 * @return 身份认证结果 */ public boolean isSignedRight(byte[] encodedPubKey) { return DSA.validate((byte[]) content, sign, encodedPubKey); } /** * 将对象转为字节数组 * @param obj 对象 * @return 字节数组 */ public static byte[] ObjectToByte(Object obj) { byte[] bytes = null; try { //object to bytearray ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(obj); bytes = bo.toByteArray(); bo.close(); oo.close(); } catch (Exception e) { System.out.println("translation" + e.getMessage()); e.printStackTrace(); } return bytes; } /** * 将字节数组转换为对象 * @param bytes 字节数组 * @return 对象 */ public static Object ByteToObject(byte[] bytes) { Object obj = null; try { //bytearray to object ByteArrayInputStream bi = new ByteArrayInputStream(bytes); ObjectInputStream oi = new ObjectInputStream(bi); obj = oi.readObject(); bi.close(); oi.close(); } catch (Exception e) { System.out.println("translation" + e.getMessage()); e.printStackTrace(); } return obj; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -