    001package org.apache.commons.ssl;
    002
    003import java.io.File;
    004import java.io.FileInputStream;
    005import java.io.FileOutputStream;
    006import java.io.IOException;
    007import java.util.Iterator;
    008import java.util.Map;
    009import java.util.Properties;
    010import java.util.TreeSet;
    011
    012/**
    013 * @author Julius Davies
    014 * @since 4-Jul-2007
    015 */
    016public class PBETestCreate {
    017
    018    public static void main(String[] args) throws Exception {
    019        FileInputStream in = new FileInputStream(args[0]);
    020        Properties p = new Properties();
    021        p.load(in);
    022        in.close();
    023
    024        String targetDir = p.getProperty("target");
    025        File dir = new File(targetDir);
    026        dir.mkdirs();
    027        if (!dir.exists()) {
    028            throw new IOException(dir.getCanonicalPath() + " doesn't exist!");
    029        }
    030
    031        TreeSet ciphers = new TreeSet();
    032        Iterator it = p.entrySet().iterator();
    033        while (it.hasNext()) {
    034            Map.Entry entry = (Map.Entry) it.next();
    035            String key = (String) entry.getKey();
    036            if (!"target".equalsIgnoreCase(key)) {
    037                ciphers.add(key);
    038                ciphers.add(key + "-cbc");
    039                ciphers.add(key + "-cfb");
    040                ciphers.add(key + "-cfb1");
    041                ciphers.add(key + "-cfb8");
    042                ciphers.add(key + "-ecb");
    043                ciphers.add(key + "-ofb");
    044            }
    045        }
    046
    047        byte[] toEncrypt = "Hello World!".getBytes("UTF-8");
    048        char[] pwd = "changeit".toCharArray();
    049        it = ciphers.iterator();
    050        while (it.hasNext()) {
    051            String cipher = (String) it.next();
    052            String cipherPadded = Util.pad(cipher, 15, false);
    053            String fileNameBase64 = cipher + ".base64";
    054            String fileNameRaw = cipher + ".raw";
    055            String d = dir.getCanonicalPath() + "/";
    056            try {
    057                byte[] base64 = OpenSSL.encrypt(cipher, pwd, toEncrypt, true);
    058                FileOutputStream out = new FileOutputStream(d + fileNameBase64);
    059                out.write(base64);
    060                out.close();
    061            }
    062            catch (Exception e) {
    063                System.err.println("FAILURE \t" + cipherPadded + "\t" + fileNameBase64 + "\t" + e);
    064            }
    065
    066            try {
    067                byte[] raw = OpenSSL.encrypt(cipher, pwd, toEncrypt, false);
    068                FileOutputStream out = new FileOutputStream(d + fileNameRaw);
    069                out.write(raw);
    070                out.close();
    071            }
    072            catch (Exception e) {
    073                System.err.println("FAILURE \t" + cipherPadded + "\t" + fileNameRaw + "\t" + e);
    074            }
    075
    076        }
    077    }
    078
    079}