1 7 8 package org.enhydra.oyster.test; 9 10 import javax.mail.Transport ; 11 import org.enhydra.oyster.smime.SignedAndEnvelopedSMIME; 12 import org.enhydra.oyster.exception.SMIMEException; 13 14 43 public class TestSigEncKeyStore 44 { 45 46 public static void main(String [] args) 47 { 48 49 String subject = "S/MIME signed and enveloped message - Subject test: ÜüÄäÖöÜüß"; 50 String content = "S/MIME signed and enveloped 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 < 8) 56 { 57 System.err.println( 58 System.getProperty("line.separator") + 59 "Usage of TestSigEncKeyStore: " + 60 System.getProperty("line.separator") + 61 "java TestSigned <mailHost> <mailAddress> <cerKeyStoreAlias> " + 62 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 63 "<pfxKeyStoreAlias> [<attachment>]"+ 64 System.getProperty("line.separator") + 65 System.getProperty("line.separator") + 66 "Examples:" + 67 System.getProperty("line.separator") + 68 "java TestSigEncKeyStore together.at recipient@together.at recipient512.cer " + 69 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 70 System.getProperty("line.separator") + 71 "java TestSigEncKeyStore together.at recipient@together.at recipient512.cer " + 72 "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip"); 73 74 System.exit(-1); 75 } 76 77 String smtpHost = args[0]; 78 String addressTO = args[1]; 79 String cerFileName = args[2]; 80 String algorithmName = args[3]; 81 String digestAlgorithm = args[4]; 82 83 boolean includingCert = true; 84 if (args[5].equals("true")) 85 includingCert = true; 86 else 87 includingCert = false; 88 89 boolean includingSignAttrib = true; 90 if (args[6].equals("true")) 91 includingSignAttrib = true; 92 else 93 includingSignAttrib = false; 94 95 String pfxfileName = args[7]; 96 97 String fileName = null; 98 if (args.length > 8) 99 fileName = args[8]; 100 101 String addressCC = "recipient@together.at"; 102 String addressBCC = "recipient@together.at"; 103 104 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 105 args[6] + " " + args[7] + " " + subject; 106 107 SignedAndEnvelopedSMIME see = null; 108 109 try { 110 see = new SignedAndEnvelopedSMIME(smtpHost, from, subject, content, "ISO-8859-1"); 112 113 if (fileName!=null) { 114 see.addAttachment(fileName); } 116 117 see.setReply(from); 119 String alias = cerFileName.replaceAll(".cer", ".pfx"); see.addRecipient(addressTO, "TO", keyStoreFile, see.BKS, password, alias); 124 see.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); see.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); see.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); 128 alias = pfxfileName; see.addSigner(keyStoreFile, see.BKS, password, alias, digestAlgorithm, 130 includingCert, includingSignAttrib); 132 if (algorithmName.equals("RC240")) 133 { 134 System.out.println("Creating signed and encrypted message with RC2 - 40 bits algorithm... "); 135 see.signingAndEnveloping("SIGN_FIRST"); } 138 else if (algorithmName.equals("RC264")) 139 { 140 System.out.println("Creating signed and encrypted message with RC2 - 64 bits algorithm... "); 141 see.signingAndEnveloping(see.RC2_CBC, 64, "SIGN_FIRST"); } 143 else if (algorithmName.equals("RC2128")) 144 { 145 System.out.println("Creating signed and encrypted message with RC2 - 128 bits algorithm... "); 146 see.signingAndEnveloping(see.RC2_CBC, 128, "SIGN_FIRST"); } 148 else if (algorithmName.equals("DES")) 149 { 150 System.out.println("Creating signed and encrypted message with DES algorithm... "); 151 see.signingAndEnveloping(see.DES, 56, "SIGN_FIRST"); } 153 else if (algorithmName.equals("3DES")) 154 { 155 System.out.println("Creating signed and encrypted message with 3DES algorithm... "); 156 see.signingAndEnveloping(see.DES_EDE3_CBC, 192, "SIGN_FIRST"); } 158 159 System.out.print("Sending signed and encrypted message ... "); 160 see.send(); System.out.println("done."); 163 164 } 165 catch (Exception e) { 166 SMIMEException.setErrorFilePath("Log"); if(e instanceof SMIMEException) { 168 SMIMEException eTmp = (SMIMEException)e; 169 eTmp.displayErrors(null); 171 e = eTmp.getNonSMIMEException(); 172 if(e != null) 173 e.printStackTrace(); 174 } 175 else { 176 e.printStackTrace(); 177 } 178 } 179 } 180 } | Popular Tags |