1 7 8 package org.enhydra.oyster.test; 9 10 import javax.mail.Transport ; 11 import org.enhydra.oyster.smime.EnvelopedSMIME; 12 import org.enhydra.oyster.exception.SMIMEException; 13 14 39 public class TestEncryptKeyStore 40 { 41 42 public static void main (String [] args) 43 { 44 45 String subject = "S/MIME encrypted message - Subject test: ÜüÄäÖöÜüß"; 46 String content = "S/MIME encrypted message example\r\nContent test: ÜüÄäÖöÜüß!"; 47 String from = "sender@together.at"; 48 String password = "together"; 49 String keyStoreFile = "keystore.ks"; 50 51 if (args.length < 4) 52 { 53 System.err.println( 54 System.getProperty("line.separator") + 55 "Usage of TestEncryptKeyStore: " + 56 System.getProperty("line.separator") + 57 "java TestEncrypt <mailHost> <mailAddress> <cerKeyStoreAlias> " + 58 "<algorithmName> [<attachment>]" + 59 System.getProperty("line.separator") + 60 System.getProperty("line.separator") + 61 "Examples:" + 62 System.getProperty("line.separator") + 63 "java TestEncryptKeyStore together.at recipient@together.at " + 64 "recipient512.cer RC240 " + 65 System.getProperty("line.separator") + 66 "java TestEncryptKeyStore together.at recipient@together.at " + 67 "recipient512.cer DES .\\test\\Zip8Test.zip"); 68 System.exit(-1); 69 } 70 String smtpHost = args[0]; 71 String addressTO = args[1]; 72 String cerFileName = args[2]; 73 String algorithmName = args[3]; 74 String fileName = null; 75 if (args.length > 4) 76 fileName = args[4]; 77 78 String addressCC = "recipient@together.at"; 79 String addressBCC = "recipient@together.at"; 80 81 subject = algorithmName + " " + cerFileName + " " + subject; 82 83 EnvelopedSMIME es = null; 84 85 try 86 { 87 es = new EnvelopedSMIME(smtpHost, from, subject, content, "ISO-8859-1"); 89 90 if (fileName!=null) { 91 es.addAttachment(fileName); } 93 94 es.setReply(from); 96 String alias = cerFileName.replaceAll(".cer", ".pfx"); es.addRecipient(addressTO, "TO", keyStoreFile, es.BKS, password, alias); 101 if (algorithmName.equals("RC240")) 102 { 103 System.out.println("Creating the encrypted message with RC2_CBC - 40 bits algorithm... "); 104 es.enveloping(); } 107 else if (algorithmName.equals("RC264")) 108 { 109 System.out.println("Creating the encrypted message with RC2_CBC - 64 bits algorithm... "); 110 es.enveloping(es.RC2_CBC, 64); } 112 else if (algorithmName.equals("RC2128")) 113 { 114 System.out.println("Creating the encrypted message with RC2_CBC - 128 bits algorithm... "); 115 es.enveloping(es.RC2_CBC, 128); } 117 else if (algorithmName.equals("DES")) 118 { 119 System.out.println("Creating the encrypted message with DES algorithm... "); 120 es.enveloping(es.DES, 56); } 122 else if (algorithmName.equals("3DES")) 123 { 124 System.out.println("Creating the encrypted message with DES_EDE3_CBC algorithm... "); 125 es.enveloping(es.DES_EDE3_CBC, 192); } 127 System.out.print("Sending encrypted message ... "); 128 es.send(); System.out.println("done."); 131 132 } 133 catch (Exception e) { 134 SMIMEException.setErrorFilePath("Log"); if(e instanceof SMIMEException) { 136 SMIMEException eTmp = (SMIMEException)e; 137 eTmp.displayErrors(null); 139 e = eTmp.getNonSMIMEException(); 140 if(e != null) 141 e.printStackTrace(); 142 } 143 else { 144 e.printStackTrace(); 145 } 146 } 147 } 148 149 } 150 151 152 153 | Popular Tags |