package elte.java2_utikalauz5.crypto; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.security.spec.AlgorithmParameterSpec; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.CipherInputStream; import javax.crypto.CipherOutputStream; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec; /** Rejtjelezett adatfolyamok. @link.forrásfájl {@docRoot}/../data/crypto/src Crypt.java @link.letöltés {@docRoot}/../data/crypto Crypt.jar @since Java 2 Útikalauz programozóknak 5.0 */ public class Crypt { private static final int ITERATION_COUNT = 42/2; private static final byte[] SALT = {0x42, 0x21, 0x42, 0x21, 0x42, 0x21, 0x42, 0x21}; private final boolean isEncrpyt; private final String password; private final InputStream is; private final OutputStream os; public Crypt(boolean isEncrypt, String password, String inFileName, String outFileName) throws Exception { this.password = password; this.isEncrpyt = isEncrypt; FileInputStream fis = new FileInputStream(new File(inFileName)); File outFile = new File(outFileName); outFile.createNewFile(); FileOutputStream fos = new FileOutputStream(outFile); SecretKeyFactory skf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); KeySpec keySpec = new PBEKeySpec(password.toCharArray()); AlgorithmParameterSpec params = new PBEParameterSpec(SALT, ITERATION_COUNT); SecretKey key = skf.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); if( isEncrypt ) { cipher.init(Cipher.ENCRYPT_MODE, key, params); this.is = fis; this.os = new CipherOutputStream(fos,cipher); } else { cipher.init(Cipher.DECRYPT_MODE, key, params); this.os = fos; this.is = new CipherInputStream(fis,cipher); } } private void close() throws IOException { try { if( null != is ) { is.close(); } } finally { if( null != os ) { os.close(); } } } private void copy() throws IOException { byte[] buffer = new byte[10000]; int i; while( (i = is.read(buffer )) > -1 ) { os.write(buffer,0,i); } } /** * @param args * @throws IOException */ public static void main(String[] args) throws Exception { Crypt c = parseArgs(args); try { c.copy(); } finally { c.close(); } } private static final String PASSWORD_SWITCH = "--password:"; private static final String DECRYPT_SWITCH = "-d"; private static Crypt parseArgs(String[] args) throws Exception { String password = null; String inFileName = null; String outFileName = null; boolean isEncrypt = true; for( String arg: args ) { if( arg.startsWith("-")) { if( arg.startsWith(PASSWORD_SWITCH)) { password = arg.substring(PASSWORD_SWITCH.length()); } else if( arg.equals(DECRYPT_SWITCH)) { isEncrypt = false; } else { error("Unrecognized option: " + arg, -1); } } else if( null == inFileName ) { inFileName = arg; } else if( null == outFileName ) { outFileName = arg; } else { error("Too many file names: " + arg, -1); } } if( (null == password) || (null == inFileName) || (null == outFileName) ) { error("Missing argument!", -1); } return new Crypt(isEncrypt, password, inFileName, outFileName); } private static void error(String message, int exitCode) { if( null != message ) { System.err.println(message+"\n"); } System.err.println("Usage:"); System.err.println("\tCrypt [-d] --password: "); System.exit(exitCode); } }