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 36 public class TestEncSig 37 { 38 39 public static void main(String [] args) 40 { 41 42 String subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß"; 43 String content = "S/MIME enveloped and signed message example\r\nContent test: ÜüÄäÖöÜüß!"; 44 String from = "sender@together.at"; 45 String password = "together"; 46 47 if (args.length < 8) 48 { 49 System.err.println( 50 System.getProperty("line.separator") + 51 "Usage of TestEncSig: " + 52 System.getProperty("line.separator") + 53 "java TestEncSig <mailHost> <mailAddress> <cerFileName> " + 54 "<algorithmName> <digestAlgorithm> <includingCert> <includingSignAttrib> " + 55 "<pfxFileName> [<attachment>]"+ 56 System.getProperty("line.separator") + 57 System.getProperty("line.separator") + 58 "Examples:" + 59 System.getProperty("line.separator") + 60 "java TestEncSig together.at recipient@together.at recipient512.cer " + 61 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 62 System.getProperty("line.separator") + 63 "java TestEncSig together.at recipient@together.at recipient512.cer " + 64 "DES MD5_WITH_RSA true true sender512.pfx .\\test\\Zip8Test.zip"); 65 66 System.exit(-1); 67 } 68 69 String smtpHost = args[0]; 70 String addressTO = args[1]; 71 String cerFileName = args[2]; 72 String algorithmName = args[3]; 73 String digestAlgorithm = args[4]; 74 75 boolean includingCert = true; 76 if (args[5].equals("true")) 77 includingCert = true; 78 else 79 includingCert = false; 80 81 boolean includingSignAttrib = true; 82 if (args[6].equals("true")) 83 includingSignAttrib = true; 84 else 85 includingSignAttrib = false; 86 87 String pfxfileName = args[7]; 88 89 String fileName = null; 90 if (args.length > 8) 91 fileName = args[8]; 92 93 String addressCC = "recipient@together.at"; 94 String addressBCC = "recipient@together.at"; 95 96 subject = args[2] + " " + args[3] + " " + args[4] + " " + args[5] + " " + 97 args[6] + " " + args[7] + " " + subject; 98 99 SignedAndEnvelopedSMIME ess = null; 100 101 try { 102 ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject, content, "ISO-8859-1"); 104 105 if (fileName!=null) { 106 ess.addAttachment(fileName); } 108 109 ess.setReply(from); 111 ess.addRecipient(addressTO, "TO", cerFileName); 115 ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); 119 ess.addSigner(pfxfileName, password, digestAlgorithm, includingCert, includingSignAttrib); 120 121 if (algorithmName.equals("RC240")) 122 { 123 System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... "); 124 ess.signingAndEnveloping("ENCRYPT_FIRST"); } 127 else if (algorithmName.equals("RC264")) 128 { 129 System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... "); 130 ess.signingAndEnveloping(ess.RC2_CBC, 64, "ENCRYPT_FIRST"); } 132 else if (algorithmName.equals("RC2128")) 133 { 134 System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... "); 135 ess.signingAndEnveloping(ess.RC2_CBC, 128, "ENCRYPT_FIRST"); } 137 else if (algorithmName.equals("DES")) 138 { 139 System.out.println("Creating enveloped and signed message with DES algorithm... "); 140 ess.signingAndEnveloping(ess.DES, 56, "ENCRYPT_FIRST"); } 142 else if (algorithmName.equals("3DES")) 143 { 144 System.out.println("Creating enveloped and signed message with 3DES algorithm... "); 145 ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "ENCRYPT_FIRST"); } 147 148 System.out.print("Sending enveloped and signed message ... "); 149 ess.send(); System.out.println("done."); 152 153 } 154 catch (Exception e) { 155 SMIMEException.setErrorFilePath("Log"); if(e instanceof SMIMEException) { 157 SMIMEException eTmp = (SMIMEException)e; 158 eTmp.displayErrors(null); 160 e = eTmp.getNonSMIMEException(); 161 if(e != null) 162 e.printStackTrace(); 163 } 164 else { 165 e.printStackTrace(); 166 } 167 } 168 } 169 } | Popular Tags |