验证签名始终返回false

时间:2014-04-06 17:35:42

标签: java cryptography

在我的应用程序服务器中使用UDP向客户端发送签名数据包。数据包包含服务器的X509Encoded公钥。收到此数据包后,客户端将根据收到的数据验证签名。我的verify()总是返回false。以下是代码。请告诉我代码中有什么问题。

//Drply.java    
public class Drply   implements Serializable, Cpacket {

private static final long serialVersionUID = 1L;
private byte ptype;
private String name;
private byte[] bpub;
private String ip;
private byte[] bsign;

public Drply(String n, byte[] bp, String i, PrivateKey prk) throws UnsupportedEncodingException {

    name = n;
    bpub = bp;
    ip = i;
    ptype = (byte)2;

    bsign = genSignature(new String(name + bpub + ip + ptype).getBytes("UTF-8"), prk);
}

public byte[] genSignature(byte[] bdata, PrivateKey prk) {
    byte[] bsign = null;  
    try {
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initSign(prk);  
        //update signature with data to be signed  
        sig.update(bdata);  
        //sign the data  
        bsign = sig.sign();  
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    }
    return bsign;
}

public boolean verifySignature( ) throws InvalidKeySpecException, UnsupportedEncodingException {
    boolean ret = false;
    try {
        X509EncodedKeySpec pkeyenc = new X509EncodedKeySpec(bpub);
        KeyFactory kfy = KeyFactory.getInstance("RSA");
        PublicKey pbk = kfy.generatePublic(pkeyenc);

        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initVerify(pbk);

        sig.update(new String(name.trim() + bpub + ip.trim() + ptype).getBytes("UTF-8"));
        ret = sig.verify(bsign);

        System.out.println("Sig. matching: " + ret );
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    }
    return ret;
}

//Server.java

public class TestServer {

public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    KeyPairGenerator kg;
    kg = KeyPairGenerator.getInstance("RSA");
    kg.initialize(1024);
    KeyPair kp = kg.generateKeyPair();
    PrivateKey pvk = kp.getPrivate();
    PublicKey pbk = kp.getPublic();
    X509EncodedKeySpec pkeyenc = new X509EncodedKeySpec(pbk.getEncoded());
    byte[] bpubKey= pkeyenc.getEncoded(); 


    InetAddress ip = InetAddress.getByName("localhost");
    DatagramSocket ds = new DatagramSocket(new InetSocketAddress(ip, 6000));
    System.out.println("Waiting....");  
    byte rcvBuf[] = new byte[500];
    DatagramPacket dp = new DatagramPacket(rcvBuf, rcvBuf.length);
    ds.receive(dp);

    ByteArrayInputStream bis = new ByteArrayInputStream(rcvBuf);
    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bis));
    Drqst drqst = null;

    Cpacket cp = (Cpacket)ois.readObject();
    drqst =   (Drqst) cp;
    bis.close();

    System.out.println("Received packet type: " +  cp.getPktType());
    System.out.println("Received: " + drqst.getName() + " with " + drqst.getBpub()+ " packet type: " +  drqst.getPktType()  );


    System.out.println("Sending reply");

    Drply drply = new Drply("Hi " + drqst.getName(), bpubKey, "192.168.100.200", pvk);
    System.out.println("Public key: " + bpubKey + "Sign: " + drply.getSign());

    ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
    oos.flush();
    oos.writeObject(drply);
    oos.flush();                        

    bos.close();

    DatagramPacket ndp = new DatagramPacket(bos.toByteArray(), bos.toByteArray().length, dp.getAddress(), dp.getPort());
    ds.send(ndp);

    System.out.println("Reply sent.");

    ds.close();

} 

}

 //client.java

 public class TestClient {

public static void main(String[] args) throws IOException, ClassNotFoundException, DataException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
    InetAddress ip = InetAddress.getByName("localhost");
    DatagramSocket ds = new DatagramSocket();

    KeyPairGenerator kg;
    kg = KeyPairGenerator.getInstance("RSA");
    kg.initialize(1024);
    KeyPair kp = kg.generateKeyPair();
    PrivateKey pvk = kp.getPrivate();
    PublicKey pbk = kp.getPublic();
    X509EncodedKeySpec pkeyenc = new X509EncodedKeySpec(pbk.getEncoded());
    byte[] bpubKey= pkeyenc.getEncoded(); 

    Drqst drqst = new Drqst("abc", bpubKey);

    ByteArrayOutputStream bos = new ByteArrayOutputStream(500);
    ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(bos));
    oos.flush();
    oos.writeObject(drqst);
    oos.flush();
          //retrieves byte array
    byte[] sendBuf = bos.toByteArray();
    DatagramPacket dp = new DatagramPacket(sendBuf, sendBuf.length, ip, 6000);
    ds.send(dp);
    oos.close(); 


    System.out.println("Waiting.... for data"); 
    byte rcvBuf[] = new byte[1000]; 
    DatagramPacket ndp = new DatagramPacket(rcvBuf, rcvBuf.length);
    ds.receive(ndp);

    Drply drp = null;
    ByteArrayInputStream bis = new ByteArrayInputStream(rcvBuf);
    ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bis));
    drp = (Drply)ois.readObject();
    bis.close();

    System.out.println("Received pkt: " + drp.getName() + " having " + drp.getIp() + " and " + drp.getBpub() + " with Pkt. type " + drp.getPktType());
    System.out.println("Public key: " + drp.getBpub() + "Sign: " + drp.getSign());
    System.out.println("Sig. matching: " + drp.verifySignature());

    ds.close();

}

}

0 个答案:

没有答案