1 7 8 package org.enhydra.oyster.test; 9 10 import org.enhydra.oyster.smime.SignedSMIME; 11 import org.enhydra.oyster.exception.SMIMEException; 12 import javax.mail.Transport ; 13 import java.io.FileInputStream ; 14 import java.io.File ; 15 16 44 public class TestSignedKeyStore 45 { 46 47 public static void main(String [] args) 48 { 49 String subject = "S/MIME signed message - Subject test: ÜüÄäÖöÜüß"; 50 String content = "S/MIME signed message example\r\nContent test: ÜüÄäÖöÜüß!"; 51 String from = "sender@together.at"; 52 String password = "together"; 53 String keyStoreFile = "keystore.ks"; 54 55 if (args.length < 7) 56 { 57 System.err.println( 58 System.getProperty("line.separator") + 59 "Usage of TestSignedKeyStore: " + 60 System.getProperty("line.separator") + 61 "java TestSigned <mailHost> <mailAddress> <digestAlgorithm> " + 62 "<includingCert> <includingSignAttrib> <pfxKeyStoreAlias> <externalSigning>" + 63 "[<attachment>]" + 64 System.getProperty("line.separator") + 65 System.getProperty("line.separator") + 66 "Examples:" + 67 System.getProperty("line.separator") + 68 "java TestSignedKeyStore together.at recipient@together.at SHA1_WITH_RSA true " + 69 "true sender512.pfx true"+ 70 System.getProperty("line.separator") + 71 "java TestSignedKeyStore together.at recipient@together.at SHA1_WITH_DSA true " + 72 "true senderDSA512.pfx false .\\test\\Zip8Test.zip"); 73 System.exit(-1); 74 } 75 76 String smtpHost = args[0]; 77 String addressTO = args[1]; 78 String digestAlgorithm = args[2]; 79 80 boolean includingCert = true; 81 if (args[3].equalsIgnoreCase("true")) 82 includingCert = true; 83 else 84 includingCert = false; 85 86 boolean includingSignAttrib = true; 87 if (args[4].equalsIgnoreCase("true")) 88 includingSignAttrib = true; 89 else 90 includingSignAttrib = false; 91 92 String pfxfileName = args[5]; 93 94 boolean externalSigning = true; 95 if (args[6].equalsIgnoreCase("true")) 96 externalSigning = true; 97 else 98 externalSigning = false; 99 100 String fileName = null; 101 if (args.length > 7) 102 fileName = args[7]; 103 104 String addressCC = "recipient@together.at"; 105 String addressBCC = "recipient@together.at"; 106 107 subject = digestAlgorithm + " " + args[3] + " " + args[4] + " " + 108 pfxfileName + " " + args[6] + " " + subject; 109 110 SignedSMIME ss = null; 111 112 try { 113 114 ss = new SignedSMIME(smtpHost, from, subject, content, "ISO-8859-1"); 116 117 if (fileName!=null) { 118 ss.addAttachment(fileName); } 120 121 ss.setReply(from); 123 ss.addRecipient(addressTO, "TO"); 127 ss.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); ss.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); ss.setCapabilities("SIGNATURE", 1, 2, 3, 4, 0); 131 String alias = pfxfileName; ss.addSigner(keyStoreFile, ss.BKS, password, alias, digestAlgorithm, 133 includingCert, includingSignAttrib); 135 System.out.println("Creating signed message with " + digestAlgorithm + " algorithm ... "); 136 ss.signing(externalSigning); 137 System.out.print("Sending signed message ... "); 138 ss.send(); System.out.println("done."); 141 } 142 catch (Exception e) { 143 SMIMEException.setErrorFilePath("Log"); if(e instanceof SMIMEException) { 145 SMIMEException eTmp = (SMIMEException)e; 146 eTmp.displayErrors(null); 148 e = eTmp.getNonSMIMEException(); 149 if(e != null) 150 e.printStackTrace(); 151 } 152 else { 153 e.printStackTrace(); 154 } 155 } 156 } 157 } | Popular Tags |