/* * EncryptHandler.java * Karl-Adam Karlsson * 06-10-04 * * EncryptHandler, krypterar fil enligt given nyckel. * Använder Blowfish. * Usage: java EncryptHandler * * Där är filen som skall krypterasz * är nyckeln som används vid krypteringen, * är namnet på den resulterande krypterade filen. */ import javax.crypto.*; import java.security.*; import java.io.*; public class EncryptHandler{ /* * Konstruktor * Anrapar allt. * Klart! */ public EncryptHandler(String data, String secretKey, String encData){ saveAndEncrypt(getInS(data), loadKey(secretKey), encData); }//end of constructor /* * loadKey * Loads a SecretKey Object from the file named * @param keyFileName Name of file to load key from * @return SecretKey the loaded key. */ private SecretKey loadKey(String keyFileName){ ObjectInputStream oInStr = null; SecretKey theKey = null; try{ FileInputStream inFstream = new FileInputStream(keyFileName); oInStr = new ObjectInputStream(inFstream); }catch(FileNotFoundException fEx){ System.err.print(fEx); System.exit(1); }catch(IOException ioe){ System.err.print(ioe); System.exit(1); } try{ theKey = (SecretKey)oInStr.readObject(); oInStr.close(); }catch(IOException io2){ System.err.print(io2); System.exit(1); }catch(ClassNotFoundException cNf){ System.err.print(cNf); System.exit(1); } return theKey; }//end of loadKey /* * saveAndEncrypt * Krypterar till filen outname givet nyckeln akey och inströmmen inStream * */ private void saveAndEncrypt(FileInputStream inStream, SecretKey akey, String outName){ Cipher myCipher = null; File outFile = new File(outName); FileOutputStream outStream=null;; // CREATING CIPHER try{ myCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); myCipher.init(Cipher.ENCRYPT_MODE, akey); }catch(NoSuchAlgorithmException alEx){ System.err.print(alEx); System.exit(1); }catch(InvalidKeyException iKe){ System.err.print(iKe); System.exit(1); }catch(NoSuchPaddingException padEx){ System.err.print("NO SUCH PADDING"); System.exit(1); } // DONE CREATING CIPHER try{ outStream = new FileOutputStream(outFile); }catch(FileNotFoundException fnf){ System.err.print(fnf); System.exit(1); } CipherOutputStream cStream = new CipherOutputStream(outStream, myCipher); int aLen; byte[] buffLn = new byte[1024]; try{ //Encrypt and save while ((aLen = inStream.read(buffLn)) != -1) { cStream.write(buffLn, 0, aLen); } inStream.close(); cStream.close(); outStream.close(); }catch(IOException ioe){ System.err.print(ioe); System.exit(1); } }//end of saveAndEncrypt /* * getInS * Returns a FileInputStream connected to a file * @param String fimeName The name of the file to connect the stream to. * @return FileInputStream A stream connected to the file. */ private FileInputStream getInS(String fileName){ FileInputStream inFstream = null; try{ inFstream = new FileInputStream(fileName); }catch(FileNotFoundException fEx){ System.err.print(fEx); System.exit(1); } return inFstream; }//end of getInS /* * main * Kollar att args e ok, skapar sedan ny instans av EncryptHandler. */ public static void main (String[] args) { if(args==null || args.length != 3){ System.out.println("Usage: java EncryptHandler "); System.out.println("Where is the file to be encrypted."); System.out.println("Where is the file containing the key."); System.out.println("Where is the name of the output file."); } else if(args.length==3 && args[0]!=null && args[1]!=null && args[2]!=null){ new EncryptHandler(args[0], args[1], args[2]); } else if(args[0]==null||args[1]==null||args[2]==null){ System.out.println("Null is not a valid argument."); System.out.println("Usage: java EncryptHandler "); System.out.println("Where is the file to be encrypted."); System.out.println("Where is the file containing the key."); System.out.println("Where is the name of the output file."); } }//end of main }//end of class