/* * [Transporter.java] * * Summary: Demonstration of how to use the Transporter to encrypt and package messages for transport. * * Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.4 2006-03-06 reformat with IntelliJ and add Javadoc. */ package com.mindprod.transporter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import static java.lang.System.*; /** * Demonstration of how to use the Transporter to encrypt and package messages for transport. * * @author Roedy Green, Canadian Mind Products * @version 1.4 2006-03-06 * @since 2004-06-08 */ public final class Transporter { /** * true if want debugging output */ private static final boolean DEBUGGING = false; private static final int FIRST_COPYRIGHT_YEAR = 2004; /** * undisplayed copyright notice */ private static final String EMBEDDED_COPYRIGHT = "Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com"; private static final String RELEASE_DATE = "2006-03-06"; /** * embedded version string. */ private static final String VERSION_STRING = "1.4"; /** * Display an array of strings * * @param s array of strings to display */ private static void displayStrings( String[] s ) { for ( final String value : s ) { out.println( value ); } } /** * Test/Demonstrate com.mindprod.transporter classes * * @param args not used */ public static void main( String[] args ) { try { String[] originalMessage = { "aardvark", "absurd", "accrue", "acme", "adrift", "adult", "afflict", "ahead", "aimless", "Algol", "allow", "alone", "ammo", "ancient", "apple", "artist", "assume", "Athens", "atlas", "Aztec", "baboon", "backfield", "backward", "banjo", "beaming", "bedlamp", "beehive", "beeswax", "befriend", "Belfast", "berserk", "billiard", "bison", "blackjack", "blockade", "blowtorch", "bluebird", "bombast", "bookshelf", "brackish", "breadline", "breakup", "brickyard", "briefcase", "Burbank", "button", "buzzard", "cement", "chairlift", "chatter", "checkup", "chisel", "choking", "chopper", "Christmas", "clamshell", "classic", "classroom", "cleanup", "clockwork", "cobra", "commence", "concert", "cowbell", "crackdown", "cranky", "crowfoot", "crucial", "crumpled", "crusade", "cubic", "dashboard", "deadbolt", "deckhand", "dogsled", "dragnet", "drainage", "dreadful", "drifter", "dropper", "drumbeat", "drunken", "Dupont", "dwelling", "eating", "edict", "egghead", "eightball", "endorse", "endow", "enlist", "erase", "escape", "exceed", "eyeglass", "eyetooth", "facial", "fallout", "flagpole", "flatfoot", "flytrap", "fracture", "framework", "freedom", "frighten", "gazelle", "Geiger", "glitter", "glucose", "goggles", "goldfish", "gremlin", "guidance", "hamlet", "highchair", "hockey", "indoors", "indulge", "inverse", "involve", "island", "jawbone", "keyboard", "kickoff", "kiwi", "klaxon", "locale", "lockup", "merit", "minnow", "miser", "Mohawk", "mural", "music", "necklace", "Neptune", "newborn", "nightbird", "Oakland", "obtuse", "offload", "optic", "orca", "payday", "peachy", "pheasant", "physique", "playhouse", "Pluto", "preclude", "prefer", "preshrunk", "printer", "prowler", "pupil", "puppy", "python", "quadrant", "quiver", "quota", "ragtime", "ratchet", "rebirth", "reform", "regain", "reindeer", "rematch", "repay", "retouch", "revenge", "reward", "rhythm", "ribcage", "ringbolt", "robust", "rocker", "ruffled", "sailboat", "sawdust", "scallion", "scenic", "scorecard", "Scotland", "seabird", "select", "sentence", "shadow", "shamrock", "showgirl", "skullcap", "skydive", "slingshot", "slowdown", "snapline", "snapshot", "snowcap", "snowslide", "solo", "southward", "soybean", "spaniel", "spearhead", "spellbind", "spheroid", "spigot", "spindle", "spyglass", "stagehand", "stagnate", "stairway", "standard", "stapler", "steamship", "sterling", "stockman", "stopwatch", "stormy", "sugar", "surmount", "suspense", "sweatband", "swelter", "tactics", "talon", "tapeworm", "tempest", "tiger", "tissue", "tonic", "topmost", "tracker", "transit", "trauma", "treadmill", "Trojan", "trouble", "tumor", "tunnel", "tycoon", "uncut", "unearth", "unwind", "uproot", "upset", "upshot", "vapor", "village", "virus", "Vulcan", "waffle", "wallet", "watchword", "wayside", "willow", "woodlark", "Zulu" }; // load keys, previously prepared with: // java.exe com.mindprod.transporter.MakeKeys testSender 1024 // and: // java.exe com.mindprod.transporter.MakeKeys testReceiver 1024 // DON'T USE THESE TEST KEYS IN YOUR OWN APPLICATIONS!! // THEY ARE ANYTHING BUT SECRET!! PrivateKey senderPrivateKey = new PrivateKey( new FileInputStream( "testSenderPrivateKey.ser" ) ); PrivateKey receiverPrivateKey = new PrivateKey( new FileInputStream( "testReceiverPrivateKey.ser" ) ); PublicKey senderPublicKey = new PublicKey( new FileInputStream( "testSenderPublicKey.ser" ) ); PublicKey receiverPublicKey = new PublicKey( new FileInputStream( "testReceiverPublicKey.ser" ) ); out.println( "Testing com.mindprod.transporter classes" ); out.println(); out.println( "Sender keySize " + senderPrivateKey.keySize ); out.println( "Receiver keySize " + receiverPrivateKey.keySize ); out.println(); long start; long stop; { out.println( "test the Transporter Wrap/Unwrap with no encryption or signing." ); start = System.currentTimeMillis(); Wrap w = new Wrap(); // you can send any Serializable object, including Strings and // raw byte arrays. String transport = w.wrap( originalMessage ); stop = System.currentTimeMillis(); out.println( "wrap elapsed " + ( stop - start ) + " milliseconds" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); Unwrap u = new Unwrap(); String[] reconstitutedMessage = ( String[] ) u.unwrap( transport ); stop = System.currentTimeMillis(); out.println( "unwrap elapsed " + ( stop - start ) + " milliseconds" ); if ( DEBUGGING ) { displayStrings( reconstitutedMessage ); } out.println(); } { out.println( "test Encrypt/Decrypt, encryption but no signing." ); start = System.currentTimeMillis(); Encrypt e = new Encrypt( receiverPublicKey ); String transport = e.wrap( originalMessage ); stop = System.currentTimeMillis(); out.println( "encrypt elapsed " + ( stop - start ) + " milliseconds" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); Decrypt d = new Decrypt( receiverPrivateKey ); String[] reconstitutedMessage = ( String[] ) d.unwrap( transport ); stop = System.currentTimeMillis(); out.println( "decrypt elapsed " + ( stop - start ) + " milliseconds" ); if ( DEBUGGING ) { displayStrings( reconstitutedMessage ); } out.println(); } { out.println( "test Sign/Verify digital signature without encryption." ); start = System.currentTimeMillis(); Sign s = new Sign( senderPrivateKey ); String transport = s.wrap( originalMessage ); stop = System.currentTimeMillis(); out.println( "sign elapsed " + ( stop - start ) + " milliseconds" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); Verify v = new Verify( senderPublicKey ); String[] reconstitutedMessage = ( String[] ) v.unwrap( transport ); stop = System.currentTimeMillis(); if ( DEBUGGING ) { displayStrings( reconstitutedMessage ); } out.println( "verify elapsed " + ( stop - start ) + " milliseconds" ); out.println(); } { out.println( "test SignAndEncrypt/DecryptAndVerify" ); start = System.currentTimeMillis(); SignAndEncrypt s = new SignAndEncrypt( senderPrivateKey, receiverPublicKey ); String transport = s.wrap( originalMessage ); stop = System.currentTimeMillis(); out.println( "sign and encrypt elapsed " + ( stop - start ) + " milliseconds" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); DecryptAndVerify v = new DecryptAndVerify( receiverPrivateKey, senderPublicKey ); String[] reconstitutedMessage = ( String[] ) v.unwrap( transport ); stop = System.currentTimeMillis(); out.println( "decrypt and verify elapsed " + ( stop - start ) + " milliseconds" ); if ( DEBUGGING ) { displayStrings( reconstitutedMessage ); } out.println(); } } catch ( FileNotFoundException e ) { err.println( "can't find key files" ); } catch ( IOException e ) { err.println( "Some problem with I/O " + e.getMessage() ); } } }