java - how to read private key for digital signature -
i building application signing files digitally, done tow parts signing , verification, want build method read private key, , sign file. because not want use different key every time when signing something.
// here method reading private key class pk implements serializable{ privatekey pk = null; public pk(string nazivfajla) throws filenotfoundexception, ioexception { this.pk = privatni; fileoutputstream fos = new fileoutputstream(nazivfajla); objectoutputstream oos = new objectoutputstream(fos); oos.writeobject(this.pk); oos.close(); fos.close(); } public privatekey getpk(string nazivfajla) throws filenotfoundexception, ioexception, classnotfoundexception { fileinputstream fis = new fileinputstream(nazivfajla); objectinputstream ois = new objectinputstream(fis); this.pk = (privatekey) ois.readobject(); return pk; }
and here method in button field:
private void ucitajprivatniactionperformed(java.awt.event.actionevent evt) { try { jfc.showopendialog(jfc); string fajlzaprivatnikljuc = jfc.getselectedfile().getabsolutepath(); pk pk = new pk(fajlzaprivatnikljuc); privatni = pk.getpk(fajlzaprivatnikljuc); this.tick_uf2.setvisible(true); } catch (ioexception | classnotfoundexception ex) { logger.getlogger(gui.class.getname()).log(level.severe, null, ex); } }
this signing method:
private void potpisi1actionperformed(java.awt.event.actionevent evt) { try { //alisa bira algoritme - izabrala je rsa sa sha-1 signature dsa = signature.getinstance("sha1withrsa"); try{ dsa.initsign(privatni); }catch(exception e){ system.out.println("crashed here!"); } fileinputstream poruka = new fileinputstream(fajlzapotpis); bufferedinputstream bufporuka = new bufferedinputstream(poruka); byte[] buffer = new byte[1024]; int len; while (bufporuka.available() != 0) { len = bufporuka.read(buffer); dsa.update(buffer, 0, len); } bufporuka.close(); poruka.close(); //alisa generiše digitalni potpis pozivajući metod sign() byte[] digitalnipotpis = dsa.sign(); jfc.showsavedialog(jfc); sacuvaj(digitalnipotpis, jfc.getselectedfile().getabsolutepath()); this.tick_uf3.setvisible(true); } catch (nosuchalgorithmexception ex) { logger.getlogger(gui.class.getname()).log(level.severe, null, ex); } catch (filenotfoundexception ex) { logger.getlogger(gui.class.getname()).log(level.severe, null, ex); } catch (ioexception ex) { logger.getlogger(gui.class.getname()).log(level.severe, null, ex); } catch (signatureexception ex) { logger.getlogger(gui.class.getname()).log(level.severe, null, ex); }
and here mistake after trying sign:
мај 14, 2015 8:09:55 pm dp.gui potpisi1actionperformed severe: null java.security.signatureexception: object not initialized signature or verification @ java.security.signature.update(signature.java:738) @ dp.gui.potpisi1actionperformed(gui.java:553) @ dp.gui.access$1500(gui.java:46) @ dp.gui$16.actionperformed(gui.java:306) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2346) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6525) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321) @ java.awt.component.processevent(component.java:6290) @ java.awt.container.processevent(container.java:2234) @ java.awt.component.dispatcheventimpl(component.java:4881) @ java.awt.container.dispatcheventimpl(container.java:2292) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4898) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4533) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4462) @ java.awt.container.dispatcheventimpl(container.java:2278) @ java.awt.window.dispatcheventimpl(window.java:2739) @ java.awt.component.dispatchevent(component.java:4703) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:746) @ java.awt.eventqueue.access$400(eventqueue.java:97) @ java.awt.eventqueue$3.run(eventqueue.java:697) @ java.awt.eventqueue$3.run(eventqueue.java:691) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:86) @ java.awt.eventqueue$4.run(eventqueue.java:719) @ java.awt.eventqueue$4.run(eventqueue.java:717) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:75) @ java.awt.eventqueue.dispatchevent(eventqueue.java:716) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82)
please if know answer please me, junior programmer, , need this.. in advance!
private keys should not encoded using object serialization. private keys can encoded using getencoded()
method , read in using pkcs8encodedkeyspec
supplied "rsa"
keyfactory
instance.
Comments
Post a Comment