java - Exception while trying to verify a digital signature -
i run java program verify digital signature
package com.cryptography; import java.io.bufferedinputstream; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.security.invalidkeyexception; import java.security.keyfactory; import java.security.nosuchalgorithmexception; import java.security.publickey; import java.security.signature; import java.security.signatureexception; import java.security.spec.invalidkeyspecexception; import java.security.spec.x509encodedkeyspec; public class verifydkimsignature { public static void main(string[] args) { fileinputstream fis; try { //read encoded public key bytes fis = new fileinputstream ("/home/src/com/cryptography/dkimpublickey"); byte[] enckey = new byte[fis.available()]; fis.read(enckey); fis.close(); //generate public key x509encodedkeyspec pubkeyspec = new x509encodedkeyspec(enckey); keyfactory keyfactory = keyfactory.getinstance("rsa"); publickey pubkey = keyfactory.generatepublic(pubkeyspec); //read signature bytes file bufferedinputstream bis = new bufferedinputstream (new fileinputstream ("/home/src/com/cryptography/signature")); byte[] signaturebytes = new byte[bis.available()]; bis.read(signaturebytes); //initialise signature instance signature sig = signature.getinstance("sha256withrsa"); sig.initverify(pubkey); //supply signature object data verification bis = new bufferedinputstream (new fileinputstream ("/home/src/com/cryptography/samplefile.txt")); byte[] origfile = new byte[1024]; int len = 0; while(bis.available() != 0) { len = bis.read(origfile); sig.update(origfile, 0, len); } boolean isverifies = sig.verify(signaturebytes); system.out.println("signature verifies::"+isverifies); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (nosuchalgorithmexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (invalidkeyspecexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (invalidkeyexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (signatureexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
when execute it, following exception
java.security.signatureexception: signature encoding error @ sun.security.rsa.rsasignature.engineverify(unknown source) @ java.security.signature$delegate.engineverify(unknown source) @ java.security.signature.verify(unknown source) @ com.cryptography.verifydkimsignature.main(verifydkimsignature.java:54) caused by: java.io.ioexception: objectidentifier mismatch: 1.3.14.3.2.26 @ sun.security.rsa.rsasignature.decodesignature(unknown source) ... 4 more
can please explain reason error?
it turns out "1.3.14.3.2.26" (from ioexception) oid sha-1 algorithm. mistake have done here using different algorithms verification , signing i.e have used sha1withrsa signing , sha256with rsa verification. got resolved once changed algorithm @ verification end sha1withrsa
Comments
Post a Comment