import java.io.*; import java.security.*; import java.security.cert.*; public class SignHandler { //private static final char[] ksPasswd = {'a', 'b', 'c', 'd', 'e', 'f'}; //private static final char[] keyPasswd = {'f', 'e', 'd', 'c', 'b', 'a'}; public static void main(String[] args) { String keystoreName = args[0]; char[] ksPasswd = args[1].toCharArray(); String alias = args[2]; char[] keyPasswd = args[3].toCharArray(); String data = args[4]; String certificateName = args[5]; String signatureName = args[6]; // Läs in privata nyckeln KeyStore ks = null; PrivateKey privateKey = null; try { ks = KeyStore.getInstance("JKS"); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(keystoreName)); ks.load(bis, ksPasswd); privateKey = (PrivateKey) ks.getKey("pierre", keyPasswd); System.out.println("DONE LOADING KEY FROM KEYSTORE"); } catch(KeyStoreException kse) { System.out.println("KeyStoreException generated"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } catch(CertificateException ce) { System.out.println("CertificateException generated"); } catch(UnrecoverableKeyException uke) { System.out.println("UnrecoverableKeyException generated"); } catch(IOException ioe) { System.out.println("IOException generated"); } // Gör en signnatur och verifikations maskin Signature signAndVerifyEngine = null; try { signAndVerifyEngine = Signature.getInstance("SHA1withDSA", "SUN"); System.out.println("DONE MAKING SIGNATURE/VERIFYING ENGINE"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } catch(NoSuchProviderException nspe) { System.out.println("NoSuchProviderException generated"); } // Initialisera maskinen try { signAndVerifyEngine.initSign(privateKey); } catch(InvalidKeyException ike) { System.out.println("InvalidKeyException generated"); } // Tillför maskinen text-meddelandet try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(data)); byte[] buffer = new byte[1024]; int length; while(bis.available() != 0) { length = bis.read(buffer); signAndVerifyEngine.update(buffer, 0, length); } bis.close(); System.out.println("DONE DELIVERING DATA TO SIGNATURE/VERIFYING ENGINE"); } catch(SignatureException ike) { System.out.println("SignatureException generated"); } catch(IOException ioe) { System.out.println("IOException generated"); } // Signera byte[] signature = null; try { signature = signAndVerifyEngine.sign(); System.out.println("DONE MESSAGE SIGNED"); } catch(SignatureException se) { System.out.println("SignatureException"); } // Spara signaturen try { FileOutputStream fos = new FileOutputStream(signatureName); fos.write(signature); fos.close(); System.out.println("DONE SAVING SIGNATURE"); } catch (IOException ioe) { System.out.println("IOException generated"); } // Extrahera certifikatet och spara detta på fil (för att kunna skicka detta till mottagaren) try { java.security.cert.Certificate cert = ks.getCertificate("pierre"); byte[] encodedCert = cert.getEncoded(); FileOutputStream fos = new FileOutputStream(certificateName); fos.write(encodedCert); fos.close(); System.out.println("DONE EXTRACTING AND SAVING CERTIFICATE"); } catch(KeyStoreException kse) { System.out.println("KeyStoreException generated"); } catch(CertificateException ce) { System.out.println("CertificateException generated"); } catch(IOException ioe) { System.out.println("IOException generated"); } } }