Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 验证签名始终返回false_Java_Cryptography - Fatal编程技术网

Java 验证签名始终返回false

Java 验证签名始终返回false,java,cryptography,Java,Cryptography,在我的应用程序中,服务器使用UDP向客户端发送一个签名数据包。数据包包含服务器的X509编码公钥。收到此数据包后,客户机将根据收到的数据验证签名。我的验证总是返回false。下面是代码。请告诉我代码中有什么错误 //Drply.java public class Drply implements Serializable, Cpacket { private static final long serialVersionUID = 1L; private byte ptype; p

在我的应用程序中,服务器使用UDP向客户端发送一个签名数据包。数据包包含服务器的X509编码公钥。收到此数据包后,客户机将根据收到的数据验证签名。我的验证总是返回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();

} 
}


}

我能发现的一个问题是,您正在调用一个字节[]上的toString。这是没有用的。过去我见过很多关于这方面的问题,但现在我似乎找不到任何问题。答案就在这一行。您必须弄清楚它如何返回true或false,因为签名类没有发布在这里。ret=信号验证信号;
 //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();

}