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 TestEncSigKeyStore 44 { 45 46 public static void main(String [] args) 47 { 48 49 String subject = "S/MIME enveloped and signed message - Subject test: ÜüÄäÖöÜüß"; 50 String content = "S/MIME enveloped and 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 < 8) 56 { 57 System.err.println( 58 System.getProperty("line.separator") + 59 "Usage of TestEncSigKeyStore: " + 60 System.getProperty("line.separator") + 61 "java TestEncSig <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 TestEncSigKeyStore together.at recipient@together.at recipient512.cer " + 69 "RC240 SHA1_WITH_RSA true true sender512.pfx" + 70 System.getProperty("line.separator") + 71 "java TestEncSigKeyStore 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 ess = null; 108 109 try { 110 ess = new SignedAndEnvelopedSMIME(smtpHost, from, subject, content, "ISO-8859-1"); 112 113 if (fileName!=null) { 114 ess.addAttachment(fileName); } 116 117 ess.setReply(from); 119 String alias = cerFileName.replaceAll(".cer", ".pfx"); ess.addRecipient(addressTO, "TO", keyStoreFile, ess.BKS, password, alias); 124 ess.setCapabilities("SYMMETRIC", 5, 3, 1, 4, 2); ess.setCapabilities("ENCIPHER", 1, 0, 0, 0, 0); ess.setCapabilities("SIGNATURE", 3, 2, 1, 4, 0); 128 alias = pfxfileName; ess.addSigner(keyStoreFile, ess.BKS, password, alias, digestAlgorithm, 130 includingCert, includingSignAttrib); 132 if (algorithmName.equals("RC240")) 133 { 134 System.out.println("Creating enveloped and signed message with RC2 - 40 bits algorithm... "); 135 ess.signingAndEnveloping("ENCRYPT_FIRST"); } 138 else if (algorithmName.equals("RC264")) 139 { 140 System.out.println("Creating enveloped and signed message with RC2 - 64 bits algorithm... "); 141 ess.signingAndEnveloping(ess.RC2_CBC, 64, "ENCRYPT_FIRST"); } 143 else if (algorithmName.equals("RC2128")) 144 { 145 System.out.println("Creating enveloped and signed message with RC2 - 128 bits algorithm... "); 146 ess.signingAndEnveloping(ess.RC2_CBC, 128, "ENCRYPT_FIRST"); } 148 else if (algorithmName.equals("DES")) 149 { 150 System.out.println("Creating enveloped and signed message with DES algorithm... "); 151 ess.signingAndEnveloping(ess.DES, 56, "ENCRYPT_FIRST"); } 153 else if (algorithmName.equals("3DES")) 154 { 155 System.out.println("Creating enveloped and signed message with 3DES algorithm... "); 156 ess.signingAndEnveloping(ess.DES_EDE3_CBC, 192, "ENCRYPT_FIRST"); } 158 159 System.out.print("Sending enveloped and signed message ... "); 160 ess.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 |