import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.security.spec.*; //import com.sun.crypto.provider.SunJCE; //import com.dstc.security.provider.DSTC; public class EncryptHandler { static final String algorithm = "Blowfish/ECB/PKCS5Padding"; public static void main(String[] args) { //Security.addProvider(new SunJCE()); //Security.addProvider(new DSTC()); // Läs in privata nyckeln SecretKey secretKey = (SecretKey) KeyHandler.loadKey(args[1]); System.out.println("DONE LOADING SECRET KEY"); // Gör cipher Cipher cipher = null; try { cipher = Cipher.getInstance(algorithm); System.out.println("DONE MAKING CIPHER ENGINE"); } catch(NoSuchPaddingException nspe) { System.out.println("NoSuchPaddingException generated"); } catch(NoSuchAlgorithmException nsae) { System.out.println("NoSuchAlgorithmException generated"); } // Initialisera cipher try { cipher.init(Cipher.ENCRYPT_MODE, secretKey); System.out.println("DONE INITIALISING CIPHER ENGINE"); } catch(InvalidKeyException ike) { System.out.println("InvalidKeyException generated"); } // Tillför maskinen data-filen och spara den krypterade data-filen byte[] cipherData = null; try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0])); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[2])); byte[] buffer = new byte[1024]; int length; while(bis.available() != 0) { length = bis.read(buffer); cipherData = cipher.update(buffer, 0, length); bos.write(cipherData); } cipherData = cipher.doFinal(); bos.write(cipherData); bis.close(); bos.close(); System.out.println("DONE DELIVERING DATA TO CIPHER ENGINE & SAVING CIPHER DATA"); } catch(BadPaddingException bpe) { System.out.println("BadPaddingException generated"); } catch(IllegalBlockSizeException ibe) { System.out.println("IllegalBlockSizeException generated"); } catch(IOException ioe) { System.out.println("IOException generated"); } } }