// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class ArneDES {
    private final static int N = 1000000;
    public static void main(String[] args) throws Exception {
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        Cipher crypt = Cipher.getInstance("DES/ECB/PKCS5Padding");
        SecretKey key = new SecretKeySpec(args[3].getBytes(), "DES");
        byte[] buf = new byte[N];
        InputStream is = new FileInputStream(args[1]);
        int len = is.read(buf);
        is.close();
        if(args[0].equals("e")) {
            crypt.init(Cipher.ENCRYPT_MODE, key);
            buf = crypt.doFinal(buf, 0, len);
            
        }
        if(args[0].equals("d")) {
            crypt.init(Cipher.DECRYPT_MODE, key);
            buf = crypt.doFinal(buf, 0, len);
        }
        OutputStream os = new FileOutputStream(args[2]);
        os.write(buf);
        os.close();
    }
}
