java - Verify signature always returns false -


in application server sends signed packet client using udp. packet contains x509encoded public key of server. on receipt of packet, client verifies signature against received data. verify() returns false. following code. please tell me wrong in code.

//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 data signed           sig.update(bdata);           //sign 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() + " " + 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.... 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() + " , " + drp.getbpub() + " pkt. type " + drp.getpkttype());     system.out.println("public key: " + drp.getbpub() + "sign: " + drp.getsign());     system.out.println("sig. matching: " + drp.verifysignature());      ds.close();  } 

}


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -