private void getKey() throws Exception { String keyFile = Readline.readline("Key file: "); try { FileInputStream fis = new FileInputStream(keyFile.trim()); ObjectInputStream ois = new ObjectInputStream(fis); iKey = (Key) ois.readObject(); ois.close(); } catch (FileNotFoundException e) { System.out.println("Creating key"); long start = System.currentTimeMillis(); KeyGenerator gen = KeyGenerator.getInstance(iAlgo); gen.init(new SecureRandom()); iKey = gen.generateKey(); long end = System.currentTimeMillis(); System.out.println("...done. Elapsed time: " + (end-start) + " ms"); FileOutputStream fos = new FileOutputStream(keyFile); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(iKey); oos.close(); } } private void getCipher() throws Exception { iCipher = Cipher.getInstance(iAlgo+iMode); if (iEncrypt) iCipher.init(Cipher.ENCRYPT_MODE,iKey); else iCipher.init(Cipher.DECRYPT_MODE,iKey); } private void runCipher() throws Exception { FileOutputStream fos = new FileOutputStream(iOutFile); CipherOutputStream cos = new CipherOutputStream(fos,iCipher); FileInputStream stream = new FileInputStream(iInFile); byte[] sbuf = new byte[BUF_SIZE]; int length; System.out.println("Encrypting/Decrypting "); long start = System.currentTimeMillis(); while ((length=stream.read(sbuf)) != -1) cos.write(sbuf,0,length); stream.close(); cos.close(); long end = System.currentTimeMillis(); System.out.println("... done.Elapsed time: " + (end-start) + " ms"); }