import java.io.*; import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import com.sun.crypto.provider.SunJCE; public class DecryptHandler { static final String algorithm = "Blowfish/ECB/PKCS5Padding"; public static void main(String[] args) { //Security.addProvider(new SunJCE()); // 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.DECRYPT_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 dekrypterade data-filen byte[] plaintextData = 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); plaintextData = cipher.update(buffer, 0, length); bos.write(plaintextData); } plaintextData = cipher.doFinal(); bos.write(plaintextData); bis.close(); bos.close(); System.out.println("DONE DELIVERING DATA TO CIPHER ENGINE & SAVING PLAIN-TEXT 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"); } } }