/* * VerifyHandler.java (för 10.1) * Karl-Adam Karlsson * 06-10-05 * * java VerifyHandler som kontrollerar om (den publika) nyckeln i filen och * signaturen i filen verifierar filen . * * */ import javax.crypto.*; import java.security.*; import java.io.*; public class VerifyHandler{ public VerifyHandler(String dataName, String keyName, String sigName){ verifySig(dataName, sigName, loadKey(keyName)); }//end of constructor /* * Verifierara signaturen aSig mot datafilen dataName givet nyckeln * aPubKey * * @param String dataName, the name of the file containing the data to verify. * @param String sigName, The signature to use during verification * @param PublicKey aPubKey The public key to use for verification * */ private void verifySig(String dataName, String sigName,PublicKey aPubKey){ BufferedInputStream bIn = null; FileInputStream fIn = null; Signature aSig = null; try{ aSig = Signature.getInstance("SHA1withDSA"); aSig.initVerify(aPubKey); fIn = new FileInputStream(dataName); bIn = new BufferedInputStream(fIn); }catch(NoSuchAlgorithmException alEx){ System.err.println("Algorithm Exception"); System.err.println(alEx); System.exit(1); } catch(InvalidKeyException iKey){ System.err.println("Invalid Key Exception Exception"); System.err.println(iKey); System.exit(1); }catch(FileNotFoundException fEx){ System.err.println("File Not Found Exception"); System.err.println(fEx); System.exit(1); } int aLen; byte[] buffLn = new byte[1024]; try{ while ((aLen = fIn.read(buffLn)) != -1) { aSig.update( buffLn ); } boolean verification = aSig.verify( loadSignature(sigName) ); System.out.println("signature verifies: " + verification); }catch(IOException ioe){ System.err.println(ioe); System.exit(1); }catch(SignatureException sigEx){ System.err.println(sigEx); System.exit(1); } }//end of verifySig /* * loadKey * Loads a PublicKey Object from the file named * @param keyFileName Name of file to load key from * @return Key the loaded key. */ private PublicKey loadKey(String keyFileName){ ObjectInputStream oInStr = null; PublicKey theKey = null; try{ FileInputStream inFstream = new FileInputStream(keyFileName); oInStr = new ObjectInputStream(inFstream); }catch(FileNotFoundException fEx){ System.err.print(fEx); System.exit(1); }catch(IOException ioe){ System.err.print(ioe); System.exit(1); } try{ theKey = (PublicKey)oInStr.readObject(); oInStr.close(); }catch(IOException io2){ System.err.print(io2); System.exit(1); }catch(ClassNotFoundException cNf){ System.err.print(cNf); System.exit(1); } return theKey; }//end of loadKey /* * Loads a signature as byte[] form from the file * signame and returns the byte[] representation. * * @param String sigName The name of the file to load the signature from * @return byte[] A bytearray representation of the signature. */ private byte[] loadSignature(String sigName){ byte[] byteArr = null; try{ FileInputStream sIn = new FileInputStream (sigName); byteArr = new byte[sIn.available ()]; sIn.read (byteArr); sIn.close (); }catch(FileNotFoundException fNf){ System.err.print(fNf); System.exit(1); }catch(IOException iOE){ System.err.print(iOE); System.exit(1); } return byteArr; } /* * main * */ public static void main (String[] args) { if(args==null || args.length < 3 ||args.length>3){ System.out.println("Usage: VerifyHandler "); System.out.println("Where is to verify."); System.out.println("Where is the name of keyfile to use for verification."); System.out.println("Where is the signature to use for verification."); } else if(args.length==3&&args[0]!=null&&args[1]!=null&&args[2]!=null){ new VerifyHandler(args[0],args[1],args[2]); } else if(args[0]==null||args[1]==null||args[2]==null){ System.out.println("Usage: VerifyHandler "); System.out.println("Where is to verify."); System.out.println("Where is the name of keyfile to use for verification."); System.out.println("Where is the signature to use for verification."); } }//end of main }//End of class